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

begin;

select plan(29);

set search_path to camper, public;

select has_table('payment_option');
select has_pk('payment_option');
select col_is_pk('payment_option', array['payment_id', 'campsite_type_option_id']);
select table_privs_are('payment_option', 'guest', array['SELECT', 'INSERT', 'UPDATE', 'DELETE']);
select table_privs_are('payment_option', 'employee', array['SELECT', 'INSERT', 'UPDATE', 'DELETE']);
select table_privs_are('payment_option', 'admin', array['SELECT', 'INSERT', 'UPDATE', 'DELETE']);
select table_privs_are('payment_option', 'authenticator', array[]::text[]);

select has_column('payment_option', 'payment_id');
select col_is_fk('payment_option', 'payment_id');
select fk_ok('payment_option', 'payment_id', 'payment', 'payment_id');
select col_type_is('payment_option', 'payment_id', 'integer');
select col_not_null('payment_option', 'payment_id');
select col_hasnt_default('payment_option', 'payment_id');

select has_column('payment_option', 'campsite_type_option_id');
select col_is_fk('payment_option', 'campsite_type_option_id');
select fk_ok('payment_option', 'campsite_type_option_id', 'campsite_type_option', 'campsite_type_option_id');
select col_type_is('payment_option', 'campsite_type_option_id', 'integer');
select col_not_null('payment_option', 'campsite_type_option_id');
select col_hasnt_default('payment_option', 'campsite_type_option_id');

select has_column('payment_option', 'units');
select col_type_is('payment_option', 'units', 'integer');
select col_not_null('payment_option', 'units');
select col_hasnt_default('payment_option', 'units');

select has_column('payment_option', 'subtotal');
select col_type_is('payment_option', 'subtotal', 'integer');
select col_not_null('payment_option', 'subtotal');
select col_hasnt_default('payment_option', 'subtotal');


set client_min_messages to warning;
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, country_code, currency_code, default_lang_tag)
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 60, '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 (2, 1, 'cover2.xpm', sha256('static char *s[]={"1 1 1 1","a c #ffffff","a"};'))
;

insert into campsite_type (campsite_type_id, company_id, media_id, name, description, max_campers, bookable_nights, active)
values (10, 1, 2, 'Type A', '<p>A</p>', 5, '[1, 7]', true)
;

insert into campsite_type_option (campsite_type_option_id, campsite_type_id, name, range, per_night)
values (11, 10, 'Option 1', '[2, 2]', true)
     , (12, 10, 'Option 2', '[4, 8]', true)
;

insert into payment (payment_id, 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)
values (15, 1, 10, '2024-07-07', '2024-07-08', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, '')
;

select throws_ok(
	$$ insert into payment_option (payment_id, campsite_type_option_id, units, subtotal) values (15, 11, 0, 0) $$,
	'23514', 'new row for relation "payment_option" violates check constraint "units_positive"',
	'Should not be able to insert a payment option with zero units'
);

select throws_ok(
	$$ insert into payment_option (payment_id, campsite_type_option_id, units, subtotal) values (15, 12, 1, -1) $$,
	'23514', 'new row for relation "payment_option" violates check constraint "subtotal_not_negative"',
	'Should not be able to insert a payment option with a negative subtotal'
);


select *
from finish();

rollback;

