"""Create the ``web_seeds`` and ``web_discovered_urls`` tables.

Includes the bloom extension, bloom filter index on ``url``, and
``DEFAULT gen_random_uuid()`` on id columns.

Revision ID: 0010
"""
from __future__ import annotations

from repo_core.migrations.builder import MigrationBuilder, RawSqlOp
from repo_core.migrations.runner import Migration
from repo_core.domains.web.url.model import WebUrlType, WebUrlStatus

VERSION = "0010"


def get_postgres_migrations() -> list[Migration]:
    return [
        # ── bloom extension ───────────────────────────────────────
        Migration(
            version="web_ext_0001",
            description="Enable the bloom extension for bloom filter indexes",
            up=RawSqlOp("CREATE EXTENSION IF NOT EXISTS bloom"),
            down=None,  # Don't drop the extension — other tables may use it
        ),
        # ── web_seeds ─────────────────────────────────────────────
        Migration(
            version="web_seed_0001",
            description="Create web_seeds table for crawl seed configuration",
            up=(
                MigrationBuilder("web_seeds")
                .uuid("id", primary_key=True, nullable=False)
                .text("name", nullable=False)
                .text("base_url", nullable=False)
                .jsonb("config", nullable=True)
                .boolean("enabled", nullable=False, default=True)
                .timestamp("created_at", nullable=True, default="now()")
                .timestamp("updated_at", nullable=True, default="now()")
                .create_table()
            ),
            down=MigrationBuilder("web_seeds").drop_table(),
        ),
        # ── web_discovered_urls ───────────────────────────────────
        Migration(
            version="web_url_0001",
            description="Create web_discovered_urls table for crawled URL lifecycle",
            up=(
                MigrationBuilder("web_discovered_urls")
                .uuid("id", primary_key=True, nullable=False)
                .uuid("seed_id", nullable=False)
                .text("url", nullable=False)
                .text("normalized_url", nullable=True)
                .integer("depth", nullable=False, default=0)
                .varchar("status", 20, nullable=False, default=WebUrlStatus.NEW.value)
                .varchar("url_type", 20, nullable=False, default=WebUrlType.UNKNOWN.value)
                .integer("priority", nullable=False, default=100)
                .timestamp("next_crawl", nullable=True)
                .jsonb("metadata", nullable=True)
                .timestamp("created_at", nullable=True, default="now()")
                .timestamp("updated_at", nullable=True, default="now()")
                .index(["url"], unique=True)
                .index(["url"], using="bloom", with_params={"length": 80})
                .index(["seed_id", "status"])
                .index(["next_crawl"])
                .create_table()
            ),
            down=MigrationBuilder("web_discovered_urls").drop_table(),
        ),
        # ── id defaults ───────────────────────────────────────────
        Migration(
            version="web_id_defaults_0001",
            description="Add DEFAULT gen_random_uuid() to web_seeds.id and web_discovered_urls.id",
            up=RawSqlOp([
                "ALTER TABLE web_seeds ALTER COLUMN id SET DEFAULT gen_random_uuid()",
                "ALTER TABLE web_discovered_urls ALTER COLUMN id SET DEFAULT gen_random_uuid()",
            ]),
            down=RawSqlOp([
                "ALTER TABLE web_seeds ALTER COLUMN id DROP DEFAULT",
                "ALTER TABLE web_discovered_urls ALTER COLUMN id DROP DEFAULT",
            ]),
        ),
    ]
