-- Test invoice_number_counter
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;

begin;

select plan(28);

set search_path to numerus, auth, public;

select has_table('invoice_number_counter');
select has_pk('invoice_number_counter' );
select col_is_pk('invoice_number_counter', array['company_id', 'year']);

select table_privs_are('invoice_number_counter', 'guest', array []::text[]);
select table_privs_are('invoice_number_counter', 'invoicer', array ['SELECT', 'INSERT', 'UPDATE']);
select table_privs_are('invoice_number_counter', 'admin', array ['SELECT', 'INSERT', 'UPDATE']);
select table_privs_are('invoice_number_counter', 'authenticator', array []::text[]);

select has_column('invoice_number_counter', 'company_id');
select col_is_fk('invoice_number_counter', 'company_id');
select fk_ok('invoice_number_counter', 'company_id', 'company', 'company_id');
select col_type_is('invoice_number_counter', 'company_id', 'integer');
select col_not_null('invoice_number_counter', 'company_id');
select col_hasnt_default('invoice_number_counter', 'company_id');

select has_column('invoice_number_counter', 'year');
select col_type_is('invoice_number_counter', 'year', 'integer');
select col_not_null('invoice_number_counter', 'year');
select col_hasnt_default('invoice_number_counter', 'year');

select has_column('invoice_number_counter', 'currval');
select col_type_is('invoice_number_counter', 'currval', 'integer');
select col_not_null('invoice_number_counter', 'currval');
select col_hasnt_default('invoice_number_counter', 'currval');


set client_min_messages to warning;
truncate invoice_number_counter cascade;
truncate company_user cascade;
truncate payment_method cascade;
truncate company cascade;
truncate auth."user" cascade;
reset client_min_messages;

insert into auth."user" (user_id, email, name, password, role, cookie, cookie_expires_at)
values (1, 'demo@tandem.blog', 'Demo', 'test', 'invoicer', '44facbb30d8a419dfd4bfbc44a4b5539d4970148dfc84bed0e', current_timestamp + interval '1 month')
     , (5, 'admin@tandem.blog', 'Demo', 'test', 'admin', '12af4c88b528c2ad4222e3740496ecbc58e76e26f087657524', current_timestamp + interval '1 month')
;

set constraints "company_default_payment_method_id_fkey" deferred;

insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code, currency_code, default_payment_method_id)
values (2, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR', 222)
     , (4, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', 'FR', 'USD', 444)
;

insert into payment_method (payment_method_id, company_id, name, instructions)
values (444, 4, 'cash', 'cash')
     , (222, 2, 'cash', 'cash')
;

set constraints "company_default_payment_method_id_fkey" immediate;

insert into company_user (company_id, user_id)
values (2, 1)
     , (4, 5)
;

insert into invoice_number_counter (company_id, year, currval)
values (2, 2010, 6)
     , (2, 2011, 8)
     , (4, 2010, 8)
     , (4, 2012, 10)
;


prepare counter_data as
select company_id, year, currval
from invoice_number_counter
;

set role invoicer;
select is_empty('counter_data', 'Should show no data when cookie is not set yet');
reset role;

select set_cookie('44facbb30d8a419dfd4bfbc44a4b5539d4970148dfc84bed0e/demo@tandem.blog');
select bag_eq(
	'counter_data',
	$$ values (2, 2010, 6)
	        , (2, 2011, 8)
	$$,
	'Should only list invoices of the companies where demo@tandem.blog is user of'
);
reset role;

select set_cookie('12af4c88b528c2ad4222e3740496ecbc58e76e26f087657524/admin@tandem.blog');
select bag_eq(
	'counter_data',
	$$ values (4, 2010, 8)
	        , (4, 2012, 10)
	$$,
	'Should only list invoices of the companies where admin@tandem.blog is user of'
);
reset role;

select set_cookie('not-a-cookie');
select throws_ok(
	'counter_data',
	'42501', 'permission denied for table invoice_number_counter',
	'Should not allow select to guest users'
);
reset role;


select lives_ok( $$
	insert into invoice_number_counter (company_id, year, currval)
	values (2, 2009, 0)
	$$,
	'Should allow starting a counter from zero'
);

select throws_ok( $$
	insert into invoice_number_counter (company_id, year, currval)
	values (2, 2008, -1)
	$$,
       '23514', 'new row for relation "invoice_number_counter" violates check constraint "counter_zero_or_positive"',
	'Should not allow starting a counter from a negative value'
);

select throws_ok( $$
	insert into invoice_number_counter (company_id, year, currval)
	values (2, -2008, 1)
	$$,
       '23514', 'new row for relation "invoice_number_counter" violates check constraint "year_always_positive"',
	'Should not allow counters for invoices issued before Jesus Christ was born'
);


select *
from finish();

rollback;