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

begin;

select plan(14);

set search_path to camper, public;

select has_function('camper', 'unset_season_range', array['daterange']);
select function_lang_is('camper', 'unset_season_range', array['daterange'], 'plpgsql');
select function_returns('camper', 'unset_season_range', array['daterange'], 'void');
select isnt_definer('camper', 'unset_season_range', array['daterange']);
select volatility_is('camper', 'unset_season_range', array['daterange'], 'volatile');
select function_privs_are('camper', 'unset_season_range', array ['daterange'], 'guest', array[]::text[]);
select function_privs_are('camper', 'unset_season_range', array ['daterange'], 'employee', array[]::text[]);
select function_privs_are('camper', 'unset_season_range', array ['daterange'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'unset_season_range', array ['daterange'], 'authenticator', array[]::text[]);


set client_min_messages to warning;
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, country_code, currency_code, default_lang_tag)
values (2, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR', 'ca')
;

insert into season (season_id, company_id, name)
values ( 7, 2, 'Peak')
     , ( 8, 2, 'Shoulder')
     , ( 9, 2, 'Off-Season')
     , (10, 2, 'Saint John’s Eve')
;

insert into season_calendar (season_id, season_range)
values ( 9, '[2023-04-01, 2023-04-05]')
     , ( 7, '[2023-04-06, 2023-04-10]')
     , ( 9, '[2023-04-11, 2023-04-12]')
     , ( 8, '[2023-04-13, 2023-04-16]')
     , ( 9, '[2023-04-17, 2023-04-19]')
     , (10, '[2023-04-20, 2023-04-22]')
     , ( 9, '[2023-04-23, 2023-04-27]')
     , ( 8, '[2023-04-28, 2023-04-30]')
;

select lives_ok($$ select unset_season_range('[2023-04-08, 2023-04-11]') $$);
select lives_ok($$ select unset_season_range('[2023-04-15, 2023-04-15]') $$);
select lives_ok($$ select unset_season_range('[2023-04-20, 2023-04-23]') $$);
select lives_ok($$ select unset_season_range('[2023-04-26, 2023-04-29]') $$);

select bag_eq(
	$$ select season_id, season_range from season_calendar $$,
	$$ values ( 9, '[2023-04-01, 2023-04-05]'::daterange)
	        , ( 7, '[2023-04-06, 2023-04-07]'::daterange)
	        , ( 9, '[2023-04-12, 2023-04-12]'::daterange)
	        , ( 8, '[2023-04-13, 2023-04-14]'::daterange)
	        , ( 8, '[2023-04-16, 2023-04-16]'::daterange)
	        , ( 9, '[2023-04-17, 2023-04-19]'::daterange)
	        , ( 9, '[2023-04-24, 2023-04-25]'::daterange)
	        , ( 8, '[2023-04-30, 2023-04-30]'::daterange)
	$$,
	'Should have updated the calendar'
);


select *
from finish();

rollback;
