-- Migration 003: Server Sessions to D1 Database
-- Migrate server session management from KV Namespace to D1 Database
-- Date: 2026-01-28

-- Create server_sessions table
CREATE TABLE IF NOT EXISTS server_sessions (
  user_id TEXT PRIMARY KEY,
  status TEXT NOT NULL CHECK(status IN ('gathering', 'recommending', 'selecting', 'ordering', 'completed')),
  collected_info TEXT,
  last_recommendation TEXT,
  messages TEXT,
  created_at INTEGER NOT NULL,
  updated_at INTEGER NOT NULL,
  expires_at INTEGER NOT NULL
);

-- Create index for expiry cleanup
CREATE INDEX IF NOT EXISTS idx_server_sessions_expires ON server_sessions(expires_at);

-- Note: Existing KV sessions will be automatically migrated on first access
-- Old sessions in KV will expire naturally (1 hour TTL)
