-- Migration: Fix DDoS Session Status Constraint
-- Created: 2026-02-05
-- Description: Update status check constraint to match TypeScript types

-- SQLite doesn't support ALTER TABLE to modify CHECK constraints
-- So we need to recreate the table with correct constraint

-- Step 1: Create new table with correct constraint
CREATE TABLE IF NOT EXISTS ddos_sessions_new (
  user_id TEXT PRIMARY KEY,
  status TEXT NOT NULL CHECK(status IN ('gathering', 'analyzing', 'mitigating', 'monitoring', 'completed')),
  collected_info TEXT,
  messages TEXT,
  created_at INTEGER NOT NULL,
  updated_at INTEGER NOT NULL,
  expires_at INTEGER NOT NULL
);

-- Step 2: Copy data from old table (if exists)
INSERT OR IGNORE INTO ddos_sessions_new
SELECT user_id,
       CASE
         WHEN status = 'recommending' THEN 'analyzing'
         ELSE status
       END as status,
       collected_info, messages, created_at, updated_at, expires_at
FROM ddos_sessions;

-- Step 3: Drop old table
DROP TABLE IF EXISTS ddos_sessions;

-- Step 4: Rename new table
ALTER TABLE ddos_sessions_new RENAME TO ddos_sessions;

-- Step 5: Recreate indexes
CREATE INDEX IF NOT EXISTS idx_ddos_sessions_expires_at ON ddos_sessions(expires_at);
CREATE INDEX IF NOT EXISTS idx_ddos_sessions_status ON ddos_sessions(status);
