-- AI agent controls (Amber, the website assistant, and Sara, the in-app assistant) and Sara's conversations.
-- Safe to run more than once. Run it BEFORE deploying the build that uses these tables.
-- Amber's chat history tables are in add_site_chat_history.sql.

-- One row per business account and agent. No row means "use the defaults" (agent on, default model).
CREATE TABLE IF NOT EXISTS fact_ai_agent_setting (
  id                  BIGINT       NOT NULL AUTO_INCREMENT,
  tenant_id           INT          NOT NULL,
  agent_code          VARCHAR(20)  NOT NULL,            -- AMBER or SARA
  is_enabled          TINYINT(1)   NOT NULL DEFAULT 1,
  model_name          VARCHAR(100) NULL,                -- NULL = the agent default
  extra_instructions  TEXT         NULL,                -- added to the agent's own instructions
  record_history      TINYINT(1)   NOT NULL DEFAULT 1,  -- Amber: keep visitor chats
  retention_days      INT          NULL,                -- Amber: delete chats this long after their last message
  max_steps           INT          NULL,                -- Sara: most tool calls she makes in one turn
  access_mode         VARCHAR(10)  NOT NULL DEFAULT 'ALL',  -- Sara: ALL users or ADMINS (tenant admins) only
  updated_at          DATETIME(6)  NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  updated_by          INT          NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_ai_agent_setting (tenant_id, agent_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Sara: one conversation belongs to one person and is private to them.
CREATE TABLE IF NOT EXISTS fact_sara_conversation (
  id          BIGINT       NOT NULL AUTO_INCREMENT,
  tenant_id   INT          NOT NULL,
  user_id     INT          NOT NULL,
  title       VARCHAR(150) NULL,
  created_at  DATETIME(6)  NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  updated_at  DATETIME(6)  NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  is_deleted  TINYINT(1)   NOT NULL DEFAULT 0,
  PRIMARY KEY (id),
  KEY idx_sara_conversation_user (tenant_id, user_id, updated_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- The conversation in the model's own format (role, content, tool calls, tool results) so a turn can be resumed exactly.
-- decision / action_summary / outcome are set on the result of a change Sara asked to make, and form the audit log.
CREATE TABLE IF NOT EXISTS fact_sara_message (
  id              BIGINT       NOT NULL AUTO_INCREMENT,
  conversation_id BIGINT       NOT NULL,
  tenant_id       INT          NOT NULL,
  user_id         INT          NOT NULL,
  role            VARCHAR(12)  NOT NULL,                -- user, assistant or tool
  content         MEDIUMTEXT   NULL,
  tool_calls_json MEDIUMTEXT   NULL,                    -- assistant messages that asked for tools
  tool_call_id    VARCHAR(100) NULL,                    -- tool messages: which call this answers
  decision        VARCHAR(10)  NULL,                    -- APPROVED or DECLINED
  action_summary  VARCHAR(500) NULL,
  outcome         VARCHAR(10)  NULL,                    -- OK, FAILED or DECLINED
  created_at      DATETIME(6)  NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  PRIMARY KEY (id),
  KEY idx_sara_message_conversation (conversation_id, id),
  KEY idx_sara_message_audit (tenant_id, decision, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
