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

begin;

set search_path to camper, public;

create table legal_text (
	company_id integer not null references company,
	slug text constraint valid_slug check(slug ~ '^[a-z0-9]+(-[a-z0-9])*$'),
	name text not null constraint name_not_empty check(length(trim(name)) > 0),
	content xml not null,
	primary key (company_id, slug)
);

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


alter table legal_text enable row level security;

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

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

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

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

commit;
