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

begin;

set search_path to camper, public;

select plan(12);

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

set client_min_messages to warning;
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 (1, '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"};')
     , ('image/x-xpixmap', 'static char *s[]={"1 1 1 1","a c #ff00ff","a"};')
     , ('image/x-xpixmap', 'static char *s[]={"1 1 1 1","a c #ffff00","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"};'))
     , (3, 1, 'cover3.xpm', sha256('static char *s[]={"1 1 1 1","a c #ff00ff","a"};'))
     , (4, 1, 'cover4.xpm', sha256('static char *s[]={"1 1 1 1","a c #ffff00","a"};'))
;

insert into campsite_type (company_id, slug, media_id, name, spiel, info, facilities, description, additional_info, check_in, check_out, max_campers, bookable_nights, overflow_allowed, ask_zone_preferences, active)
values (1, '87452b88-b48f-48d3-bb6c-0296de64164e', 2, 'Type A', '<h1>1</h1>', '<p>info 1</p>', '<p>facilities A</p>', '<p>A</p>', '<p>Additional A</p>', '', '', 5, '[1, 7]', true, false, true)
     , (1, '9b6370f7-f941-46f2-bc6e-de455675bd0a', 3, 'Type B', '<h2>2</h2>', '<p>info 2</p>', '<p>facilities B</p>', '<p>B</p>', '<p>Additional B</p>', '', '', 4, '[2, 6]', false, true, false)
;

select lives_ok(
	$$ select edit_campsite_type('87452b88-b48f-48d3-bb6c-0296de64164e', 4, 'Type 1', '<h3>A</h3>', '<p>Features A</p>', '<p>Pricing 1</p>', '<p>1</p>', '<p>Additional 1</p>', 'Check-in 1', 'Check-out 1', 2, '(1, 7)', false, true, false) $$,
	'Should be able to edit the first type'
);

select lives_ok(
	$$ select edit_campsite_type('9b6370f7-f941-46f2-bc6e-de455675bd0a', null, 'Type 2', '<h1>B</h1>', '<p>Features B</p>', '<p>Pricing 2</p>', '<p>2</p>', '<p>Additional 2</p>', 'Check-in 2', 'Check-out 2', 9, '(2, 6)', true, false, true) $$,
	'Should be able to edit the second type'
);

select bag_eq(
	$$ select slug::text, media_id, name, spiel::text, info::text, facilities::text, description::text, additional_info::text, check_in, check_out, max_campers, bookable_nights, overflow_allowed, ask_zone_preferences, active from campsite_type $$,
	$$ values ('87452b88-b48f-48d3-bb6c-0296de64164e', 4, 'Type 1', '<h3>A</h3>', '<p>Features A</p>', '<p>Pricing 1</p>', '<p>1</p>', '<p>Additional 1</p>', 'Check-in 1', 'Check-out 1', 2, int4range(2, 7), false, true, false)
	        , ('9b6370f7-f941-46f2-bc6e-de455675bd0a', 3, 'Type 2', '<h1>B</h1>', '<p>Features B</p>', '<p>Pricing 2</p>', '<p>2</p>', '<p>Additional 2</p>', 'Check-in 2', 'Check-out 2', 9, int4range(3, 6), true, false, true)
	$$,
	'Should have updated all campsite types.'
);


select *
from finish();

rollback;
