-- Extend admin enums for settlement audit
ALTER TYPE admin_target_type ADD VALUE IF NOT EXISTS 'IMAGE';
ALTER TYPE admin_target_type ADD VALUE IF NOT EXISTS 'SETTLEMENT' AFTER 'IMAGE';
ALTER TYPE admin_action ADD VALUE IF NOT EXISTS 'MARK_PAID';
ALTER TYPE admin_action ADD VALUE IF NOT EXISTS 'MARK_PENDING';

-- Payouts table: tracks settlement status per vendor+month
CREATE TABLE IF NOT EXISTS payouts (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  vendor_id UUID NOT NULL REFERENCES vendors(id),
  period_month TEXT NOT NULL,
  amount_agorot INTEGER NOT NULL,
  platform_fee_agorot INTEGER NOT NULL,
  status TEXT NOT NULL DEFAULT 'pending',
  marked_paid_at TIMESTAMPTZ,
  marked_paid_by TEXT,
  notes TEXT,
  stripe_payout_id TEXT,
  stripe_transfer_id TEXT,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  CONSTRAINT payouts_vendor_period_uniq UNIQUE (vendor_id, period_month)
);

CREATE INDEX IF NOT EXISTS payouts_vendor_id_idx ON payouts(vendor_id);
CREATE INDEX IF NOT EXISTS payouts_status_idx ON payouts(status);
