package Vote::DB::Voting;

# $Id$

use strict;
use warnings;
use Mail::Mailer;
use base 'Vote::DB::common';
use Vote::DB::Poll;

=head1 NAME

Vote::Model::Vote - Catalyst Model

=head1 DESCRIPTION

Catalyst Model.

=cut

sub new {
    my ($class, $dbstring, $key) = @_;
    
    bless {
        key => $key,
        dbstring => $dbstring,
        db => Vote::DB::common::_newdb($dbstring),
    }, $class;
}

sub votingkey { $_[0]->{key} }

sub poll {
    my ($self) = @_;
    Vote::DB::Poll->new($self->{dbstring}, $self->info->{poll});
}

sub info {
    my ($self) = @_;

    my $sth = $self->db->prepare_cached(
        q{
        select *, voting.key as vkey from voting left join signing
        on signing.key = voting.key
        where voting.key = ?
        }
    );
    $sth->execute($self->votingkey);

    my $res = $sth->fetchrow_hashref;
    $sth->finish;
    $res
}

sub auth {
    my ($self, $password) = @_;
    my $userinfo = $self->info or return;

    $userinfo->{passwd} or return;
    if (crypt($password, $userinfo->{passwd} || '') eq $userinfo->{passwd}) {
        return 1;
    } else {
        return 0;
    }
}

sub has_sign {
    my ($self) = @_;

    my $sth = $self->db->prepare_cached(
        q{
        select date from signing join voting
        on voting.key = signing.key
        where voting.key = ?
        }
    );

    $sth->execute($self->votingkey);
    my $res = $sth->fetchrow_hashref;
    $sth->finish;
    return $res->{date}
}

sub mail_voting_passwd {
    my ($self, $mailinfo) = @_;
    
    my $vinfo = $self->info or return;
    my $voteinfo = $self->poll->info;
    $voteinfo->{description} ||= "";

    my $passwd = Vote::DB::common::random_string(8);
    my $encpasswd = $self->gen_enc_passwd($passwd);

    my $upd_voting = $self->db->prepare_cached(
        q{update voting set passwd = ? where key = ?}
    );

    $upd_voting->execute($encpasswd, $self->votingkey);

    my $date = $voteinfo->{dstart} && $voteinfo->{dend}
        ? sprintf("\n" . 'Vous pourrez voter entre le %s %s et le %s %s' . "\n",
            $voteinfo->{dstart}, $voteinfo->{hstart}, $voteinfo->{dend}, $voteinfo->{hend})
        : '';

    # TODO complete this properly:
    my $mailer = new Mail::Mailer 'smtp', Server => (Vote->config->{smtp} || 'localhost');
    $ENV{MAILADDRESS} = $voteinfo->{owner};
    $mailer->open({
        From => $voteinfo->{owner},
        To => $vinfo->{mail},
        Subject => 'Invitation a voter: ' . $voteinfo->{label},
        'X-Epoll-poll' => $self->poll->voteid,
        Vote::DB::common::mail_header(),
    });
    print $mailer <<EOF;
Vous êtes convié à participer a ce vote:

--------
$voteinfo->{label}
--------
$voteinfo->{description}
--------

à l'adresse:

$mailinfo->{voteurl}
$date

-- 
Votre identifiant est: $vinfo->{mail}
Votre mot de passe est: $passwd

Conservez précieusement ces identifiants, il ne vous seront pas retransmits.

Cordialement.
EOF
    $mailer->close or warn "couldn't send whole message: $!\n";

    $self->db->commit;
}

=head1 AUTHOR

Thauvin Olivier

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself or CeCILL.

=cut

1;
