package myHive::Model::DB::Site;
use Moose;
use namespace::autoclean;
use Data::Dumper;


#extends 'Catalyst::Model';
extends 'myHive::Model::DB';


=head1 NAME

myHive::Model::DB::Site - 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.

#has 'handler' => (is => 'rw', isa => 'myHive::Model::DB', default => sub { myHive::Model::DB->new(); } );

=cut

=head2 addSite

the input string must match the stored function exactly

	account         integer,
        maf_ref         varchar(100),
        area            integer,
        landowner       integer,
        name            varchar(100),
        description     text ,
        address_1       varchar(100),
        address_2       varchar(100),
        latlng          varchar(20),
        brood           int[]


=cut

sub addSite {
	my ( $self, %params ) = @_;
	my $procedure = 'addSite';
	my $account 	= $params{account};
	my $maf		= $params{maf_ref};
	my $area	= $params{area};
	my $landowner	= $params{landowner};
	my $name	= $params{name};
	my $desc	= $params{descr};
	my $add1	= $params{address_1};
	my $add2	= $params{address_2};
	my $latlng	= $params{latlng};
	my $broodtype	= $params{broodtypes};
	my $siteId	= $params{siteid};

	
	my $in_string = qq/$account , $siteId,'$maf' , $area , $landowner , '$name' , '$desc' , '$add1' ,'$add2' , '$latlng' , '$broodtype'::INT[][]/;
        
        my $stmt      = qq/select $procedure($in_string)/;

        my ($sth,@bind)       = $self->{dbh}->prepare($stmt);
        #check to see if we should log
        if ( exists $params{log} && $params{log} == 1 ) {
                print STDERR Dumper($stmt);
		print STDERR Dumper(\@bind);
        }

        $sth->execute;
        return $sth;
	
}

sub addSchedule {
	my ( $self, %params ) = @_;
	print STDERR Dumper(\%params);
	my $procedure 		= 'addSchedule';
	my $account     	= $params{account};
	my $siteId      	= $params{siteId};
	my $date     		= $params{date};
	my $inspectionType	= $params{reason};
	my $info		= $params{additionalInfo};
	my $stmt      = qq/select $procedure($account, $siteId,'$date', $inspectionType, '$info')/;
	my ($sth,@bind)       = $self->{dbh}->prepare($stmt);
	#check to see if we should log
        if ( exists $params{log} && $params{log} == 1 ) {
                print STDERR Dumper($stmt);
                print STDERR Dumper(\@bind);
        }

        $sth->execute;
        return $sth;
	
	

	
}

=head2 addSiteInspection


hash that is passed as %params
 {
           'pestcontrol' => 'None',
           'account' => '1',
           'inspectionId' => '30',
           'date' => '10/07/2013',
           'photo' => 'no photo',
           'health' => '(null)',
           'afp' => 'no',
           'siteId' => '3',
           'type' => 'Inspection',
           'broods' => '{{4,2},{5,5},{6,0}}'

};

what the stored procedure us expecting
	s_account               integer,
        s_site_id               integer,
        s_date                  varchar(10),
        s_health                varchar(100),
	s_hives			varchar(10),
        s_type                  varchar(100),
        s_afp                   varchar(1),
        s_pestcontrol           varchar(100),
        s_info                  text,
        s_photo                 varchar(100),
        s_brood                 int[][]
	s_hash			text,




=cut

sub addSiteInspection {
	my ( $self, %params ) = @_;
	print STDERR "addSiteInspection DB call" . Dumper(\%params);
	my $procedure           = 'addsiteinspection';
        my $account             = $params{account};
        my $siteId              = $params{siteId};
	#my $inpsId		= $params{inspectionId};
        my $date                = $params{date};
        my $photo      		= $params{photo};
	my $health		= $params{health};
	my $hives		= $params{hives};
	my $afb			= $params{afb} eq 'no' ? 'N' : 'Y';
        my $info                = $params{info} || '';
	my $pest		= $params{pestcontrol};
	my $type		= $params{type};
	my $broods		= $params{broods};
	my $hash		= $params{hash};
        my $stmt      = qq/select $procedure($account, $siteId,'$date','$health','$type','$hives','$afb',$pest, '$info','$photo','$broods'::INT[][],'$hash')/;
        my ($sth,@bind)       = $self->{dbh}->prepare($stmt);
        #check to see if we should log
        if ( exists $params{log} && $params{log} == 1 ) {
                print STDERR Dumper($stmt);
                print STDERR Dumper(\@bind);
        }

        $sth->execute;
        return $sth;
	
	



}

sub getSiteHives {
	my ( $self, %params ) = @_;
	my $account	= $params{account};
	my $id		= $params{id};
	my $statement 	= qq/select sh.site as site_id, sh.id as id, bt.id as brood_id, bt.name as brood_name, sh.number as number from sitehives sh  /;
	$statement	.= qq/LEFT JOIN broodtype bt ON ( bt.id = sh.brood_type) where sh.site = $id/;
	
	my ($sth,@bind)       = $self->{dbh}->prepare($statement);
	if ( exists $params{log} && $params{log} == 1 ) {
                print STDERR Dumper($statement);
                print STDERR Dumper(\@bind);
        }
	$sth->execute;
	return $sth;
}


sub updateSite {
	my ( $self, %params ) = @_;
	my $procedure	= 'updateSite';
	my $account	= $params{account};
	my $maf         = $params{maf_ref};
	my $site_id	= $params{site_id};
	my $area        = $params{area};
	my $landowner   = $params{landowner};
	my $name        = $params{name};
	my $desc        = $params{descr};
	my $add1        = $params{address_1};
	my $add2        = $params{address_2};
	my $latlng      = $params{latlng};
	my $broodtype   = $params{broodtypes};

	my $in_string = qq/$account , '$maf' , $site_id, $area , $landowner  ,'$name', '$desc' , '$add1' ,'$add2' , '$latlng' , '$broodtype'::INT[][]/;

	my $stmt      = qq/select $procedure($in_string)/;

        my ($sth,@bind)       = $self->{dbh}->prepare($stmt);
        #check to see if we should log
        if ( exists $params{log} && $params{log} == 1 ) {
                print STDERR Dumper($stmt);
                print STDERR Dumper(\@bind);
        }

        $sth->execute;
        return $sth;




}

sub getSiteInspectionsList {
	my ( $self, $account ) = @_;
	my $procedure   = 'getSiteInspections';
	my $stmt      = qq/select * from $procedure($account) as siteinspectionslist(id integer, site varchar, date date, health varchar, type varchar, afbcheck char, pestcontrol varchar, photo varchar)/;
	
	my ($sth,@bind)       = $self->{dbh}->prepare($stmt);
	# if ( exists $params{log} && $params{log} == 1 ) {
                print STDERR Dumper($stmt);
                print STDERR Dumper(\@bind);
        #}

        $sth->execute;
        return $sth;


}

sub getSiteInspectionDetails {
        my ( $self, %params ) = @_;
        my $procedure   = 'getSiteInspection';
        my $stmt      = qq/select * from $procedure($params{account},$params{inspection_id}) /;

        my ($sth,@bind)       = $self->{dbh}->prepare($stmt);
        # if ( exists $params{log} && $params{log} == 1 ) {
                print STDERR Dumper($stmt);
                print STDERR Dumper(\@bind);
        #}

        $sth->execute;
        return $sth;


}



__PACKAGE__->meta->make_immutable;

1;
