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


set client_min_messages to warning;
truncate payment_option cascade;
truncate payment cascade;
truncate campsite_type_option_cost cascade;
truncate campsite_type_option cascade;
truncate campsite_type_pet_cost cascade;
truncate campsite_type_cost cascade;
truncate campsite_type cascade;
truncate media cascade;
truncate media_content cascade;
truncate season_calendar cascade;
truncate season 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, country_code, currency_code, default_lang_tag)
values (2, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 350, 'ES', 'EUR', 'ca')
;

insert into season (season_id, company_id, name)
values (4, 2, 'High')
     , (6, 2, 'Shoulder')
     , (8, 2, 'Offseason')
;

insert into season_calendar (season_id, season_range)
values (4, '[2024-07-01,2024-08-30)')
     , (6, '[2024-08-30,2024-09-03)')
     , (8, '[2024-09-03,2024-09-08)')
;

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)
     , (14, 'b065f4e3-2cc8-491d-a413-d015d7d00183', 2, 'Bungalow', 10, 6, '[1, 7]', false)
;

insert into campsite_type_cost (campsite_type_id, season_id, cost_per_night, cost_per_adult, cost_per_teenager, cost_per_child)
values (12, 4,   400, 795, 795, 640)
     , (12, 6,   200, 740, 740, 590)
     , (12, 8,     0, 660, 660, 540)
     , (14, 4, 17000,   0,   0,   0)
     , (14, 6, 13500,   0,   0,   0)
     , (14, 8, 10500,   0,   0,   0)
;

insert into campsite_type_pet_cost (campsite_type_id, cost_per_night)
values (12, 350)
;

insert into campsite_type_option (campsite_type_option_id, campsite_type_id, name, range, per_night)
values (16, 12, 'Big tent', '[0, 4)', true)
     , (18, 12, 'Car', '[0, 4)', true)
     , (20, 12, 'Electricity', '[0, 5)', false)
;

insert into campsite_type_option_cost (campsite_type_option_id, season_id, cost)
values (16, 4, 800)
     , (16, 6, 720)
     , (16, 8, 620)
     , (18, 4, 700)
     , (18, 6, 630)
     , (18, 8, 530)
     , (20, 4, 690)
     , (20, 6, 610)
     , (20, 8, 590)
;

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, zone_preferences, created_at, updated_at)
values (22, '7cccfe16-695e-486d-a6a5-1162fb85cafb', 2, 12, '2024-08-30', '2024-09-01', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, '', '2024-01-01 01:01:01', '2024-01-01 01:01:01')
;

insert into payment_option (payment_id, campsite_type_option_id, units, subtotal)
values (22, 16, 1, 0)
     , (22, 18, 1, 0)
;

select lives_ok(
	$$ select draft_payment(null, '2024-08-29', '2024-09-03', 'b065f4e3-2cc8-491d-a413-d015d7d00183', 1, 2, 3, 0, null, null) $$,
	'Should be able to create a new draft for Bungalows'
);

select lives_ok(
	$$ select draft_payment('7cccfe16-695e-486d-a6a5-1162fb85cafb', '2024-08-28', '2024-09-04', 'c1b6f4fc-32c1-4cd5-b796-0c5059152a52', 2, 4, 6, 3, 'pref I before E', array[(16, 2), (20, 3)]::option_units[]) $$,
	'Should be able to update the draft for Plots'
);

select bag_eq(
	$$ select 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, payment_status, created_at, updated_at from payment $$,
	$$ values (2, 12, '2024-08-28', '2024-09-04', 3200, 2, 10420, 4, 20840, 6, 25080, 3, 2450, 4900, 79160, 'pref I before E', 'draft', '2024-01-01 01:01:01', current_timestamp)
	        , (2, 14, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, '', 'draft', current_timestamp, current_timestamp)
	$$,
	'Should have added and updated payments'
);

select bag_eq(
	$$ select payment_id, campsite_type_option_id, units, subtotal from payment_option $$,
	$$ values (22, 16, 2, 10200)
	        , (22, 20, 3, 2070)
	$$,
	'Should have added, updated, and removed payment options'
);


select *
from finish();

rollback;
