"""Initial federation tables

Revision ID: fed_0001
Revises: None
Create Date: 2026-02-26
"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.postgresql import JSONB

revision = "fed_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(), "ap_actor_profiles"):
        return

    # 1. ap_actor_profiles
    op.create_table(
        "ap_actor_profiles",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("preferred_username", sa.String(64), nullable=False),
        sa.Column("display_name", sa.String(255), nullable=True),
        sa.Column("summary", sa.Text(), nullable=True),
        sa.Column("public_key_pem", sa.Text(), nullable=False),
        sa.Column("private_key_pem", sa.Text(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id"),
        sa.UniqueConstraint("preferred_username"),
    )
    op.create_index("ix_ap_actor_user_id", "ap_actor_profiles", ["user_id"], unique=True)
    op.create_index("ix_ap_actor_username", "ap_actor_profiles", ["preferred_username"], unique=True)

    # 2. ap_anchors
    op.create_table(
        "ap_anchors",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("merkle_root", sa.String(128), nullable=False),
        sa.Column("tree_ipfs_cid", sa.String(128), nullable=True),
        sa.Column("ots_proof_cid", sa.String(128), nullable=True),
        sa.Column("activity_count", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("confirmed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("bitcoin_txid", sa.String(128), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )

    # 3. ap_remote_actors
    op.create_table(
        "ap_remote_actors",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("actor_url", sa.String(512), nullable=False),
        sa.Column("inbox_url", sa.String(512), nullable=False),
        sa.Column("shared_inbox_url", sa.String(512), nullable=True),
        sa.Column("preferred_username", sa.String(255), nullable=False),
        sa.Column("display_name", sa.String(255), nullable=True),
        sa.Column("summary", sa.Text(), nullable=True),
        sa.Column("icon_url", sa.String(512), nullable=True),
        sa.Column("public_key_pem", sa.Text(), nullable=True),
        sa.Column("domain", sa.String(255), nullable=False),
        sa.Column("fetched_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("actor_url"),
    )
    op.create_index("ix_ap_remote_actor_url", "ap_remote_actors", ["actor_url"], unique=True)
    op.create_index("ix_ap_remote_actor_domain", "ap_remote_actors", ["domain"])

    # 4. ap_activities
    op.create_table(
        "ap_activities",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("activity_id", sa.String(512), nullable=False),
        sa.Column("activity_type", sa.String(64), nullable=False),
        sa.Column("actor_profile_id", sa.Integer(), sa.ForeignKey("ap_actor_profiles.id", ondelete="CASCADE"), nullable=True),
        sa.Column("object_type", sa.String(64), nullable=True),
        sa.Column("object_data", JSONB(), nullable=True),
        sa.Column("published", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("signature", JSONB(), nullable=True),
        sa.Column("is_local", sa.Boolean(), nullable=False, server_default="true"),
        sa.Column("source_type", sa.String(64), nullable=True),
        sa.Column("source_id", sa.Integer(), nullable=True),
        sa.Column("ipfs_cid", sa.String(128), nullable=True),
        sa.Column("anchor_id", sa.Integer(), sa.ForeignKey("ap_anchors.id", ondelete="SET NULL"), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("actor_uri", sa.String(512), nullable=True),
        sa.Column("visibility", sa.String(20), nullable=False, server_default="public"),
        sa.Column("process_state", sa.String(20), nullable=False, server_default="completed"),
        sa.Column("process_attempts", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("process_max_attempts", sa.Integer(), nullable=False, server_default="5"),
        sa.Column("process_error", sa.Text(), nullable=True),
        sa.Column("processed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("origin_app", sa.String(64), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("activity_id"),
    )
    op.create_index("ix_ap_activity_actor", "ap_activities", ["actor_profile_id"])
    op.create_index("ix_ap_activity_source", "ap_activities", ["source_type", "source_id"])
    op.create_index("ix_ap_activity_published", "ap_activities", ["published"])
    op.create_index("ix_ap_activity_process", "ap_activities", ["process_state"])

    # 5. ap_followers
    op.create_table(
        "ap_followers",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("actor_profile_id", sa.Integer(), sa.ForeignKey("ap_actor_profiles.id", ondelete="CASCADE"), nullable=False),
        sa.Column("follower_acct", sa.String(512), nullable=False),
        sa.Column("follower_inbox", sa.String(512), nullable=False),
        sa.Column("follower_actor_url", sa.String(512), nullable=False),
        sa.Column("follower_public_key", sa.Text(), nullable=True),
        sa.Column("app_domain", sa.String(64), nullable=False, server_default="federation"),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("actor_profile_id", "follower_acct", "app_domain", name="uq_follower_acct_app"),
    )
    op.create_index("ix_ap_follower_actor", "ap_followers", ["actor_profile_id"])
    op.create_index("ix_ap_follower_app_domain", "ap_followers", ["actor_profile_id", "app_domain"])

    # 6. ap_inbox_items
    op.create_table(
        "ap_inbox_items",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("actor_profile_id", sa.Integer(), sa.ForeignKey("ap_actor_profiles.id", ondelete="CASCADE"), nullable=False),
        sa.Column("raw_json", JSONB(), nullable=False),
        sa.Column("activity_type", sa.String(64), nullable=True),
        sa.Column("from_actor", sa.String(512), nullable=True),
        sa.Column("state", sa.String(20), nullable=False, server_default="pending"),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("processed_at", sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("ix_ap_inbox_state", "ap_inbox_items", ["state"])
    op.create_index("ix_ap_inbox_actor", "ap_inbox_items", ["actor_profile_id"])

    # 7. ipfs_pins
    op.create_table(
        "ipfs_pins",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("content_hash", sa.String(128), nullable=False),
        sa.Column("ipfs_cid", sa.String(128), nullable=False),
        sa.Column("pin_type", sa.String(64), nullable=False),
        sa.Column("source_type", sa.String(64), nullable=True),
        sa.Column("source_id", sa.Integer(), nullable=True),
        sa.Column("size_bytes", sa.BigInteger(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("ipfs_cid"),
    )
    op.create_index("ix_ipfs_pin_source", "ipfs_pins", ["source_type", "source_id"])
    op.create_index("ix_ipfs_pin_cid", "ipfs_pins", ["ipfs_cid"], unique=True)

    # 8. ap_following
    op.create_table(
        "ap_following",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("actor_profile_id", sa.Integer(), sa.ForeignKey("ap_actor_profiles.id", ondelete="CASCADE"), nullable=False),
        sa.Column("remote_actor_id", sa.Integer(), sa.ForeignKey("ap_remote_actors.id", ondelete="CASCADE"), nullable=False),
        sa.Column("state", sa.String(20), nullable=False, server_default="pending"),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("accepted_at", sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("actor_profile_id", "remote_actor_id", name="uq_following"),
    )
    op.create_index("ix_ap_following_actor", "ap_following", ["actor_profile_id"])
    op.create_index("ix_ap_following_remote", "ap_following", ["remote_actor_id"])

    # 9. ap_remote_posts
    op.create_table(
        "ap_remote_posts",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("remote_actor_id", sa.Integer(), sa.ForeignKey("ap_remote_actors.id", ondelete="CASCADE"), nullable=False),
        sa.Column("activity_id", sa.String(512), nullable=False),
        sa.Column("object_id", sa.String(512), nullable=False),
        sa.Column("object_type", sa.String(64), nullable=False),
        sa.Column("content", sa.Text(), nullable=True),
        sa.Column("summary", sa.Text(), nullable=True),
        sa.Column("url", sa.String(512), nullable=True),
        sa.Column("attachment_data", JSONB(), nullable=True),
        sa.Column("tag_data", JSONB(), nullable=True),
        sa.Column("in_reply_to", sa.String(512), nullable=True),
        sa.Column("conversation", sa.String(512), nullable=True),
        sa.Column("published", sa.DateTime(timezone=True), nullable=True),
        sa.Column("fetched_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("activity_id"),
        sa.UniqueConstraint("object_id"),
    )
    op.create_index("ix_ap_remote_post_actor", "ap_remote_posts", ["remote_actor_id"])
    op.create_index("ix_ap_remote_post_published", "ap_remote_posts", ["published"])
    op.create_index("ix_ap_remote_post_object", "ap_remote_posts", ["object_id"], unique=True)

    # 10. ap_local_posts
    op.create_table(
        "ap_local_posts",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("actor_profile_id", sa.Integer(), sa.ForeignKey("ap_actor_profiles.id", ondelete="CASCADE"), nullable=False),
        sa.Column("content", sa.Text(), nullable=False),
        sa.Column("visibility", sa.String(20), nullable=False, server_default="public"),
        sa.Column("in_reply_to", sa.String(512), nullable=True),
        sa.Column("published", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        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"),
    )
    op.create_index("ix_ap_local_post_actor", "ap_local_posts", ["actor_profile_id"])
    op.create_index("ix_ap_local_post_published", "ap_local_posts", ["published"])

    # 11. ap_interactions
    op.create_table(
        "ap_interactions",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("actor_profile_id", sa.Integer(), sa.ForeignKey("ap_actor_profiles.id", ondelete="CASCADE"), nullable=True),
        sa.Column("remote_actor_id", sa.Integer(), sa.ForeignKey("ap_remote_actors.id", ondelete="CASCADE"), nullable=True),
        sa.Column("post_type", sa.String(20), nullable=False),
        sa.Column("post_id", sa.Integer(), nullable=False),
        sa.Column("interaction_type", sa.String(20), nullable=False),
        sa.Column("activity_id", sa.String(512), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("ix_ap_interaction_post", "ap_interactions", ["post_type", "post_id"])
    op.create_index("ix_ap_interaction_actor", "ap_interactions", ["actor_profile_id"])
    op.create_index("ix_ap_interaction_remote", "ap_interactions", ["remote_actor_id"])

    # 12. ap_notifications
    op.create_table(
        "ap_notifications",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("actor_profile_id", sa.Integer(), sa.ForeignKey("ap_actor_profiles.id", ondelete="CASCADE"), nullable=False),
        sa.Column("notification_type", sa.String(20), nullable=False),
        sa.Column("from_remote_actor_id", sa.Integer(), sa.ForeignKey("ap_remote_actors.id", ondelete="SET NULL"), nullable=True),
        sa.Column("from_actor_profile_id", sa.Integer(), sa.ForeignKey("ap_actor_profiles.id", ondelete="SET NULL"), nullable=True),
        sa.Column("target_activity_id", sa.Integer(), sa.ForeignKey("ap_activities.id", ondelete="SET NULL"), nullable=True),
        sa.Column("target_remote_post_id", sa.Integer(), sa.ForeignKey("ap_remote_posts.id", ondelete="SET NULL"), nullable=True),
        sa.Column("app_domain", sa.String(30), nullable=True),
        sa.Column("read", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("ix_ap_notification_actor", "ap_notifications", ["actor_profile_id"])
    op.create_index("ix_ap_notification_read", "ap_notifications", ["actor_profile_id", "read"])
    op.create_index("ix_ap_notification_created", "ap_notifications", ["created_at"])

    # 13. ap_delivery_log
    op.create_table(
        "ap_delivery_log",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("activity_id", sa.Integer(), sa.ForeignKey("ap_activities.id", ondelete="CASCADE"), nullable=False),
        sa.Column("inbox_url", sa.String(512), nullable=False),
        sa.Column("app_domain", sa.String(128), nullable=False, server_default="federation"),
        sa.Column("status_code", sa.Integer(), nullable=True),
        sa.Column("delivered_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("activity_id", "inbox_url", "app_domain", name="uq_delivery_activity_inbox_domain"),
    )
    op.create_index("ix_ap_delivery_activity", "ap_delivery_log", ["activity_id"])


def downgrade():
    op.drop_table("ap_delivery_log")
    op.drop_table("ap_notifications")
    op.drop_table("ap_interactions")
    op.drop_table("ap_local_posts")
    op.drop_table("ap_remote_posts")
    op.drop_table("ap_following")
    op.drop_table("ipfs_pins")
    op.drop_table("ap_inbox_items")
    op.drop_table("ap_followers")
    op.drop_table("ap_activities")
    op.drop_table("ap_remote_actors")
    op.drop_table("ap_anchors")
    op.drop_table("ap_actor_profiles")
