package AccountManager::Account;

use utf8;
use strict;
use warnings;

use base 'AccountManager::DB::Object';

__PACKAGE__->meta->setup(
    table   => 'accounts',

    columns => [
        id              => { type => 'bigserial', not_null => 1 },
        password_hash   => { type => 'varchar', length => 50, not_null => 1 },
        password_crypt  => { type => 'varchar', length => 50, not_null => 1 },
        password        => { type => 'varchar', length => 50, nonpersistent => 1 },
        token           => { type => 'varchar', length => 50, not_null => 1 },
        creation_date   => { type => 'datetime' },
        expiration_date => { type => 'datetime' },
        profile         => { type => 'varchar', length => 100, not_null => 1 },
        scope           => { type => 'varchar', length => 100, not_null => 1 },
        sp_entityid     => { type => 'varchar', length => 250, not_null => 1 },
    ],

    primary_key_columns => [ 'id' ],
);

my %cn = (
    alumni1        => 'Åsold Wahlstrøm - eduGAIN Access Check account',
    employee1      => 'Linnéa Hsu - eduGAIN Access Check account',
    fullset1       => 'Gundabald Lightfoot - eduGAIN Access Check account',
    librarywalkin1 => 'Ramón Núñez - eduGAIN Access Check account',
    researcher1    => 'Stéphane Larivière - eduGAIN Access Check account',
    student1       => 'Ciarán MacCárthaigh - eduGAIN Access Check account',
    student2       => 'Damiën Kuijper - eduGAIN Access Check account',
    teacher1       => 'Peter Müller - eduGAIN Access Check account',
);

my %givenName = (
    fullset1       => 'Gundabald',
);

my %sn = (
    fullset1       => 'Lightfoot - eduGAIN Access Check account',
);

my %mail = (
    alumni1        => 'asold.wahlstrom',
    employee1      => 'linnea.hsu',
    fullset1       => 'gundabald.lightfoot',
    generic1       => 'forearartian',
    librarywalkin1 => 'ramon.nunez',
    researcher1    => 'stephane.lariviere',
    student1       => 'ciaran.maccarthaigh',
    student2       => 'damien.kuijper',
    teacher1       => 'peter.muller',
);

my %affiliation = (
    alumni1        => [ qw/alum/ ],
    employee1      => [ qw/member staff employee/ ],
    fullset1       => [ qw/member faculty/ ],
    librarywalkin1 => [ qw/library-walk-in/ ],
    researcher1    => [ qw/member faculty/ ],
    student1       => [ qw/member student/ ],
    student2       => [ qw/member student faculty/ ],
    teacher1       => [ qw/member faculty/ ],
);

my %scopedAffiliation = (
    alumni1        => [ qw/alum/ ],
    employee1      => [ qw/member staff employee/ ],
    fullset1       => [ qw/member faculty/ ],
    librarywalkin1 => [ qw/library-walk-in/ ],
    researcher1    => [ qw/member faculty/ ],
    student1       => [ qw/member student/ ],
    student2       => [ qw/member student faculty/ ],
    teacher1       => [ qw/member faculty/ ],
    teacher2       => [ qw/member faculty/ ],
);

my %comment = (
    alumni1        => 'An ex-student with "alum" value for eduPersonAffiliation.',
    employee1      => 'A person with "member", "staff" and "employee" values for eduPersonAffiliation.',
    fullset1       => 'A person with all eduGAIN user attributes plus givenName and surname.',
    generic1       => 'A person with a limited set of attributes (eduPersonPrincipalName, mail and displayName).',
    librarywalkin1 => 'A library user on the campus network.',
    researcher1    => 'A researcher with only an eduPersonTargetedID attribute.',
    student1       => 'A student with "member" and "student" values for eduPersonAffiliation.',
    student2       => 'A PhD student with "member", "student" and "faculty" values for eduPersonAffiliation.',
    teacher1       => 'A teacher with a limited set of attributes (eduPersonScopedAffiliation and eduPersonTargetedID).',
    teacher2       => 'A teacher with "member" and "faculty" values for eduPersonAffiliation.',
);

sub print {
    my ($self, $fd) = @_;
    $fd = \*STDOUT unless $fd;

    printf $fd
        "Account ID=%s; password_hash=%s; sp_entityid=%s; profile=%s; scope=%s; creation_date=%s; expiration_date=%s\n",
        $self->id(),
        $self->password_hash(),
        $self->sp_entityid(),
        $self->profile(),
        $self->scope(),
        $self->creation_date()->strftime('%Y:%m:%d'),
        $self->expiration_date()->strftime('%Y:%m:%d');
}

sub internal_uid {
    my ($self) = @_;
    return 'user' . $self->id();
}

sub cn {
    my ($self) = @_;
    return $cn{$self->profile()};
}

sub displayName {
    my ($self) = @_;
    return $cn{$self->profile()};
}

sub givenName {
    my ($self) = @_;
    return $givenName{$self->profile()};
}

sub mail {
    my ($self) = @_;
    my $prefix = $mail{$self->profile()};
    return $prefix ?
        $prefix . '@' . $self->{scope} : undef;
}

sub eduPersonAffiliation {
    my ($self) = @_;
    my $affiliations = $affiliation{$self->profile()} || [];

    return @$affiliations;
}

sub eduPersonScopedAffiliation {
    my ($self) = @_;
    my $affiliations = $scopedAffiliation{$self->profile()} || [];

    return map { $_ . '@' . $self->{scope} } @$affiliations;
}

sub eduPersonPrincipalName {
    my ($self) = @_;
    return $self->id() . '@'. $self->{scope};
}

sub schacHomeOrganization {
    my ($self) = @_;
    return $self->{scope};
}

sub schacHomeOrganizationType {
    my ($self) = @_;
    return "urn:schac:homeOrganizationType:int:other";
}

sub comment {
    my ($self) = @_;
    return $comment{$self->profile()};
}

sub associatedSP {
    my ($self) = @_;
    return $self->{sp_entityid};
}

1;