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


set client_min_messages to warning;
truncate booking_campsite cascade;
truncate booking 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 (10, 2, 'Wooden lodge', 6, 7, '[1, 7]')
;

insert into campsite (campsite_id, company_id, label, campsite_type_id)
values (12, 2, 'A', 10)
     , (14, 2, 'B', 10)
     , (16, 2, 'C', 10)
;

insert into booking (booking_id, company_id, campsite_type_id, holder_name, stay, acsi_card, currency_code, zone_preferences, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total)
values (18, 2, 10, 'Holder 2', daterange('2024-01-18', '2024-01-29'), false, 'EUR', '', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
     , (20, 2, 10, 'Holder 4', daterange('2024-01-28', '2024-01-29'), true, 'USD', 'None', 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
;

insert into booking_campsite (booking_id, campsite_id, stay)
values (18, 12, daterange('2024-01-18', '2024-01-29'))
     , (18, 14, daterange('2024-01-18', '2024-01-29'))
     , (20, 16, daterange('2024-01-28', '2024-01-29'))
;

select lives_ok(
	$$ select cancel_booking(18) $$,
	'Should be able to cancel the first booking'
);


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 (18, 2, 10, daterange('2024-01-18', '2024-01-29'), 'Holder 2', null, null, null, null, null, null, 'und', '', 1, 2, 3, 4, 5, 6, 7, 8,  9, 10, 11, false, 'EUR', 'cancelled')
	        , (20, 2, 10, daterange('2024-01-28', '2024-01-29'), 'Holder 4', null, null, null, null, null, null, 'und', 'None', 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, true, 'USD', 'created')
	$$,
	'Should have updated the bookings'
);

select bag_eq (
	$$ select booking_id, campsite_id, stay from booking_campsite $$,
	$$ values (20, 16, daterange('2024-01-28', '2024-01-29'))
	$$ ,
	'Should have deleted the cancelled booking’ campsites'
);

select *
from finish();

rollback;
