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

begin;

select plan(19);

set search_path to camper, public;

select has_function('camper', 'set_season_range', array['integer', 'daterange']);
select function_lang_is('camper', 'set_season_range', array['integer', 'daterange'], 'plpgsql');
select function_returns('camper', 'set_season_range', array['integer', 'daterange'], 'void');
select isnt_definer('camper', 'set_season_range', array['integer', 'daterange']);
select volatility_is('camper', 'set_season_range', array['integer', 'daterange'], 'volatile');
select function_privs_are('camper', 'set_season_range', array ['integer', 'daterange'], 'guest', array[]::text[]);
select function_privs_are('camper', 'set_season_range', array ['integer', 'daterange'], 'employee', array[]::text[]);
select function_privs_are('camper', 'set_season_range', array ['integer', 'daterange'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'set_season_range', array ['integer', '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, 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 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 (7, '[2023-04-07, 2023-04-09]')
     , (9, '[2023-04-12, 2023-04-16]')
     , (8, '[2023-04-18, 2023-04-22]')
     , (9, '[2023-04-24, 2023-05-14]')
     , (9, '[2023-05-22, 2023-05-28]')
     , (9, '[2023-06-05, 2023-06-15]')
     , (8, '[2023-06-16, 2023-06-30]')
;

select lives_ok($$ select set_season_range( 7, '[2023-04-06, 2023-04-07]') $$);
select lives_ok($$ select set_season_range( 7, '[2023-04-08, 2023-04-10]') $$);
select lives_ok($$ select set_season_range( 9, '[2023-04-11, 2023-06-15]') $$);
select lives_ok($$ select set_season_range( 8, '[2023-04-28, 2023-04-30]') $$);
select lives_ok($$ select set_season_range( 8, '[2023-06-02, 2023-06-03]') $$);
select lives_ok($$ select set_season_range( 8, '[2023-06-09, 2023-06-10]') $$);
select lives_ok($$ select set_season_range(10, '[2023-06-23, 2023-06-25]') $$);
select lives_ok($$ select set_season_range( 7, '[2023-07-01, 2023-08-27]') $$);
select lives_ok($$ select set_season_range( 8, '[2023-08-28, 2023-08-31]') $$);

select bag_eq(
	$$ select season_id, season_range from season_calendar $$,
	$$ values ( 7, '[2023-04-06, 2023-04-10]'::daterange)
	        , ( 9, '[2023-04-11, 2023-04-27]'::daterange)
	        , ( 8, '[2023-04-28, 2023-04-30]'::daterange)
	        , ( 9, '[2023-05-01, 2023-06-01]'::daterange)
	        , ( 8, '[2023-06-02, 2023-06-03]'::daterange)
	        , ( 9, '[2023-06-04, 2023-06-08]'::daterange)
	        , ( 8, '[2023-06-09, 2023-06-10]'::daterange)
	        , ( 9, '[2023-06-11, 2023-06-15]'::daterange)
	        , ( 8, '[2023-06-16, 2023-06-22]'::daterange)
	        , (10, '[2023-06-23, 2023-06-25]'::daterange)
	        , ( 8, '[2023-06-26, 2023-06-30]'::daterange)
	        , ( 7, '[2023-07-01, 2023-08-27]'::daterange)
	        , ( 8, '[2023-08-28, 2023-08-31]'::daterange)
	$$,
	'Should have updated the calendar'
);

select *
from finish();

rollback;
