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

begin;

set search_path to auth, numerus, public;

select plan(12);

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


set client_min_messages to warning;
truncate invoice_attachment cascade;
truncate invoice cascade;
truncate contact_tax_details cascade;
truncate contact cascade;
truncate tax cascade;
truncate tax_class 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 tax_class (tax_class_id, company_id, name)
values (11, 1, 'tax')
;

insert into tax (tax_id, company_id, tax_class_id, name, rate)
values (3, 1, 11, 'IRPF -15 %', -0.15)
     , (4, 1, 11, 'IVA 21 %', 0.21)
;

insert into contact (contact_id, company_id, name)
values (12, 1, 'Contact 2.1')
     , (13, 1, 'Contact 2.2')
;

insert into contact_tax_details (contact_id, business_name, vatin, address, city, province, postal_code, country_code)
values (12, 'Customer 2.1 Ltd', 'XXX123', 'Fake St.', 'City', 'Province', '17480', 'ES')
     , (13, 'Customer 2.2 Ltd', 'XXX234', 'Fake St.', 'City', 'Province', '17480', 'ES')
;

insert into invoice (invoice_id, company_id, slug, invoice_number, invoice_date, contact_id, payment_method_id, currency_code, tags)
values (15, 1, '7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'INV1', '2023-05-04', 12, 111, 'EUR', '{tag1}')
     , (16, 1, 'b57b980b-247b-4be4-a0b7-03a7819c53ae', 'INV2', '2023-05-05', 13, 112, 'EUR', '{tag2}')
;

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

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

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

select bag_eq(
	$$ select invoice_id, original_filename, mime_type, content from invoice_attachment $$,
	$$ values (15, 'invoice.txt',  'text/plain', convert_to('Total 42€', 'UTF-8'))
	        , (16, 'invoice.html', 'text/html',  convert_to('<html><p>Total 42€</p></html>', 'UTF-8'))
	$$,
	'Should have attached all documents'
);

select *
from finish();

rollback;
