package myHive::Model::Landowner::Manager;
use Moose;
use MooseX::Params::Validate;
use Moose::Util::TypeConstraints;

use namespace::autoclean;
use TryCatch;
use myHive::Model::DB;
use myHive::Model::Landowner;
use Data::Dumper;


extends 'Catalyst::Model';

=head1 NAME

myHive::Model::Landowner - 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

has 'handler'		=> ( is => 'ro' , isa => 'myHive::Model::DB', default => sub 
				{
					myHive::Model::DB->new();
				});
has 'collection'	=> ( is => 'rw', isa => 'ArrayRef', default => sub { [] }  );



sub add {
	try {
		my ( $self, %params ) = validated_hash( 
			 \@_,
			account 	=> { isa => 'Str' },
			land_name 	=> { isa => 'Str' },
			city		=> { isa => 'Str' },
			ref_number	=> { isa => 'Str', optional => 1 },
			company		=> { isa => 'Str', optional => 1 },
			address_1	=> { isa => 'Str', optional => 1 },
			address_2       => { isa => 'Str', optional => 1 },
			postcode        => { isa => 'Str', optional => 1 },
		
		);	 
		$params{name} = $params{land_name};
		delete $params{land_name};
		#new hash not hashref
		#my %newHash = ();
		#foreach my $k ( keys %{ $params } ) {
		#	if ( $k eq 'land_name' ) {
		#	$newHash{name} = $params->{$k};
		#	}
		#	else {
		#		$newHash{$k} = $params->{$k};
		#	}
		#}

		return  $self->handler->insert(
				table    => ['landowner'],
				data     =>  \%params,
				log	=> 1
		);	
	}
	catch ( $e ) {
		print STDERR "landowner->add failed: ". Dumper($e);
		return;
	}
	return;
}


sub getAllLandowners {



}

sub getLandowner {
	my ( $self, %params ) = @_;
	my $sth = $self->handler->selectSTH(
			table  => ['landowner'],
			fields => ['*'],
			where  => qq/id = $params{id}/,
			log    => 1,
		);
	my $row = $sth->fetchrow_hashref;
	$sth->finish;
	return myHive::Model::Landowner->new(
                        account         => $row->{account},
                        id              => $row->{id},
                        ref_number      => $row->{ref_number},
                        name            => $row->{name},
                        company         => $row->{company},
                        address_1       => $row->{address_1},
                        address_2       => $row->{address_2},
                        postcode        => $row->{postcode},
                        city            => $row->{city},
                );
	

}

sub getLandowners {
	my ( $self, %params ) = @_;
	
	my $sth = $self->handler->selectSTH(
			table  	=> ['landowner'],
			fields 	=> ['*'],
			where  	=> qq/account = $params{account}/,
			log	=> 1,
	);
	while ( my $row = $sth->fetchrow_hashref ) {
		my $landowner = myHive::Model::Landowner->new(
			account 	=> $row->{account},
			id		=> $row->{id},
			ref_number 	=> $row->{ref_number},
			name		=> $row->{name},
			company		=> $row->{company},
			address_1	=> $row->{address_1},
			address_2	=> $row->{address_2},
			postcode	=> $row->{postcode},
			city		=> $row->{city},
		);
		print STDERR Dumper($landowner);
		push (  @{ $self->collection } , $landowner );
		#$self->collection( $landowner );

	}
	$sth->finish; 
	print STDERR Dumper($self->collection);

	return $self->collection;

}


sub update {
	my ( $self, %params ) = @_;
	print STDERR Dumper(\%params);
	return	$self->handler->update(
			table => ['landowner'],
			data  => {
					name 		=> $params{land_name},
					company		=> $params{company},
					ref_number	=> $params{ref_number},
					address_1	=> $params{address_1},
					address_2	=> $params{address_2},
					postcode	=> $params{postcode},
					city		=> $params{city},
				},
			where	=> qq/account = $params{account} and id = $params{id}/,
			log	=> 1,
		);



}





__PACKAGE__->meta->make_immutable;

1;
