package myHive::Controller::Rest::JSON;
use Moose;
use namespace::autoclean;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

myHive::Controller::Rest::JSON - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


sub begin : Private {
        my ( $self, $c ) = @_;

        $c->config->{session}->{'Catalyst::Plugin::Session'} = 'bug';#horrible
        $c->{authentication}    = myHive::Model::Authentication->new;
        $c->{landowner}         = myHive::Model::Landowner::Manager->new;
        $c->{area}              = myHive::Model::Area::Manager->new;


        return;
}


=head2 index

=cut

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;

    $c->response->body('Matched myHive::Controller::Rest::JSON in Rest::JSON.');
}


sub authenticate : Local : Args() {
        my ( $self, $c ) = @_; 
        my $username = $c->request->query_parameters->{username};
        my $password = $c->request->query_parameters->{password};
        
        if ( !exists  $c->session->{account_id} ) {
                 my $account_id = $c->{authentication}->checkUserCredentials(
                                        username => $username,
                                        password => $password,
                                );
                 print STDERR "account id: $account_id";
                 if ( $account_id )  { 
                        $c->session(
                                {
                                        loggedin        => 'yes',
                                        username        =>  $username,
                                        account_id      =>  $account_id,
                                        seed            =>  $c->generate_session_id,
                                        expires         =>  5
                                }
                        );
                }


        }

        $c->stash->{seed} =  $c->session->{seed} || 'failed';
        $c->stash->{template} = 'rest/json_authentication.tt';
        return;
}



=head1 AUTHOR

root

=head1 LICENSE

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

=cut

__PACKAGE__->meta->make_immutable;

1;
