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

begin;

set search_path to camper, public;

create table campsite_type (
	campsite_type_id serial primary key,
	company_id integer not null references company,
	slug uuid not null unique default gen_random_uuid(),
	name text not null constraint name_not_empty check(length(trim(name)) > 0),
	description xml not null default ''::xml,
	active boolean not null default true
);

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

grant usage on sequence campsite_type_campsite_type_id_seq to admin;

alter table campsite_type enable row level security;

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

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

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

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


commit;
