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

begin;

set search_path to camper, public;

create table product (
	product_id integer generated by default as identity primary key,
	company_id integer not null references company,
	slug uuid not null default gen_random_uuid(),
	name text not null constraint name_not_empty check(length(trim(name)) > 0),
	description text not null default '',
	price integer not null,
	created_at timestamptz not null default current_timestamp
);

comment on column product.price is
'Price is stored in cents.';

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

alter table product enable row level security;

create policy company_policy
on product
using (
	exists(
		select 1
		from company_user
		join user_profile using (user_id)
		where company_user.company_id = product.company_id
	)
);

commit;
