package myHive::Model::Authentication;
use Moose;
use MooseX::Params::Validate;
use Moose::Util::TypeConstraints;
use namespace::autoclean;
use TryCatch;
use myHive::Model::DB::Authentication;
use Data::Dumper;
use Digest::MD5 qw(md5_hex);
use strict;
use warnings;

extends 'Catalyst::Model';

=head1 NAME

myHive::Model::Authentication - Catalyst Model

=head1 DESCRIPTION

Catalyst Model.

=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

#subtype 'myHive::Model::DB::Authentication' => as Object => where { $_->isa('myHive::Model::DB::Authentication') };


has 'handler'	=> ( is => 'ro', isa => 'myHive::Model::DB::Authentication', default => sub { 
		myHive::Model::DB::Authentication->new();
	} );
has 'username'	=> ( is => 'rw', isa => 'Str' );
has 'password'	=> ( is => 'rw', isa => 'Str' );
has 'mafref'	=> ( is => 'rw', isa => 'Str' );

sub BUILD {
	my ( $self ) = @_;
	#$self->handler( myHive::Model::DB->new );
	return;
}


sub checkUserCredentials{
	try {
		my  ( $self,  %params ) = validated_hash(
                        \@_,
                        username => { isa => 'Str' },
                        password => { isa => 'Str' },
			mafref   => { isa => 'Str' },
                );
                my $md5 = md5_hex($params{password});
                return  $self->handler->checkCredentialsUser(
                        where     => qq/username='$params{username}' and password='$md5' and maf_ref= '$params{mafref}'/,
                        log       => 1,
                );

        }
        catch ($e) {
                print STDERR "something gone wrong" . Dumper($e);
                return;
        }

}



sub checkCredentials {
	try {
		my  ( $self,  %params ) = validated_hash(
          		\@_,
          		username => { isa => 'Str' },
			password => { isa => 'Str' },
		);
		my $md5 = md5_hex($params{password});
		return  $self->handler->checkCredentials(
                        where     => qq/username='$params{username}' and password='$md5'/,
			log	  => 1,
                );

	}
	catch ($e) {
		print STDERR "something gone wrong" . Dumper($e);
		return;
	}

	
	
}

sub getUserName {
	my ( $self, $account_id ) = @_;
	my $account = $account_id;
        my $sth = $self->handler->selectSTH(
                        table   => ['account'],
                        fields  => ['l_name', 'f_name'],
                        where   => qq/id = $account /,
                        log     => 1,
                );
        my $row = $sth->fetchrow_hashref;
        return qq/$row->{f_name} $row->{l_name}/;


}

sub register {
	try {
		my  ( $self,  %params ) = validated_hash(
                        \@_,
			title		=> { isa => 'Str', optional	=> 1 },
			maf_ref 	=> { isa => 'Str', optional	=> 0 },
			f_name		=> { isa => 'Str', optional 	=> 0 },
			l_name		=> { isa => 'Str', optional	=> 0 },
                        email 		=> { isa => 'Str', optional	=> 0 },
			company_name	=> { isa => 'Str', optional	=> 1 },
			address_1	=> { isa => 'Str', optional	=> 0 },
			address_2	=> { isa => 'Str', optional	=> 1 },
			postcode	=> { isa => 'Str', optional	=> 1 },
			city		=> { isa => 'Str', optional 	=> 0 },
			username	=> { isa => 'Str', optional	=> 0 },
			password	=> { isa => 'Str', optional	=> 0 },
			
                );
		$params{password}  = md5_hex($params{password});
		return $self->handler->insert(
				table => qw[account],
				data => \%params,
				log     => 1,
			);
		


	}
	catch ( $e ) {
		print STDERR "register : something went wrong $e";
		return;


	}
	
return;
}

__PACKAGE__->meta->make_immutable;

1;
