-- Deploy camper:campsite to pg
-- requires: roles
-- requires: schema_camper
-- requires: company
-- requires: campsite_type
-- requires: user_profile

begin;

set search_path to camper, public;

create table campsite (
	campsite_id integer generated by default as identity primary key,
	company_id integer not null references company,
	label text not null constraint label_not_empty check(length(trim(label)) > 0),
	campsite_type_id integer not null references campsite_type,
	active boolean not null default true,
	unique (company_id, label)
);

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

alter table campsite enable row level security;

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

create policy insert_to_company
on campsite
for insert
with check (
	company_id in (select company_id from user_profile)
)
;

create policy update_company
on campsite
for update
using (
	company_id in (select company_id from user_profile)
)
;

create policy delete_from_company
on campsite
for delete
using (
	company_id in (select company_id from user_profile)
)
;

commit;
