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

begin;

select plan(20);

set search_path to auth, numerus, public;

select has_function('numerus', 'add_contact', array ['integer', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]']);
select function_lang_is('numerus', 'add_contact', array ['integer', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'plpgsql');
select function_returns('numerus', 'add_contact', array ['integer', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'uuid');
select isnt_definer('numerus', 'add_contact', array ['integer', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]']);
select volatility_is('numerus', 'add_contact', array ['integer', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'volatile');
select function_privs_are('numerus', 'add_contact', array ['integer', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'guest', array []::text[]);
select function_privs_are('numerus', 'add_contact', array ['integer', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'add_contact', array ['integer', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'add_contact', array ['integer', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'authenticator', array []::text[]);


set client_min_messages to warning;
truncate contact_swift cascade;
truncate contact_iban cascade;
truncate contact_web cascade;
truncate contact_email cascade;
truncate contact_phone cascade;
truncate contact_tax_details cascade;
truncate contact cascade;
truncate payment_method cascade;
truncate company cascade;
reset client_min_messages;

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 (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR', 111)
     , (2, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', 'FR', 'USD', 222)
;

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

set constraints "company_default_payment_method_id_fkey" immediate;

select lives_ok(
	$$ select add_contact(1, 'Contact 2.1', '777-777-777', '', 'https://c', null, 'NL35INGB5262865534', '', '{tag1,tag2}') $$,
	'Should be able to insert a contact for the first company with two tags, no email, and no tax details'
);

select lives_ok(
	$$ select add_contact(1, 'Contact 2.2', '', 'd@d', '', '(Contact 2.2 Ltd,41414141L,"Fake St., 123",City 2.2,Province 2.2,17487,ES)', '', 'HSBCSKBA', '{}') $$,
	'Should be able to insert a second contact for the first company with no tag, no phone, and not website'
);

select lives_ok(
	$$ select add_contact(2, 'Contact 4.1', '999-999-999', 'e@e', 'http://e', '(Contact 4.1 Ltd,42424242Y,"Another Fake St., 123",City 4.1,Province 4.1,17488,ES)', 'MT47JQRS54557143744629565915326', 'ARBNNL22', '{tag2}') $$,
	'Should be able to insert a contact for the second company with a tag and everything else'
);

select lives_ok(
	$$ select add_contact(1, 'Contact 2.3', '', '', '', null, '', '', '{tag2}') $$,
	'Should be able to insert another contact with a repeated tag'
);

select bag_eq(
	$$ select company_id, name, tags, created_at from contact $$,
	$$ values (1, 'Contact 2.1', '{tag1,tag2}'::tag_name[], CURRENT_TIMESTAMP)
	        , (1, 'Contact 2.2', '{}'::tag_name[],          CURRENT_TIMESTAMP)
	        , (2, 'Contact 4.1', '{tag2}'::tag_name[],      CURRENT_TIMESTAMP)
	        , (1, 'Contact 2.3', '{tag2}'::tag_name[],      CURRENT_TIMESTAMP)
	$$,
	'Should have created all contacts'
);

select bag_eq(
	$$ select name, business_name, vatin::text, address, city, province, postal_code, country_code::text from contact join contact_tax_details using (contact_id) $$,
	$$ values ('Contact 2.2', 'Contact 2.2 Ltd', 'ES41414141L', 'Fake St., 123',         'City 2.2', 'Province 2.2', '17487', 'ES')
	        , ('Contact 4.1', 'Contact 4.1 Ltd', 'ES42424242Y', 'Another Fake St., 123', 'City 4.1', 'Province 4.1', '17488', 'ES')
	$$,
	'Should have created all contacts’ tax details'
);

select bag_eq(
	$$ select name, phone::text from contact join contact_phone using (contact_id) $$,
	$$ values ('Contact 2.1', '+34 777 77 77 77')
	        , ('Contact 4.1', '+34 999 99 99 99')
	$$,
	'Should have created all contacts’ phone'
);

select bag_eq(
	$$ select name, email::text from contact join contact_email using (contact_id) $$,
	$$ values ('Contact 2.2', 'd@d')
	        , ('Contact 4.1', 'e@e')
	$$,
	'Should have created all contacts’ email'
);

select bag_eq(
	$$ select name, uri::text from contact join contact_web using (contact_id) $$,
	$$ values ('Contact 2.1', 'https://c')
	        , ('Contact 4.1', 'http://e')
	$$,
	'Should have created all contacts’ web'
);

select bag_eq(
	$$ select name, iban::text from contact join contact_iban using (contact_id) $$,
	$$ values ('Contact 2.1', 'NL35INGB5262865534')
	        , ('Contact 4.1', 'MT47JQRS54557143744629565915326')
	$$,
	'Should have created all contacts’ IBAN'
);

select bag_eq(
	$$ select name, bic::text from contact join contact_swift using (contact_id) $$,
	$$ values ('Contact 2.2', 'HSBCSKBA')
	        , ('Contact 4.1', 'ARBNNL22')
	$$,
	'Should have created all contacts’ BIC'
);


select *
from finish();

rollback;
