import { NextRequest, NextResponse } from 'next/server'
import { requireAdmin } from '@/lib/admin-auth'
import pool from '@/lib/db'

export async function POST(request: NextRequest) {
  const guard = await requireAdmin(request)
  if (guard.kind === 'response') return guard.response

  const results: string[] = []

  // Finanzplan tables — the ones missing on production
  const statements = [
    // 004 — investor data masking columns
    `ALTER TABLE pitch_investors ADD COLUMN IF NOT EXISTS first_activity_at TIMESTAMPTZ`,
    `ALTER TABLE pitch_investors ADD COLUMN IF NOT EXISTS data_masked_at TIMESTAMPTZ`,
    `CREATE INDEX IF NOT EXISTS idx_pitch_investors_mask_check
       ON pitch_investors (first_activity_at)
       WHERE first_activity_at IS NOT NULL AND data_masked_at IS NULL`,
    // 004b — backfill first_activity_at for existing investors from last_login_at
    `UPDATE pitch_investors
     SET first_activity_at = last_login_at
     WHERE last_login_at IS NOT NULL
       AND first_activity_at IS NULL`,
    `CREATE TABLE IF NOT EXISTS fp_scenarios (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      name TEXT NOT NULL DEFAULT 'Base Case',
      description TEXT,
      is_default BOOLEAN DEFAULT false,
      created_at TIMESTAMPTZ DEFAULT NOW(),
      updated_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `INSERT INTO fp_scenarios (name, description, is_default)
     SELECT 'Base Case', 'Basisdaten aus Excel-Import', true
     WHERE NOT EXISTS (SELECT 1 FROM fp_scenarios WHERE is_default = true)`,
    `CREATE TABLE IF NOT EXISTS fp_kunden (
      id SERIAL PRIMARY KEY, scenario_id UUID REFERENCES fp_scenarios(id) ON DELETE CASCADE,
      segment_name TEXT NOT NULL, segment_index INT NOT NULL, row_label TEXT NOT NULL, row_index INT NOT NULL,
      percentage NUMERIC(5,3), formula_type TEXT, is_editable BOOLEAN DEFAULT false,
      values JSONB NOT NULL DEFAULT '{}', excel_row INT, sort_order INT NOT NULL,
      created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `CREATE TABLE IF NOT EXISTS fp_kunden_summary (
      id SERIAL PRIMARY KEY, scenario_id UUID REFERENCES fp_scenarios(id) ON DELETE CASCADE,
      row_label TEXT NOT NULL, row_index INT NOT NULL, values JSONB NOT NULL DEFAULT '{}',
      excel_row INT, sort_order INT NOT NULL
    )`,
    `CREATE TABLE IF NOT EXISTS fp_umsatzerloese (
      id SERIAL PRIMARY KEY, scenario_id UUID REFERENCES fp_scenarios(id) ON DELETE CASCADE,
      section TEXT NOT NULL, row_label TEXT NOT NULL, row_index INT NOT NULL,
      is_editable BOOLEAN DEFAULT false, values JSONB NOT NULL DEFAULT '{}',
      excel_row INT, sort_order INT NOT NULL,
      created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `CREATE TABLE IF NOT EXISTS fp_materialaufwand (
      id SERIAL PRIMARY KEY, scenario_id UUID REFERENCES fp_scenarios(id) ON DELETE CASCADE,
      section TEXT NOT NULL, row_label TEXT NOT NULL, row_index INT NOT NULL,
      is_editable BOOLEAN DEFAULT false, values JSONB NOT NULL DEFAULT '{}',
      excel_row INT, sort_order INT NOT NULL,
      created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `CREATE TABLE IF NOT EXISTS fp_personalkosten (
      id SERIAL PRIMARY KEY, scenario_id UUID REFERENCES fp_scenarios(id) ON DELETE CASCADE,
      person_name TEXT NOT NULL, person_nr TEXT, position TEXT,
      start_date DATE, end_date DATE, brutto_monthly NUMERIC(10,2),
      annual_raise_pct NUMERIC(5,2) DEFAULT 3.0, ag_sozial_pct NUMERIC(5,2) DEFAULT 20.425,
      is_editable BOOLEAN DEFAULT true,
      values_brutto JSONB NOT NULL DEFAULT '{}', values_sozial JSONB NOT NULL DEFAULT '{}',
      values_total JSONB NOT NULL DEFAULT '{}',
      excel_row INT, sort_order INT NOT NULL,
      created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `CREATE TABLE IF NOT EXISTS fp_betriebliche_aufwendungen (
      id SERIAL PRIMARY KEY, scenario_id UUID REFERENCES fp_scenarios(id) ON DELETE CASCADE,
      category TEXT NOT NULL, row_label TEXT NOT NULL, row_index INT NOT NULL,
      is_editable BOOLEAN DEFAULT true, is_sum_row BOOLEAN DEFAULT false, formula_desc TEXT,
      values JSONB NOT NULL DEFAULT '{}', excel_row INT, sort_order INT NOT NULL,
      created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `CREATE TABLE IF NOT EXISTS fp_investitionen (
      id SERIAL PRIMARY KEY, scenario_id UUID REFERENCES fp_scenarios(id) ON DELETE CASCADE,
      item_name TEXT NOT NULL, category TEXT, purchase_amount NUMERIC(12,2) NOT NULL,
      purchase_date DATE, afa_years INT, afa_end_date DATE, is_editable BOOLEAN DEFAULT true,
      values_invest JSONB NOT NULL DEFAULT '{}', values_afa JSONB NOT NULL DEFAULT '{}',
      excel_row INT, sort_order INT NOT NULL,
      created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `CREATE TABLE IF NOT EXISTS fp_sonst_ertraege (
      id SERIAL PRIMARY KEY, scenario_id UUID REFERENCES fp_scenarios(id) ON DELETE CASCADE,
      category TEXT NOT NULL, row_label TEXT, row_index INT NOT NULL,
      is_editable BOOLEAN DEFAULT true, is_sum_row BOOLEAN DEFAULT false,
      values JSONB NOT NULL DEFAULT '{}', excel_row INT, sort_order INT NOT NULL,
      created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `CREATE TABLE IF NOT EXISTS fp_liquiditaet (
      id SERIAL PRIMARY KEY, scenario_id UUID REFERENCES fp_scenarios(id) ON DELETE CASCADE,
      row_label TEXT NOT NULL, row_type TEXT NOT NULL,
      is_editable BOOLEAN DEFAULT false, formula_desc TEXT,
      values JSONB NOT NULL DEFAULT '{}', excel_row INT, sort_order INT NOT NULL,
      created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `CREATE TABLE IF NOT EXISTS fp_guv (
      id SERIAL PRIMARY KEY, scenario_id UUID REFERENCES fp_scenarios(id) ON DELETE CASCADE,
      row_label TEXT NOT NULL, row_index INT NOT NULL,
      is_sum_row BOOLEAN DEFAULT false, formula_desc TEXT,
      values JSONB NOT NULL DEFAULT '{}', excel_row INT, sort_order INT NOT NULL,
      created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `CREATE TABLE IF NOT EXISTS fp_cell_overrides (
      id SERIAL PRIMARY KEY, scenario_id UUID REFERENCES fp_scenarios(id) ON DELETE CASCADE,
      sheet_name TEXT NOT NULL, row_id INT NOT NULL, month_key TEXT NOT NULL,
      override_value NUMERIC, created_at TIMESTAMPTZ DEFAULT NOW(),
      UNIQUE(scenario_id, sheet_name, row_id, month_key)
    )`,
    `CREATE INDEX IF NOT EXISTS idx_fp_kunden_scenario ON fp_kunden(scenario_id)`,
    `CREATE INDEX IF NOT EXISTS idx_fp_kunden_summary_scenario ON fp_kunden_summary(scenario_id)`,
    `CREATE INDEX IF NOT EXISTS idx_fp_umsatz_scenario ON fp_umsatzerloese(scenario_id)`,
    `CREATE INDEX IF NOT EXISTS idx_fp_material_scenario ON fp_materialaufwand(scenario_id)`,
    `CREATE INDEX IF NOT EXISTS idx_fp_personal_scenario ON fp_personalkosten(scenario_id)`,
    `CREATE INDEX IF NOT EXISTS idx_fp_betrieb_scenario ON fp_betriebliche_aufwendungen(scenario_id)`,
    `CREATE INDEX IF NOT EXISTS idx_fp_invest_scenario ON fp_investitionen(scenario_id)`,
    `CREATE INDEX IF NOT EXISTS idx_fp_sonst_scenario ON fp_sonst_ertraege(scenario_id)`,
    `CREATE INDEX IF NOT EXISTS idx_fp_liquid_scenario ON fp_liquiditaet(scenario_id)`,
    `CREATE INDEX IF NOT EXISTS idx_fp_guv_scenario ON fp_guv(scenario_id)`,
    `CREATE INDEX IF NOT EXISTS idx_fp_overrides_lookup ON fp_cell_overrides(scenario_id, sheet_name, row_id)`,
    // 005 — data room
    `CREATE TABLE IF NOT EXISTS dataroom_documents (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      filename TEXT NOT NULL,
      file_path TEXT NOT NULL,
      display_name TEXT,
      mime_type TEXT,
      file_size BIGINT,
      uploaded_by TEXT NOT NULL DEFAULT 'admin',
      created_at TIMESTAMPTZ DEFAULT NOW(),
      updated_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `CREATE TABLE IF NOT EXISTS dataroom_releases (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      document_id UUID REFERENCES dataroom_documents(id) ON DELETE CASCADE,
      investor_id UUID REFERENCES pitch_investors(id) ON DELETE CASCADE,
      released_by TEXT NOT NULL,
      released_at TIMESTAMPTZ DEFAULT NOW(),
      UNIQUE(document_id, investor_id)
    )`,
    `CREATE TABLE IF NOT EXISTS dataroom_investor_uploads (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      investor_id UUID REFERENCES pitch_investors(id) ON DELETE CASCADE,
      filename TEXT NOT NULL,
      file_path TEXT NOT NULL,
      display_name TEXT,
      mime_type TEXT,
      file_size BIGINT,
      created_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `CREATE INDEX IF NOT EXISTS idx_dataroom_releases_investor ON dataroom_releases(investor_id)`,
    `CREATE INDEX IF NOT EXISTS idx_dataroom_releases_document ON dataroom_releases(document_id)`,
    `CREATE INDEX IF NOT EXISTS idx_dataroom_uploads_investor ON dataroom_investor_uploads(investor_id)`,
    // 006 — dataroom bilingual descriptions
    `ALTER TABLE dataroom_documents ADD COLUMN IF NOT EXISTS description_de TEXT`,
    `ALTER TABLE dataroom_documents ADD COLUMN IF NOT EXISTS description_en TEXT`,
    `ALTER TABLE dataroom_investor_uploads ADD COLUMN IF NOT EXISTS description_de TEXT`,
    `ALTER TABLE dataroom_investor_uploads ADD COLUMN IF NOT EXISTS description_en TEXT`,
    // 007 — investor preferred language
    `ALTER TABLE pitch_investors ADD COLUMN IF NOT EXISTS preferred_lang VARCHAR(5) NOT NULL DEFAULT 'de'`,
    // 008 — short links for magic URLs
    `CREATE TABLE IF NOT EXISTS pitch_short_links (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      short_code VARCHAR(10) UNIQUE NOT NULL,
      token VARCHAR(128) NOT NULL,
      created_at TIMESTAMPTZ DEFAULT NOW()
    )`,
    `CREATE INDEX IF NOT EXISTS idx_pitch_short_links_code ON pitch_short_links(short_code)`,
  ]

  for (const sql of statements) {
    try {
      await pool.query(sql)
      const label = sql.substring(0, 60).replace(/\s+/g, ' ')
      results.push(`OK: ${label}...`)
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err)
      results.push(`ERROR: ${msg}`)
    }
  }

  return NextResponse.json({ success: true, results })
}
