/**
 * src/db/migrations/006-add-zatca-invoices.ts
 * Migration: ZATCA-issued e-invoices for tenants' own customers.
 *
 * Note: this is NOT the same as the existing `invoices` table from
 * 002_billing.sql, which tracks how the platform charges its tenants.
 * This table tracks invoices a tenant issues to their end customers
 * (e.g. Kafaat issuing a brokerage receipt to a buyer).
 *
 * Phase 1 fields: ubl_xml, qr_base64, status='draft'|'pending_approval'|'issued'.
 * Phase 2 fields (zatca_uuid, zatca_hash, signature) will be populated when
 * the cryptographic stamping + Fatoora clearance call is implemented.
 */
import { createChildLogger } from '../../utils/logger.js';
const log = createChildLogger('db:migration:006');
const MIGRATION_SQL = `
CREATE TABLE IF NOT EXISTS zatca_invoices (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
  customer_id TEXT NOT NULL,

  invoice_number TEXT NOT NULL,
  invoice_type TEXT NOT NULL DEFAULT 'simplified'
    CHECK (invoice_type IN ('simplified', 'standard')),

  -- Seller snapshot (preserved at issuance for historical truth)
  seller_name TEXT NOT NULL,
  seller_vat_number TEXT NOT NULL,
  seller_cr_number TEXT,

  -- Buyer
  buyer_name TEXT NOT NULL,
  buyer_vat_number TEXT,

  -- Items + money
  items JSONB NOT NULL,
  subtotal NUMERIC(14,2) NOT NULL,
  vat_total NUMERIC(14,2) NOT NULL,
  total NUMERIC(14,2) NOT NULL,
  currency TEXT NOT NULL DEFAULT 'SAR',

  -- ZATCA artifacts
  ubl_xml TEXT NOT NULL,
  qr_base64 TEXT NOT NULL,

  -- Phase-2 fields (filled when signing/clearance lands)
  zatca_uuid UUID,
  zatca_hash TEXT,
  zatca_clearance_response JSONB,

  -- Status & approval
  status TEXT NOT NULL DEFAULT 'pending_approval'
    CHECK (status IN ('draft', 'pending_approval', 'issued', 'cleared', 'rejected', 'cancelled')),
  approval_request_id UUID REFERENCES approval_requests(id) ON DELETE SET NULL,

  -- Timestamps
  issued_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
  approved_at TIMESTAMP WITH TIME ZONE,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),

  UNIQUE(tenant_id, invoice_number)
);

CREATE INDEX IF NOT EXISTS idx_zatca_invoices_tenant
  ON zatca_invoices(tenant_id);
CREATE INDEX IF NOT EXISTS idx_zatca_invoices_customer
  ON zatca_invoices(tenant_id, customer_id);
CREATE INDEX IF NOT EXISTS idx_zatca_invoices_status
  ON zatca_invoices(tenant_id, status) WHERE status IN ('pending_approval', 'issued');
CREATE INDEX IF NOT EXISTS idx_zatca_invoices_issued_at
  ON zatca_invoices(tenant_id, issued_at DESC);
`;
export async function migrate() {
    log.info('Running migration 006: add zatca_invoices table');
    log.info('Run the following SQL in Supabase SQL Editor:');
    log.info(MIGRATION_SQL);
}
export async function rollback() {
    log.info('Rollback SQL:');
    log.info(`DROP TABLE IF EXISTS zatca_invoices CASCADE;`);
}
//# sourceMappingURL=006-add-zatca-invoices.js.map