-- Chat Switchboard — 018 Workflows (SQLite)
-- Consolidated v0.28.0: merges 023 + 025 + 027

CREATE TABLE IF NOT EXISTS workflows (
    id           TEXT PRIMARY KEY,
    team_id      TEXT REFERENCES teams(id) ON DELETE CASCADE,
    name         TEXT NOT NULL,
    slug         TEXT NOT NULL,
    description  TEXT NOT NULL DEFAULT '',
    branding     TEXT NOT NULL DEFAULT '{}',
    entry_mode   TEXT NOT NULL DEFAULT 'public_link'
                     CHECK (entry_mode IN ('public_link', 'team_only')),
    is_active    INTEGER NOT NULL DEFAULT 0,
    version      INTEGER NOT NULL DEFAULT 1,
    on_complete  TEXT,
    retention    TEXT NOT NULL DEFAULT '{"mode": "archive"}',
    webhook_url    TEXT,
    webhook_secret TEXT,
    created_by   TEXT NOT NULL REFERENCES users(id),
    created_at   TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at   TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE UNIQUE INDEX IF NOT EXISTS idx_workflows_team_slug
    ON workflows(team_id, slug) WHERE team_id IS NOT NULL;
CREATE UNIQUE INDEX IF NOT EXISTS idx_workflows_global_slug
    ON workflows(slug) WHERE team_id IS NULL;

CREATE TABLE IF NOT EXISTS workflow_stages (
    id                 TEXT PRIMARY KEY,
    workflow_id        TEXT NOT NULL REFERENCES workflows(id) ON DELETE CASCADE,
    ordinal            INTEGER NOT NULL,
    name               TEXT NOT NULL,
    persona_id         TEXT REFERENCES personas(id) ON DELETE SET NULL,
    assignment_team_id TEXT REFERENCES teams(id) ON DELETE SET NULL,
    form_template      TEXT NOT NULL DEFAULT '{}',
    stage_mode         TEXT NOT NULL DEFAULT 'chat_only'
                           CHECK (stage_mode IN ('chat_only', 'form_only', 'form_chat', 'review')),
    history_mode       TEXT NOT NULL DEFAULT 'full'
                           CHECK (history_mode IN ('full', 'summary', 'fresh')),
    auto_transition    INTEGER NOT NULL DEFAULT 0,
    transition_rules   TEXT NOT NULL DEFAULT '{}',
    surface_pkg_id     TEXT,
    sla_seconds        INTEGER,
    created_at         TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_workflow_stages_workflow
    ON workflow_stages(workflow_id, ordinal);

CREATE TABLE IF NOT EXISTS workflow_versions (
    id              TEXT PRIMARY KEY,
    workflow_id     TEXT NOT NULL REFERENCES workflows(id) ON DELETE CASCADE,
    version_number  INTEGER NOT NULL,
    snapshot        TEXT NOT NULL,
    created_at      TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE (workflow_id, version_number)
);

CREATE INDEX IF NOT EXISTS idx_workflow_versions_workflow
    ON workflow_versions(workflow_id, version_number);

CREATE TABLE IF NOT EXISTS workflow_assignments (
    id           TEXT PRIMARY KEY,
    channel_id   TEXT NOT NULL REFERENCES channels(id) ON DELETE CASCADE,
    stage        INTEGER NOT NULL,
    team_id      TEXT NOT NULL REFERENCES teams(id) ON DELETE CASCADE,
    assigned_to  TEXT REFERENCES users(id) ON DELETE SET NULL,
    status       TEXT NOT NULL DEFAULT 'unassigned'
                     CHECK (status IN ('unassigned', 'claimed', 'completed')),
    review_comments TEXT NOT NULL DEFAULT '[]',
    created_at   TEXT NOT NULL DEFAULT (datetime('now')),
    claimed_at   TEXT,
    completed_at TEXT
);

CREATE INDEX IF NOT EXISTS idx_workflow_assignments_channel
    ON workflow_assignments(channel_id, stage);
CREATE INDEX IF NOT EXISTS idx_workflow_assignments_team_status
    ON workflow_assignments(team_id, status);
