"""Change articles unique constraint from url_path alone to (url_path, language).

Previously url_path was declared UNIQUE, which prevented multiple languages
from sharing the same url_path (e.g. /markets/germany/berlin for both de and en).
"""


def up(conn) -> None:
    # ── 1. Drop FTS triggers + virtual table ──────────────────────────────────
    conn.execute("DROP TRIGGER IF EXISTS articles_ai")
    conn.execute("DROP TRIGGER IF EXISTS articles_ad")
    conn.execute("DROP TRIGGER IF EXISTS articles_au")
    conn.execute("DROP TABLE IF EXISTS articles_fts")

    # ── 2. Recreate articles with UNIQUE(url_path, language) ──────────────────
    conn.execute("""
        CREATE TABLE articles_new (
            id               INTEGER PRIMARY KEY AUTOINCREMENT,
            url_path         TEXT 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_slug    TEXT,
            language         TEXT NOT NULL DEFAULT 'en',
            date_modified    TEXT,
            seo_head         TEXT,
            created_at       TEXT NOT NULL DEFAULT (datetime('now')),
            updated_at       TEXT,
            group_key        TEXT DEFAULT NULL,
            UNIQUE(url_path, language)
        )
    """)
    conn.execute("""
        INSERT INTO articles_new
            (id, url_path, slug, title, meta_description, country, region,
             og_image_url, status, published_at, template_slug, language,
             date_modified, seo_head, created_at, updated_at, group_key)
        SELECT id, url_path, slug, title, meta_description, country, region,
               og_image_url, status, published_at, template_slug, language,
               date_modified, seo_head, created_at, updated_at, group_key
        FROM articles
    """)
    conn.execute("DROP TABLE articles")
    conn.execute("ALTER TABLE articles_new RENAME TO articles")

    conn.execute("CREATE INDEX IF NOT EXISTS idx_articles_url_path ON articles(url_path)")
    conn.execute("CREATE INDEX IF NOT EXISTS idx_articles_url_lang ON articles(url_path, language)")
    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)")

    # ── 3. Recreate FTS + triggers ─────────────────────────────────────────────
    conn.execute("""
        CREATE VIRTUAL TABLE IF NOT EXISTS articles_fts USING fts5(
            title, meta_description, country, region,
            content='articles', content_rowid='id'
        )
    """)
    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
    """)
