create table if not exists public.commission_settlement_proofs (
  id uuid primary key default gen_random_uuid(),
  tenant_id uuid not null references public.tenants(id) on delete cascade,
  settlement_id uuid not null references public.commission_settlements(id) on delete cascade,
  proof_type text not null default 'payment'
    check (proof_type in ('payment', 'invoice', 'receipt', 'adjustment', 'other')),
  status text not null default 'submitted'
    check (status in ('submitted', 'approved', 'rejected', 'voided')),
  title text,
  description text,
  asset_id uuid references public.content_assets(id) on delete set null,
  external_url text,
  amount_cents integer check (amount_cents is null or amount_cents >= 0),
  payment_method text,
  payment_account text,
  paid_at timestamptz,
  submitted_by uuid references public.platform_users(id) on delete set null,
  reviewed_by uuid references public.platform_users(id) on delete set null,
  reviewed_at timestamptz,
  review_note text,
  metadata jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create table if not exists public.commission_settlement_export_events (
  id uuid primary key default gen_random_uuid(),
  tenant_id uuid not null references public.tenants(id) on delete cascade,
  settlement_id uuid not null references public.commission_settlements(id) on delete cascade,
  export_format text not null check (export_format in ('csv', 'json')),
  filename text not null,
  row_count integer not null default 0 check (row_count >= 0),
  content_sha256 text,
  exported_by uuid references public.platform_users(id) on delete set null,
  metadata jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now()
);

create index if not exists idx_commission_proofs_settlement
  on public.commission_settlement_proofs(tenant_id, settlement_id, status, created_at desc);

create index if not exists idx_commission_exports_settlement
  on public.commission_settlement_export_events(tenant_id, settlement_id, created_at desc);

alter table public.commission_settlement_proofs enable row level security;
alter table public.commission_settlement_export_events enable row level security;

drop policy if exists tenant_isolation on public.commission_settlement_proofs;
create policy tenant_isolation on public.commission_settlement_proofs
  for all
  using (tenant_id = app.current_tenant_id() or app.is_platform_admin())
  with check (tenant_id = app.current_tenant_id() or app.is_platform_admin());

drop policy if exists tenant_isolation on public.commission_settlement_export_events;
create policy tenant_isolation on public.commission_settlement_export_events
  for all
  using (tenant_id = app.current_tenant_id() or app.is_platform_admin())
  with check (tenant_id = app.current_tenant_id() or app.is_platform_admin());

drop trigger if exists set_updated_at on public.commission_settlement_proofs;
create trigger set_updated_at
  before update on public.commission_settlement_proofs
  for each row execute function app.touch_updated_at();
