-- Wave 10: Remaining legacy JSON entities migrated to PostgreSQL

CREATE TABLE IF NOT EXISTS chart_of_accounts (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS checks_issued (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS checks_received (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS currencies (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS daily_exchange_rates (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS employees (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS exchange_rate_sources (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS internal_transfer_docs (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS journal_entries (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS payments (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS production_reports (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS purchase_credit_notes (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS purchase_returns (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS shift_overrides (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS shipment_sequences (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS shipments (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS supplier_payment_locks (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS supplier_payments (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS transfer_locks (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS sequence_counters (
  id TEXT PRIMARY KEY,
  raw JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_chart_of_accounts_updated_at ON chart_of_accounts (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_checks_issued_updated_at ON checks_issued (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_checks_received_updated_at ON checks_received (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_currencies_updated_at ON currencies (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_daily_exchange_rates_updated_at ON daily_exchange_rates (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_employees_updated_at ON employees (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_exchange_rate_sources_updated_at ON exchange_rate_sources (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_internal_transfer_docs_updated_at ON internal_transfer_docs (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_journal_entries_updated_at ON journal_entries (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_payments_updated_at ON payments (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_production_reports_updated_at ON production_reports (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_purchase_credit_notes_updated_at ON purchase_credit_notes (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_purchase_returns_updated_at ON purchase_returns (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_shift_overrides_updated_at ON shift_overrides (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_shipment_sequences_updated_at ON shipment_sequences (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_shipments_updated_at ON shipments (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_supplier_payment_locks_updated_at ON supplier_payment_locks (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_supplier_payments_updated_at ON supplier_payments (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_transfer_locks_updated_at ON transfer_locks (updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_sequence_counters_updated_at ON sequence_counters (updated_at DESC);
