-- Chat Switchboard — 013 Files (SQLite)
-- Consolidated v0.28.0: clean install, no legacy attachments migration

CREATE TABLE IF NOT EXISTS files (
    id              TEXT PRIMARY KEY,
    channel_id      TEXT REFERENCES channels(id) ON DELETE CASCADE,
    message_id      TEXT REFERENCES messages(id) ON DELETE SET NULL,
    user_id         TEXT REFERENCES users(id) ON DELETE SET NULL,
    project_id      TEXT REFERENCES projects(id) ON DELETE SET NULL,
    origin          TEXT NOT NULL DEFAULT 'user_upload'
                    CHECK (origin IN ('user_upload', 'tool_output', 'system')),
    filename        TEXT NOT NULL,
    content_type    TEXT NOT NULL DEFAULT 'application/octet-stream',
    size_bytes      INTEGER NOT NULL DEFAULT 0,
    storage_key     TEXT NOT NULL,
    display_hint    TEXT NOT NULL DEFAULT 'download'
                    CHECK (display_hint IN ('inline', 'download', 'thumbnail')),
    extracted_text  TEXT,
    metadata        TEXT DEFAULT '{}',
    created_at      TEXT DEFAULT (datetime('now')),
    updated_at      TEXT DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_files_channel  ON files(channel_id);
CREATE INDEX IF NOT EXISTS idx_files_message  ON files(message_id);
CREATE INDEX IF NOT EXISTS idx_files_user     ON files(user_id);
CREATE INDEX IF NOT EXISTS idx_files_project  ON files(project_id);
CREATE INDEX IF NOT EXISTS idx_files_origin   ON files(origin);
