-- Deploy camper:season_calendar to pg
-- requires: roles
-- requires: schema_camper
-- requires: season
-- requires: extension_btree_gist
-- requires: user_profile

begin;

set search_path to camper, public;

create table season_calendar (
	season_id integer not null,
	season_range daterange not null,
	primary key (season_id, season_range),
	constraint disallow_overlap exclude using gist (season_id with =, season_range with &&)
);

grant select on table season_calendar to guest;
grant select on table season_calendar to employee;
grant select, insert, update, delete on table season_calendar to admin;

alter table season_calendar enable row level security;

create policy guest_ok
on season_calendar
for select
using (true)
;

create policy insert_to_company
on season_calendar
for insert
with check (
	exists (select 1 from season join user_profile using (company_id) where season.season_id = season_calendar.season_id)
)
;

create policy update_company
on season_calendar
for update
using (
	exists (select 1 from season join user_profile using (company_id) where season.season_id = season_calendar.season_id)
)
;

create policy delete_from_company
on season_calendar
for delete
using (
	exists (select 1 from season join user_profile using (company_id) where season.season_id = season_calendar.season_id)
)
;

commit;
