package Test::API::VendorAdmin;

use Test::More;
use Try::Tiny;
use base 'Test::Class';

#INIT { Test::Class->runtests }

# this class uses a database connection
use ECS::SQL;
use JSON;
our $ID_base = 5555;

my $p = {};
$p->{ DBH } = ECS::SQL::connect;

my @attrs = qw/method name description/;

my $test_vals = {
        taxMethodID => $ID_base + $$,
        name => "TEST $$",
        description => "Test Test Test ($$)",
        method => [{ percent => "" }],
};

my $new_vals = {
        name => "TEST CHANGE $$",
        description => "Test Test Test Change ($$)",
        method => [{ plus => "10" },{ percent => "" }],
};


my $non_available = "#XXX_NOT_HERE";
my $non_available_id = $ID_base - 1;

sub class{ 'API::VendorAdmin' }


sub testing : Tests {
        my $test = shift;
        my $class = $test->class;

        use_ok $test->class;
        can_ok $class, 'new';
				
				my $dbh = ECS::SQL::toDBI( $p );
        ok my $apiObj = $class->new( { 'dbh'  => $dbh } ), 'the constructor is working';
        isa_ok $apiObj, $class;
        isa_ok $apiObj, 'API::Controller';

				can_ok $apiObj =>  'editTaxMethod';
				can_ok $apiObj => 'listTaxMethods'; 
				can_ok $apiObj => 'updateTaxMethod';
				can_ok $apiObj =>  'deleteTaxMethod'; 
				can_ok $apiObj => 'addTaxMethod'; 
			
				my $newTaxMethodID;

				# test adding a tax method
				{
					diag "Invalid new method";
					local $test_vals->{method} = 'INVALID JSON';
					my $apiObj = $class->new({ dbh => $dbh });
					my $result = $apiObj->addTaxMethod({ method => JSON::to_json($test_vals) });
					ok !$result->{success} && $result->{error}, q{can't add a method with invalid JSON};
				}
				{
					diag "add new tax method";
					my $apiObj = $class->new({ dbh => $dbh });
					my $result = $apiObj->addTaxMethod({ method => JSON::to_json($test_vals) });
					ok $result->{success} && !$result->{error}, q{can add a new tax method};
					ok my $data  = $result->{data}{data}, q{method returns data};
					ok $data->{selectedID}, q{method returns new tax method ID};
					ok $data->{taxMethods}, q{method returns tax method list};
					ok scalar(grep { $data->{selectedID} == $_->{ taxMethodID } } @{ $data->{taxMethods} }), q{method returns new record};
					my ($tm) = grep { $data->{selectedID} == $_->{ taxMethodID } } @{ $data->{taxMethods} };
					if ($tm) {
						local $test_vals->{method} = JSON::to_json( $test_vals->{method} );
						ok $tm->{ $_ } eq $test_vals->{ $_ } => qq{update successfully added field '$_'}  for @attrs;
					}
					$test_vals->{taxMethodID} = $data->{selectedID};
					$new_vals->{taxMethodID} = $data->{selectedID};
				}
				{
					last unless $test_vals->{taxMethodID};
					diag "update tax methods";
					my $apiObj = $class->new({ dbh => $dbh});
					my $result = $apiObj->updateTaxMethod({ taxMethodID => $test_vals->{taxMethodID}, method => JSON::to_json( $new_vals ) });
					ok my $data  = $result->{data}{data}, q{method returns data};
					ok $data->{selectedID}, q{method returns updated tax method ID};
					ok $data->{taxMethods}, q{method returns tax method list};
					ok scalar(grep { $data->{selectedID} == $_->{ taxMethodID } } @{ $data->{taxMethods} }), q{list returns updated record};
					my ($tm) = grep { $data->{selectedID} == $_->{ taxMethodID } } @{ $data->{taxMethods} };
					if ($tm) {
						local $new_vals->{method} = JSON::to_json( $new_vals->{method} );
						ok $tm->{ $_ } eq $new_vals->{ $_ } => qq{update successfully updated field '$_'}  for @attrs;
					}
			}
			{
					last unless $test_vals->{taxMethodID};
					diag "list tax methods";
					my $apiObj = $class->new({ dbh => $dbh});
					my $result = $apiObj->listTaxMethods;
					ok my $data  = $result->{data}{data}, q{method returns data};
					ok $data->{taxMethods}, q{method returns tax method list};
					ok scalar(grep { $test_vals->{taxMethodID} == $_->{ taxMethodID } } @{ $data->{taxMethods} }), q{list returns updated record};
					my ($tm) = grep { $test_vals->{taxMethodID} == $_->{ taxMethodID } } @{ $data->{taxMethods} };
					if ($tm) {
						local $new_vals->{method} = JSON::to_json( $new_vals->{method} );
						ok $tm->{ $_ } eq $new_vals->{ $_ } => qq{list returns updated field '$_'}  for @attrs;
					}
			}
			{
					last unless $test_vals->{taxMethodID};
					diag "delete tax methods";
					my $apiObj = $class->new({ dbh => $dbh});
					my $result = $apiObj->deleteTaxMethod({ taxMethodID => $test_vals->{taxMethodID} });
					ok my $data  = $result->{data}{data}, q{method returns data};
					ok $data->{taxMethods}, q{method returns tax method list};
					ok !(scalar(grep { $test_vals->{taxMethodID} == $_->{ taxMethodID } } @{ $data->{taxMethods} })), q{list does not contain deleted record};
					my ($tm) = grep { $test_vals->{taxMethodID} == $_->{ taxMethodID } } @{ $data->{taxMethods} };
			}
			
					
}
