"""Programmatic SEO: article templates, template data, published scenarios, articles + FTS."""


def up(conn):
    conn.execute("""
        CREATE TABLE IF NOT EXISTS published_scenarios (
            id           INTEGER PRIMARY KEY AUTOINCREMENT,
            slug         TEXT UNIQUE NOT NULL,
            title        TEXT NOT NULL,
            subtitle     TEXT,
            location     TEXT NOT NULL,
            country      TEXT NOT NULL,
            venue_type   TEXT NOT NULL DEFAULT 'indoor',
            ownership    TEXT NOT NULL DEFAULT 'rent',
            court_config TEXT NOT NULL,
            state_json   TEXT NOT NULL,
            calc_json    TEXT NOT NULL,
            template_data_id INTEGER REFERENCES template_data(id),
            created_at   TEXT NOT NULL DEFAULT (datetime('now')),
            updated_at   TEXT
        )
    """)
    conn.execute("CREATE INDEX IF NOT EXISTS idx_pub_scenarios_slug ON published_scenarios(slug)")

    conn.execute("""
        CREATE TABLE IF NOT EXISTS article_templates (
            id                       INTEGER PRIMARY KEY AUTOINCREMENT,
            name                     TEXT NOT NULL,
            slug                     TEXT UNIQUE NOT NULL,
            content_type             TEXT NOT NULL DEFAULT 'calculator',
            input_schema             TEXT NOT NULL,
            url_pattern              TEXT NOT NULL,
            title_pattern            TEXT NOT NULL,
            meta_description_pattern TEXT,
            body_template            TEXT NOT NULL,
            created_at               TEXT NOT NULL DEFAULT (datetime('now')),
            updated_at               TEXT
        )
    """)
    conn.execute("CREATE INDEX IF NOT EXISTS idx_article_templates_slug ON article_templates(slug)")

    conn.execute("""
        CREATE TABLE IF NOT EXISTS template_data (
            id          INTEGER PRIMARY KEY AUTOINCREMENT,
            template_id INTEGER NOT NULL REFERENCES article_templates(id),
            data_json   TEXT NOT NULL,
            scenario_id INTEGER REFERENCES published_scenarios(id),
            article_id  INTEGER REFERENCES articles(id),
            created_at  TEXT NOT NULL DEFAULT (datetime('now')),
            updated_at  TEXT
        )
    """)
    conn.execute("CREATE INDEX IF NOT EXISTS idx_template_data_template ON template_data(template_id)")

    conn.execute("""
        CREATE TABLE IF NOT EXISTS articles (
            id               INTEGER PRIMARY KEY AUTOINCREMENT,
            url_path         TEXT UNIQUE NOT NULL,
            slug             TEXT UNIQUE NOT NULL,
            title            TEXT NOT NULL,
            meta_description TEXT,
            country          TEXT,
            region           TEXT,
            og_image_url     TEXT,
            status           TEXT NOT NULL DEFAULT 'draft',
            published_at     TEXT,
            template_data_id INTEGER REFERENCES template_data(id),
            created_at       TEXT NOT NULL DEFAULT (datetime('now')),
            updated_at       TEXT
        )
    """)
    conn.execute("CREATE INDEX IF NOT EXISTS idx_articles_url_path ON articles(url_path)")
    conn.execute("CREATE INDEX IF NOT EXISTS idx_articles_slug ON articles(slug)")
    conn.execute("CREATE INDEX IF NOT EXISTS idx_articles_status ON articles(status, published_at)")

    conn.execute("""
        CREATE VIRTUAL TABLE IF NOT EXISTS articles_fts USING fts5(
            title, meta_description, country, region,
            content='articles', content_rowid='id'
        )
    """)

    # FTS sync triggers
    conn.execute("""
        CREATE TRIGGER IF NOT EXISTS articles_ai AFTER INSERT ON articles BEGIN
            INSERT INTO articles_fts(rowid, title, meta_description, country, region)
            VALUES (new.id, new.title, new.meta_description, new.country, new.region);
        END
    """)
    conn.execute("""
        CREATE TRIGGER IF NOT EXISTS articles_ad AFTER DELETE ON articles BEGIN
            INSERT INTO articles_fts(articles_fts, rowid, title, meta_description, country, region)
            VALUES ('delete', old.id, old.title, old.meta_description, old.country, old.region);
        END
    """)
    conn.execute("""
        CREATE TRIGGER IF NOT EXISTS articles_au AFTER UPDATE ON articles BEGIN
            INSERT INTO articles_fts(articles_fts, rowid, title, meta_description, country, region)
            VALUES ('delete', old.id, old.title, old.meta_description, old.country, old.region);
            INSERT INTO articles_fts(rowid, title, meta_description, country, region)
            VALUES (new.id, new.title, new.meta_description, new.country, new.region);
        END
    """)
