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

begin;

set search_path to camper, public;

create table campsite_type (
	campsite_type_id integer generated by default as identity 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),
	media_id integer not null references media,
	spiel xml not null default ''::xml,
	info xml not null default ''::xml,
	facilities xml not null default ''::xml,
	description xml not null default ''::xml,
	max_campers integer not null constraint at_least_one_camper check(max_campers > 0),
	dogs_allowed boolean not null,
	active boolean not null default true,
	position integer not null default 2147483647
);

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;

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;
