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

begin;

select plan(12);

set search_path to camper, public;

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


set client_min_messages to warning;
truncate booking_option cascade;
truncate booking cascade;
truncate payment_option 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')
;

insert into payment_option (payment_id, campsite_type_option_id, units, subtotal)
values (28, 18, 1, 1500)
;

insert into booking (booking_id, slug, company_id, campsite_type_id, stay, holder_name, address, postal_code, city, country_code, email, phone, lang_tag, zone_preferences, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, acsi_card, currency_code, booking_status)
values (30, 'e3c478f1-8895-4cc6-b644-b19b5a553bb9', 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', 'confirmed');

insert into booking_option (booking_id, campsite_type_option_id, units, subtotal)
values (30, 22, 2, 111)
     , (30, 24, 3, 2222)
;

select lives_ok(
	$$ select edit_booking_from_payment('e3c478f1-8895-4cc6-b644-b19b5a553bb9', '4ef35e2f-ef98-42d6-a724-913bd761ca8c') $$,
	'Should be able to update a booking from a payment'
);

select bag_eq(
	$$ select booking_id, 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 (30, 2, 12, daterange('2024-08-28', '2024-09-04'), 'First', 'Fake St., 123', '17800', 'Girona', 'ES', 'customer@example.com', '+34 977 97 79 77', 'ca', 'pref I before E', 3200, 2, 10420, 4, 20840, 6, 25080, 3, 2450, 4900, 79160, true, 'EUR', 'confirmed')
	$$,
	'Should have updated the booking'
);

select bag_eq (
	$$ select booking_id, campsite_type_option_id, units, subtotal from booking_option $$,
	$$ values (30, 18, 1, 1500)
	$$ ,
	'Should have updated the booking options too'
);

select *
from finish();

rollback;
