-- 034 — Refonte is_legal : modèle "façade légale + activité illégale cachée" (2026-04-20)
--
-- Avant : business.is_legal binaire + production_type fourre-tout (brasserie légal ET speakeasy illégal dans le même champ)
-- Après : business.type reste STABLE (façade publique), business.illegal_activity_type héberge l'activité cachée
--
-- Deux revenus distincts :
--   revenue_per_day                  = revenu légal versé en infinite_light
--   illegal_activity_revenue_per_day = revenu illégal versé en infinite_shadow
--
-- Idempotent via IF NOT EXISTS.

-- 1. Renomme production_type → legal_specialization (sémantique claire : spécialisation LÉGALE)
DO $$
BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns
             WHERE table_name='businesses' AND column_name='production_type')
  AND NOT EXISTS (SELECT 1 FROM information_schema.columns
                  WHERE table_name='businesses' AND column_name='legal_specialization') THEN
    ALTER TABLE businesses RENAME COLUMN production_type TO legal_specialization;
  END IF;
END $$;

-- 2. Nouveau slot activité illégale
ALTER TABLE businesses ADD COLUMN IF NOT EXISTS illegal_activity_type VARCHAR(40);
ALTER TABLE businesses ADD COLUMN IF NOT EXISTS illegal_activity_started_at TIMESTAMP;
ALTER TABLE businesses ADD COLUMN IF NOT EXISTS illegal_activity_revenue_per_day INT NOT NULL DEFAULT 0;
ALTER TABLE businesses ADD COLUMN IF NOT EXISTS illegal_heat_generated INT NOT NULL DEFAULT 0;
ALTER TABLE businesses ADD COLUMN IF NOT EXISTS illegal_cash_register INT NOT NULL DEFAULT 0;
ALTER TABLE businesses ADD COLUMN IF NOT EXISTS illegal_closed_until TIMESTAMP;

-- 3. Backfill : migrer les business is_legal=false avec un legal_specialization "illégal" connu
--    vers illegal_activity_type + reset façade
UPDATE businesses
   SET illegal_activity_type = legal_specialization,
       illegal_activity_started_at = COALESCE(illegal_activity_started_at, NOW()),
       illegal_activity_revenue_per_day = CASE WHEN revenue_per_day > 0 THEN revenue_per_day ELSE 0 END,
       illegal_heat_generated = COALESCE(heat_generated, 0),
       legal_specialization = NULL,
       revenue_per_day = 0  -- reset façade, sera recalculé quand spec legal ajoutée
 WHERE is_legal = false
   AND legal_specialization IN ('speakeasy','chop_shop','point_de_deal','casino_clandestin',
                                'blackmarket_front','laundry_front','front_illegal');

-- 4. Flag d'arrêt permanent FBI/DEA (Phase 9)
ALTER TABLE businesses ADD COLUMN IF NOT EXISTS is_permanent_shutdown BOOLEAN NOT NULL DEFAULT false;
ALTER TABLE businesses ADD COLUMN IF NOT EXISTS permanent_shutdown_reason VARCHAR(20);  -- 'fbi_raid'|'dea_raid'|'admin'
ALTER TABLE businesses ADD COLUMN IF NOT EXISTS permanent_shutdown_at TIMESTAMP;

-- 5. Index de requête
CREATE INDEX IF NOT EXISTS idx_businesses_illegal_active
  ON businesses(illegal_activity_type) WHERE illegal_activity_type IS NOT NULL;

CREATE INDEX IF NOT EXISTS idx_businesses_permanent_shutdown
  ON businesses(is_permanent_shutdown) WHERE is_permanent_shutdown = true;
