-- Deploy numerus:product to pg
-- requires: schema_numerus
-- requires: company

begin;

set search_path to numerus, public;

create table product (
	product_id serial primary key,
	company_id integer not null references company,
	slug uuid not null default gen_random_uuid(),
	name text not null,
	description text not null,
	price integer not null,
	tax_id integer not null references tax,
	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 invoicer;
grant select, insert, update, delete on table product to admin;

grant usage on sequence product_product_id_seq to invoicer;
grant usage on sequence product_product_id_seq 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;
