export const SCHEMA_SQL = `
CREATE TABLE IF NOT EXISTS users (
  id TEXT PRIMARY KEY,
  username TEXT UNIQUE NOT NULL,
  email TEXT,
  password_hash TEXT NOT NULL,
  role TEXT NOT NULL DEFAULT 'operator',
  created_at TEXT NOT NULL,
  last_login TEXT
);

CREATE TABLE IF NOT EXISTS sessions (
  id TEXT PRIMARY KEY,
  user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  token_hash TEXT NOT NULL,
  expires_at TEXT NOT NULL,
  created_at TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS audit_log (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  user_id TEXT NOT NULL REFERENCES users(id),
  action TEXT NOT NULL,
  details TEXT,
  created_at TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS conversations (
  id TEXT PRIMARY KEY,
  squad_name TEXT NOT NULL,
  created_at TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS chat_messages (
  id TEXT PRIMARY KEY,
  conversation_id TEXT NOT NULL REFERENCES conversations(id),
  role TEXT NOT NULL,
  content TEXT NOT NULL,
  agent_id TEXT,
  agent_name TEXT,
  created_at TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS squad_activities (
  id TEXT PRIMARY KEY,
  squad_code TEXT NOT NULL,
  conversation_id TEXT,
  started_at TEXT NOT NULL,
  finished_at TEXT,
  duration_ms INTEGER,
  status TEXT NOT NULL DEFAULT 'running',
  user_message TEXT,
  result_summary TEXT,
  triggered_by TEXT DEFAULT 'dashboard',
  created_at TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_squad_activities_squad_date
  ON squad_activities(squad_code, started_at);

CREATE INDEX IF NOT EXISTS idx_squad_activities_date
  ON squad_activities(started_at);

CREATE TABLE IF NOT EXISTS scheduled_jobs (
  squad_name TEXT PRIMARY KEY,
  cron_expression TEXT NOT NULL,
  prompt TEXT NOT NULL,
  mode TEXT DEFAULT 'autonomous',
  timeout_minutes INTEGER DEFAULT 30,
  retry_on_failure INTEGER DEFAULT 0,
  timezone TEXT,
  status TEXT DEFAULT 'active',
  last_run_at TEXT,
  last_result TEXT,
  next_run_at TEXT,
  created_at TEXT DEFAULT (datetime('now')),
  updated_at TEXT DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_squad_activities_triggered_by
  ON squad_activities(triggered_by, squad_code);

CREATE TABLE IF NOT EXISTS leads (
  id TEXT PRIMARY KEY,
  email TEXT UNIQUE NOT NULL,
  name TEXT,
  company TEXT,
  metadata TEXT,
  status TEXT DEFAULT 'active',
  source TEXT,
  source_detail TEXT,
  created_at TEXT DEFAULT (datetime('now')),
  updated_at TEXT DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS lead_tags (
  lead_id TEXT NOT NULL REFERENCES leads(id) ON DELETE CASCADE,
  tag TEXT NOT NULL,
  PRIMARY KEY (lead_id, tag)
);

CREATE TABLE IF NOT EXISTS lead_lists (
  lead_id TEXT NOT NULL REFERENCES leads(id) ON DELETE CASCADE,
  list_name TEXT NOT NULL,
  PRIMARY KEY (lead_id, list_name)
);

CREATE TABLE IF NOT EXISTS email_campaigns (
  id TEXT PRIMARY KEY,
  name TEXT,
  squad_name TEXT,
  subject TEXT,
  body_html TEXT,
  body_text TEXT,
  from_email TEXT,
  from_name TEXT,
  status TEXT DEFAULT 'draft',
  total_recipients INTEGER DEFAULT 0,
  sent_count INTEGER DEFAULT 0,
  failed_count INTEGER DEFAULT 0,
  bounce_count INTEGER DEFAULT 0,
  complaint_count INTEGER DEFAULT 0,
  created_at TEXT DEFAULT (datetime('now')),
  started_at TEXT,
  completed_at TEXT
);

CREATE TABLE IF NOT EXISTS email_queue (
  id TEXT PRIMARY KEY,
  campaign_id TEXT REFERENCES email_campaigns(id) ON DELETE CASCADE,
  lead_id TEXT REFERENCES leads(id) ON DELETE SET NULL,
  to_email TEXT NOT NULL,
  status TEXT DEFAULT 'pending',
  ses_message_id TEXT,
  error TEXT,
  attempts INTEGER DEFAULT 0,
  scheduled_at TEXT,
  sent_at TEXT,
  created_at TEXT DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_email_queue_status_scheduled
  ON email_queue(status, scheduled_at);
CREATE INDEX IF NOT EXISTS idx_email_queue_ses_message_id
  ON email_queue(ses_message_id);
CREATE INDEX IF NOT EXISTS idx_email_queue_campaign_status
  ON email_queue(campaign_id, status);
CREATE INDEX IF NOT EXISTS idx_lead_tags_tag
  ON lead_tags(tag);
CREATE INDEX IF NOT EXISTS idx_lead_lists_list_name
  ON lead_lists(list_name);

CREATE TABLE IF NOT EXISTS team_members (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  role TEXT NOT NULL,
  email TEXT,
  phone TEXT,
  created_at TEXT DEFAULT (datetime('now')),
  updated_at TEXT DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS integrations (
  provider TEXT PRIMARY KEY,
  enabled INTEGER NOT NULL DEFAULT 0,
  config TEXT NOT NULL DEFAULT '{}',
  created_at TEXT DEFAULT (datetime('now')),
  updated_at TEXT DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS kanban_tasks (
  id TEXT PRIMARY KEY,
  squad_code TEXT NOT NULL,
  title TEXT NOT NULL,
  description TEXT,
  status TEXT NOT NULL DEFAULT 'planned',
  position INTEGER NOT NULL DEFAULT 0,
  result TEXT,
  started_at TEXT,
  finished_at TEXT,
  step_id TEXT,
  agent_id TEXT,
  agent_name TEXT,
  activity_id TEXT REFERENCES squad_activities(id) ON DELETE SET NULL,
  triggered_by TEXT DEFAULT 'dashboard',
  created_at TEXT DEFAULT (datetime('now')),
  updated_at TEXT DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_kanban_tasks_squad_status
  ON kanban_tasks(squad_code, status);
`;
//# sourceMappingURL=schema.js.map