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

begin;

select plan(10);

set search_path to camper, public;

select has_function('camper', 'reference', array['payment']);
select function_lang_is('camper', 'reference', array['payment'], 'sql');
select function_returns('camper', 'reference', array['payment'], 'text');
select isnt_definer('camper', 'reference', array['payment']);
select volatility_is('camper', 'reference', array['payment'], 'stable');
select function_privs_are('camper', 'reference', array['payment'], 'guest', array['EXECUTE']);
select function_privs_are('camper', 'reference', array['payment'], 'employee', array['EXECUTE']);
select function_privs_are('camper', 'reference', array['payment'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'reference', array['payment'], 'authenticator', array[]::text[]);


set client_min_messages to warning;
truncate payment cascade;
truncate campsite_type cascade;
truncate media cascade;
truncate media_content cascade;
truncate company cascade;
reset client_min_messages;

insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, rtc_number, tourist_tax, tourist_tax_max_days, country_code, currency_code, default_lang_tag)
values (2, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 350, 7, 'ES', 'EUR', 'ca')
;

insert into media_content (media_type, bytes)
values ('image/x-xpixmap', 'static char *s[]={"1 1 1 1","a c #ffffff","a"};')
;

insert into media (media_id, company_id, original_filename, content_hash)
values (10, 2, 'cover2.xpm', sha256('static char *s[]={"1 1 1 1","a c #ffffff","a"};'))
;

insert into campsite_type (campsite_type_id, slug, company_id, name, media_id, max_campers, bookable_nights, overflow_allowed)
values (12, 'c1b6f4fc-32c1-4cd5-b796-0c5059152a52', 2, 'Plots', 10, 6, '[1, 7]', true)
;

insert into payment (payment_id, slug, company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, currency_code, zone_preferences)
values (22, '4ef35e2f-ef98-42d6-a724-913bd761ca8c', 2, 12, '2024-08-29', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'EUR', '')
     , (23, '6d1b8e4c-c3c6-4fe4-92c1-2cbf94526693', 2, 12, '2024-08-29', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'EUR', '')
     , (24, '8d38a482-8a25-4d85-9929-e5f425fcac04', 2, 12, '2024-08-29', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'EUR', '')
     , (25, 'b770f8b7-f148-4ab4-a786-aa070af598e5', 2, 12, '2024-08-29', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'EUR', '')
     , (26, '31910d73-d343-44b7-8a29-f7e075b64933', 2, 12, '2024-08-29', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'EUR', '')
     , (27, 'c9488490-ac09-4402-90cd-f6f0546f04c0', 2, 12, '2024-08-29', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'EUR', '')
     , (28, '5819823e-c0ac-4baa-a3ae-515fbb70e909', 2, 12, '2024-08-29', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'EUR', '')
;

select bag_eq(
	$$ select payment_id, payment.reference from payment $$,
	$$ values (22, '000000224ef3')
	        , (23, '000000236d1b')
	        , (24, '000000248d38')
	        , (25, '00000025b770')
	        , (26, '000000263191')
	        , (27, '00000027c948')
	        , (28, '000000285819')
	$$,
	'Should give out the order code for each payment'
);


select *
from finish();

rollback;
