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

begin;

select plan(15);

set search_path to camper, public;

select has_function('camper', 'ready_payment', array['uuid', 'text', 'text', 'text', 'text', 'country_code', 'email', 'text', 'text']);
select function_lang_is('camper', 'ready_payment', array['uuid', 'text', 'text', 'text', 'text', 'country_code', 'email', 'text', 'text'], 'plpgsql');
select function_returns('camper', 'ready_payment', array['uuid', 'text', 'text', 'text', 'text', 'country_code', 'email', 'text', 'text'], 'integer');
select isnt_definer('camper', 'ready_payment', array['uuid', 'text', 'text', 'text', 'text', 'country_code', 'email', 'text', 'text']);
select volatility_is('camper', 'ready_payment', array['uuid', 'text', 'text', 'text', 'text', 'country_code', 'email', 'text', 'text'], 'volatile');
select function_privs_are('camper', 'ready_payment', array ['uuid', 'text', 'text', 'text', 'text', 'country_code', 'email', 'text', 'text'], 'guest', array['EXECUTE']);
select function_privs_are('camper', 'ready_payment', array ['uuid', 'text', 'text', 'text', 'text', 'country_code', 'email', 'text', 'text'], 'employee', array['EXECUTE']);
select function_privs_are('camper', 'ready_payment', array ['uuid', 'text', 'text', 'text', 'text', 'country_code', 'email', 'text', 'text'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'ready_payment', array ['uuid', 'text', 'text', 'text', 'text', 'country_code', 'email', 'text', 'text'], 'authenticator', array[]::text[]);


set client_min_messages to warning;
truncate payment_customer cascade;
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, acsi_card, payment_status, created_at, updated_at)
values (22, '4ef35e2f-ef98-42d6-a724-913bd761ca8c', 2, 12, '2024-08-28', '2024-09-04', 3200, 2, 10420, 4, 20840, 6, 25080, 3, 2450, 4900, 79160, 'EUR', 'pref I before E', true, 'draft', '2024-01-01 01:01:01', '2024-01-01')
     , (24, '6d1b8e4c-c3c6-4fe4-92c1-2cbf94526693', 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'draft', current_timestamp, current_timestamp)
;

insert into payment_customer (payment_id, full_name, address, postal_code, city, country_code, email, phone, lang_tag)
values (24, '', '', '', '', 'FR', 'a@a.com', '555-555-555', 'es')
;

select is(
	ready_payment('4ef35e2f-ef98-42d6-a724-913bd761ca8c', 'Juli Verd', 'C/ Fals, 123', '17486', 'Castelló d’Empúries', 'ES', 'juli@verd.cat', '972486  160', 'ca'),
	22,
	'Should be able to ready the first draft payment'
);

select is(
	ready_payment('6d1b8e4c-c3c6-4fe4-92c1-2cbf94526693', 'Pere Gil', 'Gutenbergstr. 9-13', '82178', 'Puchheim', 'DE', 'pere@gil.de', '8980902-0', 'en'),
	24,
	'Should be able to ready the second draft payment, and update customer details'
);

select throws_ok(
	$$ select ready_payment('7ba7f0a5-d73d-4d6e-a9c4-3b88f2b2a357', 'Juli Verd', 'C/ Fals, 123', '17486', 'Castelló d’Empúries', 'ES', 'juli@verd.cat', '972 486 160', 'ca') $$,
	'23514', 'insert or update on table "payment" violates check constraint "payment_is_draft"',
	'Should not be able to ready an inexistent payment'
);

select throws_ok(
	$$ select ready_payment('4ef35e2f-ef98-42d6-a724-913bd761ca8c', 'Juli Verd', 'C/ Fals, 123', '17486', 'Castelló d’Empúries', 'ES', 'juli@verd.cat', '972 486 160', 'ca') $$,
	'23514', 'insert or update on table "payment" violates check constraint "payment_is_draft"',
	'Should not be able to ready an already processed payment'
);

select bag_eq(
	$$ select payment_id, slug::text, company_id, campsite_type_id, arrival_date::text, departure_date::text, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences, acsi_card, payment_status, created_at, updated_at from payment $$,
	$$ values (22, '4ef35e2f-ef98-42d6-a724-913bd761ca8c', 2, 12, '2024-08-28', '2024-09-04', 3200, 2, 10420, 4, 20840, 6, 25080, 3, 2450, 4900, 79160, 'pref I before E', true, 'pending', '2024-01-01 01:01:01', current_timestamp)
	        , (24, '6d1b8e4c-c3c6-4fe4-92c1-2cbf94526693', 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, '', false, 'pending', current_timestamp, current_timestamp)
	$$,
	'Should have updated payments'
);

select bag_eq(
	$$ select payment_id, full_name, address, postal_code, city, country_code::text, email::text, phone::text, lang_tag from payment_customer $$,
	$$ values (22, 'Juli Verd', 'C/ Fals, 123', '17486', 'Castelló d’Empúries', 'ES', 'juli@verd.cat', '+34 972 48 61 60', 'ca')
	        , (24, 'Pere Gil', 'Gutenbergstr. 9-13', '82178', 'Puchheim', 'DE', 'pere@gil.de', '+49 89 809020', 'en')
	$$,
	'Should have added and updated customer details'
);


select *
from finish();

rollback;
