-- Deploy numerus:payment_account to pg
-- requires: roles
-- requires: schema_numerus
-- requires: company
-- requires: payment_account_type
-- requires: extension_pgcrypto

begin;

set search_path to numerus, public;

create table payment_account (
	payment_account_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(),
	payment_account_type text not null references payment_account_type,
	name text not null constraint payment_account_name_not_empty check(length(trim(name)) > 0),
	unique (payment_account_id, payment_account_type)
);

grant select, insert, update, delete on table payment_account to invoicer;
grant select, insert, update, delete on table payment_account to admin;

alter table payment_account enable row level security;

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


commit;
