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

begin;

select plan(12);

set search_path to numerus, public;

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


set client_min_messages to warning;
truncate collection_attachment cascade;
truncate collection cascade;
truncate payment_account 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')
;

set constraints "company_default_payment_method_id_fkey" immediate;

insert into payment_account (payment_account_id, company_id, payment_account_type, name)
values (11, 1, 'cash', 'Cash 1')
     , (12, 1, 'cash', 'Cash 2')
     , (13, 1, 'other', 'Other')
;

insert into collection (collection_id, company_id, slug, description, collection_date, payment_account_id, amount, currency_code, payment_status)
values (16, 1, '7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'Collection 1', '2023-05-04', 12, 111, 'EUR', 'complete')
     , (17, 1, 'b57b980b-247b-4be4-a0b7-03a7819c53ae', 'Collection 2', '2023-05-05', 13, 100, 'EUR', 'partial')
;

insert into collection_attachment (collection_id, original_filename, mime_type, content)
values (17, 'something.txt', 'text/plain', convert_to('Once upon a time…', 'UTF-8'))
;

select lives_ok(
	$$ select attach_to_collection('7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'collection.txt', 'text/plain', convert_to('To receive 42 €', 'UTF-8')) $$,
	'Should be able to attach a document to the first collection'
);

select lives_ok(
	$$ select attach_to_collection('b57b980b-247b-4be4-a0b7-03a7819c53ae', 'collection.html', 'text/html', convert_to('<html><p>To receive 42 €</p></html>', 'UTF-8')) $$,
	'Should be able to replate the second collection’s attachment with a new document'
);

select bag_eq(
	$$ select collection_id, original_filename, mime_type, convert_from(content, 'UTF-8') from collection_attachment $$,
	$$ values (16, 'collection.txt',  'text/plain', 'To receive 42 €')
	        , (17, 'collection.html', 'text/html',  '<html><p>To receive 42 €</p></html>')
	$$,
	'Should have attached all documents'
);

select *
from finish();

rollback;
