"""Initial blog tables

Revision ID: blog_0001
Revises: -
Create Date: 2026-02-26
"""

import sqlalchemy as sa
from alembic import op

revision = "blog_0001"
down_revision = None
branch_labels = None
depends_on = None


def _table_exists(conn, name):
    result = conn.execute(sa.text(
        "SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name=:t"
    ), {"t": name})
    return result.scalar() is not None


def upgrade():
    if _table_exists(op.get_bind(), "posts"):
        return

    op.create_table(
        "tags",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("ghost_id", sa.String(64), nullable=False),
        sa.Column("slug", sa.String(191), nullable=False),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("visibility", sa.String(32), nullable=False),
        sa.Column("feature_image", sa.Text(), nullable=True),
        sa.Column("meta_title", sa.String(300), nullable=True),
        sa.Column("meta_description", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("ghost_id"),
    )
    op.create_index("ix_tags_ghost_id", "tags", ["ghost_id"])
    op.create_index("ix_tags_slug", "tags", ["slug"])

    op.create_table(
        "authors",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("ghost_id", sa.String(64), nullable=False),
        sa.Column("slug", sa.String(191), nullable=False),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("email", sa.String(255), nullable=True),
        sa.Column("profile_image", sa.Text(), nullable=True),
        sa.Column("cover_image", sa.Text(), nullable=True),
        sa.Column("bio", sa.Text(), nullable=True),
        sa.Column("website", sa.Text(), nullable=True),
        sa.Column("location", sa.Text(), nullable=True),
        sa.Column("facebook", sa.Text(), nullable=True),
        sa.Column("twitter", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("ghost_id"),
    )
    op.create_index("ix_authors_ghost_id", "authors", ["ghost_id"])
    op.create_index("ix_authors_slug", "authors", ["slug"])

    op.create_table(
        "posts",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("ghost_id", sa.String(64), nullable=False),
        sa.Column("uuid", sa.String(64), nullable=False),
        sa.Column("slug", sa.String(191), nullable=False),
        sa.Column("title", sa.String(500), nullable=False),
        sa.Column("html", sa.Text(), nullable=True),
        sa.Column("plaintext", sa.Text(), nullable=True),
        sa.Column("mobiledoc", sa.Text(), nullable=True),
        sa.Column("lexical", sa.Text(), nullable=True),
        sa.Column("feature_image", sa.Text(), nullable=True),
        sa.Column("feature_image_alt", sa.Text(), nullable=True),
        sa.Column("feature_image_caption", sa.Text(), nullable=True),
        sa.Column("excerpt", sa.Text(), nullable=True),
        sa.Column("custom_excerpt", sa.Text(), nullable=True),
        sa.Column("visibility", sa.String(32), nullable=False),
        sa.Column("status", sa.String(32), nullable=False),
        sa.Column("featured", sa.Boolean(), nullable=False),
        sa.Column("is_page", sa.Boolean(), nullable=False),
        sa.Column("email_only", sa.Boolean(), nullable=False),
        sa.Column("canonical_url", sa.Text(), nullable=True),
        sa.Column("meta_title", sa.String(500), nullable=True),
        sa.Column("meta_description", sa.Text(), nullable=True),
        sa.Column("og_image", sa.Text(), nullable=True),
        sa.Column("og_title", sa.String(500), nullable=True),
        sa.Column("og_description", sa.Text(), nullable=True),
        sa.Column("twitter_image", sa.Text(), nullable=True),
        sa.Column("twitter_title", sa.String(500), nullable=True),
        sa.Column("twitter_description", sa.Text(), nullable=True),
        sa.Column("custom_template", sa.String(191), nullable=True),
        sa.Column("reading_time", sa.Integer(), nullable=True),
        sa.Column("comment_id", sa.String(191), nullable=True),
        sa.Column("published_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("user_id", sa.Integer(), nullable=True),
        sa.Column("publish_requested", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("primary_author_id", sa.Integer(), nullable=True),
        sa.Column("primary_tag_id", sa.Integer(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("ghost_id"),
        sa.UniqueConstraint("uuid"),
        sa.ForeignKeyConstraint(["primary_author_id"], ["authors.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["primary_tag_id"], ["tags.id"], ondelete="SET NULL"),
    )
    op.create_index("ix_posts_ghost_id", "posts", ["ghost_id"])
    op.create_index("ix_posts_slug", "posts", ["slug"])
    op.create_index("ix_posts_user_id", "posts", ["user_id"])

    op.create_table(
        "post_authors",
        sa.Column("post_id", sa.Integer(), nullable=False),
        sa.Column("author_id", sa.Integer(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("post_id", "author_id"),
        sa.ForeignKeyConstraint(["post_id"], ["posts.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["author_id"], ["authors.id"], ondelete="CASCADE"),
    )

    op.create_table(
        "post_tags",
        sa.Column("post_id", sa.Integer(), nullable=False),
        sa.Column("tag_id", sa.Integer(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("post_id", "tag_id"),
        sa.ForeignKeyConstraint(["post_id"], ["posts.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["tag_id"], ["tags.id"], ondelete="CASCADE"),
    )

    op.create_table(
        "post_likes",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("post_id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.ForeignKeyConstraint(["post_id"], ["posts.id"], ondelete="CASCADE"),
    )

    op.create_table(
        "snippets",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("value", sa.Text(), nullable=False),
        sa.Column("visibility", sa.String(20), nullable=False, server_default="private"),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id", "name", name="uq_snippets_user_name"),
    )
    op.create_index("ix_snippets_user_id", "snippets", ["user_id"])
    op.create_index("ix_snippets_visibility", "snippets", ["visibility"])

    op.create_table(
        "tag_groups",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("slug", sa.String(191), nullable=False),
        sa.Column("feature_image", sa.Text(), nullable=True),
        sa.Column("colour", sa.String(32), nullable=True),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("slug"),
    )

    op.create_table(
        "tag_group_tags",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("tag_group_id", sa.Integer(), nullable=False),
        sa.Column("tag_id", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.ForeignKeyConstraint(["tag_group_id"], ["tag_groups.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["tag_id"], ["tags.id"], ondelete="CASCADE"),
        sa.UniqueConstraint("tag_group_id", "tag_id", name="uq_tag_group_tag"),
    )

    op.create_table(
        "menu_items",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("post_id", sa.Integer(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("ix_menu_items_post_id", "menu_items", ["post_id"])
    op.create_index("ix_menu_items_sort_order", "menu_items", ["sort_order"])

    op.create_table(
        "menu_nodes",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("container_type", sa.String(32), nullable=False),
        sa.Column("container_id", sa.Integer(), nullable=False),
        sa.Column("parent_id", sa.Integer(), nullable=True),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.Column("depth", sa.Integer(), nullable=False),
        sa.Column("label", sa.String(255), nullable=False),
        sa.Column("slug", sa.String(255), nullable=True),
        sa.Column("href", sa.String(1024), nullable=True),
        sa.Column("icon", sa.String(64), nullable=True),
        sa.Column("feature_image", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.ForeignKeyConstraint(["parent_id"], ["menu_nodes.id"], ondelete="SET NULL"),
    )
    op.create_index("ix_menu_nodes_container", "menu_nodes", ["container_type", "container_id"])
    op.create_index("ix_menu_nodes_parent_id", "menu_nodes", ["parent_id"])

    op.create_table(
        "kv",
        sa.Column("key", sa.String(120), nullable=False),
        sa.Column("value", sa.Text(), nullable=True),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("key"),
    )

    op.create_table(
        "container_relations",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("parent_type", sa.String(32), nullable=False),
        sa.Column("parent_id", sa.Integer(), nullable=False),
        sa.Column("child_type", sa.String(32), nullable=False),
        sa.Column("child_id", sa.Integer(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.Column("label", sa.String(255), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("parent_type", "parent_id", "child_type", "child_id", name="uq_container_relations_parent_child"),
    )
    op.create_index("ix_container_relations_parent", "container_relations", ["parent_type", "parent_id"])
    op.create_index("ix_container_relations_child", "container_relations", ["child_type", "child_id"])


def downgrade():
    op.drop_table("container_relations")
    op.drop_table("kv")
    op.drop_table("menu_nodes")
    op.drop_table("menu_items")
    op.drop_table("tag_group_tags")
    op.drop_table("tag_groups")
    op.drop_table("snippets")
    op.drop_table("post_likes")
    op.drop_table("post_tags")
    op.drop_table("post_authors")
    op.drop_table("posts")
    op.drop_table("authors")
    op.drop_table("tags")
