package AccountManager::Account;

use Mojo::Base 'AccountManager::DB::Object';

use Rose::DB::Object::Manager;

__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 },
        entityid        => { type => 'varchar', length => 250, not_null => 1 },
    ],

    primary_key_columns => [ 'id' ],
);

Rose::DB::Object::Manager->make_manager_methods('accounts');

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

    printf $fd
        "Account ID=%s; password_hash=%s; entityid=%s; profile=%s; scope=%s; creation_date=%s; expiration_date=%s\n",
        $self->id(),
        $self->password_hash(),
        $self->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();
}

1;