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

begin;

select plan(17);

set search_path to numerus, auth, public;

select has_function('numerus', 'tag_contact', array ['integer', 'integer', 'tag_name[]']);
select function_lang_is('numerus', 'tag_contact', array ['integer', 'integer', 'tag_name[]'], 'sql');
select function_returns('numerus', 'tag_contact', array ['integer', 'integer', 'tag_name[]'], 'void');
select isnt_definer('numerus', 'tag_contact', array ['integer', 'integer', 'tag_name[]']);
select volatility_is('numerus', 'tag_contact', array ['integer', 'integer', 'tag_name[]'], 'volatile');
select function_privs_are('numerus', 'tag_contact', array ['integer', 'integer', 'tag_name[]'], 'guest', array []::text[]);
select function_privs_are('numerus', 'tag_contact', array ['integer', 'integer', 'tag_name[]'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'tag_contact', array ['integer', 'integer', 'tag_name[]'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'tag_contact', array ['integer', 'integer', 'tag_name[]'], 'authenticator', array []::text[]);


set client_min_messages to warning;
truncate contact_tag cascade;
truncate tag 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)
;

insert into payment_method (payment_method_id, company_id, name, instructions)
values (111, 1, 'cash', 'cash')
     , (112, 1, 'bank', 'send money to my bank account')
;

set constraints "company_default_payment_method_id_fkey" immediate;

insert into tag (tag_id, company_id, name)
values (10, 1, 'tag1')
     , (11, 1, 'tag2')
;
-- tag_contact uses the sequence and sometimes it would confict
alter sequence tag_tag_id_seq restart with 15;

insert into contact (contact_id, company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code)
values (12, 1, 'Contact 2.1', 'XX555', '', '777-777-777', 'c@c', '', '', '', '', '', 'ES')
     , (13, 1, 'Contact 2.2', 'XX666', '', '888-888-888', 'd@d', '', '', '', '', '', 'ES')
;

insert into contact_tag (contact_id, tag_id)
values (12, 10)
     , (13, 11)
;

prepare current_tags as
select contact_id, tag.name
from contact
join contact_tag using (contact_id)
join tag using (tag_id);

select lives_ok(
	$$ select tag_contact(1, 12, array['tag1']) $$,
	'Should be able to keep the same tags to the contact'
);

select bag_eq(
	'current_tags',
	$$ values (12, 'tag1')
	        , (13, 'tag2')
	$$,
	'Should not have changed any contact tag'
);

select lives_ok(
	$$ select tag_contact(1, 12, array['tag1', 'tag2']) $$,
	'Should be able to add tag2 contact'
);

select bag_eq(
	'current_tags',
	$$ values (12, 'tag1')
	        , (12, 'tag2')
	        , (13, 'tag2')
	$$,
	'Should have added tag2 to contact'
);

select lives_ok(
	$$ select tag_contact(1, 13, array['tag3']) $$,
	'Should be able to replace all tags of an contact with a new one'
);

select bag_eq(
	'current_tags',
	$$ values (12, 'tag1')
	        , (12, 'tag2')
	        , (13, 'tag3')
	$$,
	'Should have set tag3 to contact'
);

select lives_ok(
	$$ select tag_contact(1, 12, array[]::tag_name[]) $$,
	'Should be able to remove all tags from an contact'
);

select bag_eq(
	'current_tags',
	$$ values (13, 'tag3')
	$$,
	'Should have remove all tags from contact'
);


select *
from finish();

rollback;
