-- Test add_booking_from_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', 'add_booking_from_payment', array['uuid']);
select function_lang_is('camper', 'add_booking_from_payment', array['uuid'], 'plpgsql');
select function_returns('camper', 'add_booking_from_payment', array['uuid'], 'integer');
select isnt_definer('camper', 'add_booking_from_payment', array['uuid']);
select volatility_is('camper', 'add_booking_from_payment', array['uuid'], 'volatile');
select function_privs_are('camper', 'add_booking_from_payment', array ['uuid'], 'guest', array[]::text[]);
select function_privs_are('camper', 'add_booking_from_payment', array ['uuid'], 'employee', array['EXECUTE']);
select function_privs_are('camper', 'add_booking_from_payment', array ['uuid'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'add_booking_from_payment', array ['uuid'], 'authenticator', array[]::text[]);


set client_min_messages to warning;
truncate booking_option cascade;
truncate booking cascade;
truncate payment_option cascade;
truncate payment_customer cascade;
truncate payment cascade;
truncate campsite_type_option 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', '', '', '', '', '', '', 60, 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 (6, 2, 'cover2.xpm', sha256('static char *s[]={"1 1 1 1","a c #ffffff","a"};'))
;

insert into campsite_type (campsite_type_id, company_id, name, media_id, max_campers, bookable_nights)
values (12, 2, 'Wooden lodge', 6, 7, '[1, 7]')
     , (14, 2, 'Bungalow', 6, 7, '[1, 7]')
     , (16, 2, 'Plot', 6, 7, '[1, 7]')
;

insert into campsite_type_option (campsite_type_option_id, campsite_type_id, name, range, per_night)
values (18, 12, 'Big tent', '[0, 4)', true)
     , (20, 12, 'Small tent', '[0, 4)', true)
     , (22, 14, 'Electricity', '[0, 5)', false)
     , (24, 14, 'Car', '[0, 4)', true)
     , (26, 16, 'Autocaravan', '[0, 4)', 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)
values (28, '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')
     , (30, '6d1b8e4c-c3c6-4fe4-92c1-2cbf94526693', 2, 14, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'draft')
     , (32, '7cae7d1c-d626-41e0-b1c5-48359e515579', 2, 16, '2024-08-30', '2024-09-02', 74000, 3, 122, 1, 333, 2, 444, 3, 555, 666, 777, 'USD', 'None', false, 'preauth')
;

insert into payment_customer (payment_id, full_name, address, postal_code, city, country_code, email, phone, lang_tag)
values (30, 'First', 'Fake St., 123', '17800', 'Girona', 'ES', 'customer@example.com', '+34 977 977 977', 'ca')
     , (32, 'Second', 'None', '555', 'Paris', 'FR', 'customer@example.fr', '+33 123 123 123', 'fr')
;

insert into payment_option (payment_id, campsite_type_option_id, units, subtotal)
values (28, 18, 1, 1500)
     , (30, 22, 2, 111)
     , (30, 24, 3, 2222)
;

select lives_ok(
	$$ select add_booking_from_payment('4ef35e2f-ef98-42d6-a724-913bd761ca8c') $$,
	'Should be able to create a booking from a payment without customer data'
);

select lives_ok(
	$$ select add_booking_from_payment('6d1b8e4c-c3c6-4fe4-92c1-2cbf94526693') $$,
	'Should be able to create a booking from a payment with all data'
);

select lives_ok(
	$$ select add_booking_from_payment('7cae7d1c-d626-41e0-b1c5-48359e515579') $$,
	'Should be able to create a booking from a payment without options'
);

select throws_ok(
	$$ select add_booking_from_payment('ef603c62-cf35-4192-8642-2ab6bd71dc26') $$,
	'22023', 'ef603c62-cf35-4192-8642-2ab6bd71dc26 is not a valid payment.',
	'Should not be possible to create a booking from a non-existing payment'
);

select bag_eq(
	$$ select company_id, campsite_type_id, stay, holder_name, address, postal_code, city, country_code::text, email::text, phone::text, lang_tag, zone_preferences, subtotal_nights::integer, number_adults::integer, subtotal_adults::integer, number_teenagers::integer, subtotal_teenagers::integer, number_children::integer, subtotal_children::integer, number_dogs::integer, subtotal_dogs::integer, subtotal_tourist_tax::integer, total::integer, acsi_card, currency_code::text, booking_status from booking $$,
	$$ values (2, 12, daterange('2024-08-28', '2024-09-04'), 'Unknown', null, null, null, null, null, null, 'und', 'pref I before E', 3200, 2, 10420, 4, 20840, 6, 25080, 3, 2450, 4900, 79160, true, 'EUR', 'created')
	        , (2, 14, daterange('2024-08-29', '2024-09-03'), 'First', 'Fake St., 123', '17800', 'Girona', 'ES', 'customer@example.com', '+34 977 97 79 77', 'ca', '', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, false, 'EUR', 'created')
	        , (2, 16, daterange('2024-08-30', '2024-09-02'), 'Second', 'None', '555', 'Paris', 'FR', 'customer@example.fr', '+33 1 23 12 31 23', 'fr', 'None', 74000, 3, 122, 1, 333, 2, 444, 3, 555, 666, 777, false, 'USD', 'created')
	$$,
	'Should have created the new bookings'
);

select bag_eq (
	$$ select campsite_type_id, campsite_type_option_id, units, subtotal from booking_option join booking using (booking_id) $$,
	$$ values (12, 18, 1, 1500)
	        , (14, 22, 2, 111)
	        , (14, 24, 3, 2222)
	$$ ,
	'Should have added the booking options too'
);

select *
from finish();

rollback;
