-- RSSuper Database Schema
-- SQLite with FTS5 for full-text search

-- Enable foreign keys
PRAGMA foreign_keys = ON;

-- Migration tracking table
CREATE TABLE IF NOT EXISTS schema_migrations (
    version INTEGER PRIMARY KEY,
    applied_at TEXT NOT NULL DEFAULT (datetime('now'))
);

-- Feed subscriptions table
CREATE TABLE IF NOT EXISTS feed_subscriptions (
    id TEXT PRIMARY KEY,
    url TEXT NOT NULL UNIQUE,
    title TEXT NOT NULL,
    category TEXT,
    enabled INTEGER NOT NULL DEFAULT 1,
    fetch_interval INTEGER NOT NULL DEFAULT 60,
    created_at TEXT NOT NULL,
    updated_at TEXT NOT NULL,
    last_fetched_at TEXT,
    next_fetch_at TEXT,
    error TEXT,
    http_auth_username TEXT,
    http_auth_password TEXT
);

-- Feed items table
CREATE TABLE IF NOT EXISTS feed_items (
    id TEXT PRIMARY KEY,
    subscription_id TEXT NOT NULL,
    title TEXT NOT NULL,
    link TEXT,
    description TEXT,
    content TEXT,
    author TEXT,
    published TEXT,
    updated TEXT,
    categories TEXT,  -- JSON array as text
    enclosure_url TEXT,
    enclosure_type TEXT,
    enclosure_length TEXT,
    guid TEXT,
    is_read INTEGER NOT NULL DEFAULT 0,
    is_starred INTEGER NOT NULL DEFAULT 0,
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    FOREIGN KEY (subscription_id) REFERENCES feed_subscriptions(id) ON DELETE CASCADE
);

-- Create index for feed items
CREATE INDEX IF NOT EXISTS idx_feed_items_subscription ON feed_items(subscription_id);
CREATE INDEX IF NOT EXISTS idx_feed_items_published ON feed_items(published DESC);
CREATE INDEX IF NOT EXISTS idx_feed_items_read ON feed_items(is_read);
CREATE INDEX IF NOT EXISTS idx_feed_items_starred ON feed_items(is_starred);

-- Search history table
CREATE TABLE IF NOT EXISTS search_history (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    query TEXT NOT NULL,
    filters_json TEXT,
    sort_option TEXT NOT NULL DEFAULT 'relevance',
    page INTEGER NOT NULL DEFAULT 1,
    page_size INTEGER NOT NULL DEFAULT 20,
    result_count INTEGER,
    created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_search_history_created ON search_history(created_at DESC);

-- FTS5 virtual table for full-text search on feed items
CREATE VIRTUAL TABLE IF NOT EXISTS feed_items_fts USING fts5(
    title,
    description,
    content,
    author,
    content='feed_items',
    content_rowid='rowid'
);

-- Trigger to keep FTS table in sync on INSERT
CREATE TRIGGER IF NOT EXISTS feed_items_ai AFTER INSERT ON feed_items BEGIN
    INSERT INTO feed_items_fts(rowid, title, description, content, author)
    VALUES (new.rowid, new.title, new.description, new.content, new.author);
END;

-- Trigger to keep FTS table in sync on DELETE
CREATE TRIGGER IF NOT EXISTS feed_items_ad AFTER DELETE ON feed_items BEGIN
    INSERT INTO feed_items_fts(feed_items_fts, rowid, title, description, content, author)
    VALUES('delete', old.rowid, old.title, old.description, old.content, old.author);
END;

-- Trigger to keep FTS table in sync on UPDATE
CREATE TRIGGER IF NOT EXISTS feed_items_au AFTER UPDATE ON feed_items BEGIN
    INSERT INTO feed_items_fts(feed_items_fts, rowid, title, description, content, author)
    VALUES('delete', old.rowid, old.title, old.description, old.content, old.author);
    INSERT INTO feed_items_fts(rowid, title, description, content, author)
    VALUES (new.rowid, new.title, new.description, new.content, new.author);
END;

-- Initial migration record
INSERT OR IGNORE INTO schema_migrations (version) VALUES (1);
