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

begin;

select plan(13);

set search_path to camper, public;

select has_function('camper', 'check_in_guests', array['integer', 'checked_in_guest[]']);
select function_lang_is('camper', 'check_in_guests', array['integer', 'checked_in_guest[]'], 'sql');
select function_returns('camper', 'check_in_guests', array['integer', 'checked_in_guest[]'], 'void');
select isnt_definer('camper', 'check_in_guests', array['integer', 'checked_in_guest[]']);
select volatility_is('camper', 'check_in_guests', array['integer', 'checked_in_guest[]'], 'volatile');
select function_privs_are('camper', 'check_in_guests', array ['integer', 'checked_in_guest[]'], 'guest', array[]::text[]);
select function_privs_are('camper', 'check_in_guests', array ['integer', 'checked_in_guest[]'], 'employee', array['EXECUTE']);
select function_privs_are('camper', 'check_in_guests', array ['integer', 'checked_in_guest[]'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'check_in_guests', array ['integer', 'checked_in_guest[]'], 'authenticator', array[]::text[]);


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

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, booking_status)
values (14, 2, 12, 'Holder 2', daterange('2024-01-18', '2024-01-29'), false, 'EUR', '', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'confirmed')
     , (16, 2, 12, 'Holder 4', daterange('2024-01-28', '2024-01-29'), true, 'USD', 'None', 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 'invoiced')
;

insert into booking_guest (booking_id, id_document_type_id, id_document_number, id_document_issue_date, given_name, first_surname, second_surname, sex_id, birthdate, country_code, phone, address, created_at, updated_at)
values (14, 'D', '4444444A', '2022-01-01', 'Given', 'First', 'Last', 'F', '1991-07-02', 'ES', '+32 977 977 977', 'Fake St., 123', '2023-03-03 03:03:03', '2023-03-03 03:03:03')
     , (16, 'P', '123456-ABC', '2020-12-20', 'Mah', 'Name', '', 'M', '1980-03-31', 'JP', '123 123 123', 'Dunno', '2022-02-02 02:02:02', '2022-02-02 02:02:02')
;

select lives_ok(
	$$ select check_in_guests(14, array[('C', 'UABCA', null, 'Jan-Michael', 'Vincent', '', 'M', '1944-07-15', 'US', '5673159097', 'One Rabbit Road'), ('D', '4444444A', '2021-02-02', 'Gibbon', 'Di', 'Luffy', 'M', '1981-09-25', 'GB', null, '')]::checked_in_guest[]) $$,
	'Should be able to check-in guests for the first booking'
);

select lives_ok(
	$$ select check_in_guests(16, array[('I', 'Huh?', '1999-09-09', 'Mayumi', 'Tanaka', '', 'F', '1955-01-15', 'JP', '5083262771', 'Somewhere')]::checked_in_guest[]) $$,
	'Should be able to check-in guests for the second booking'
);

select bag_eq (
	$$ select booking_id, booking_status from booking $$,
	$$ values (14, 'checked-in')
	        , (16, 'invoiced')
	$$,
	'Should have updated the status if it was created or confirmed'
);

select bag_eq (
	$$ select booking_id, id_document_type_id::text, id_document_number, id_document_issue_date::text, given_name, first_surname, second_surname, sex_id::text, birthdate::text, country_code::text, phone::text, address, created_at, updated_at from booking_guest $$,
	$$ values (14, 'C', 'UABCA', null, 'Jan-Michael', 'Vincent', '', 'M', '1944-07-15', 'US', '+1 567-315-9097', 'One Rabbit Road', current_timestamp, current_timestamp)
	        , (14, 'D', '4444444A', '2021-02-02', 'Gibbon', 'Di', 'Luffy', 'M', '1981-09-25', 'GB', null, '', '2023-03-03 03:03:03', current_timestamp)
	        , (16, 'I', 'Huh?', '1999-09-09', 'Mayumi', 'Tanaka', '', 'F', '1955-01-15', 'JP', '+81 50-8326-2771', 'Somewhere', current_timestamp, current_timestamp)
	$$,
	'Should have updated guest information'
);


select *
from finish();

rollback;
