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

begin;

set search_path to camper, public;

create table amenity (
	amenity_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),
	name text not null constraint name_not_empty check(length(trim(name)) > 0),
	info1 xml not null default '',
	info2 xml not null default '',
	active boolean not null default true,
	unique (company_id, label)
);

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

alter table amenity enable row level security;

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

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

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

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

commit;