CREATE TABLE IF NOT EXISTS workspaces (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    channel_id      TEXT NOT NULL UNIQUE,
    guild_id        TEXT NOT NULL,
    agent_name      TEXT NOT NULL UNIQUE,
    workspace_path  TEXT NOT NULL,
    created_at      TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS conversations (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    workspace_id    INTEGER NOT NULL REFERENCES workspaces(id),
    discord_msg_id  TEXT NOT NULL UNIQUE,
    role            TEXT NOT NULL,
    content         TEXT NOT NULL,
    author_name     TEXT,
    created_at      TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_conversations_workspace
    ON conversations(workspace_id, created_at);
