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

# revision identifiers, used by Alembic.
revision = "20251102_223123"
down_revision = "20251028_ghost_content"
branch_labels = None
depends_on = None

def upgrade():
    # Extend users
    op.add_column("users", sa.Column("ghost_id", sa.String(length=64), nullable=True))
    op.add_column("users", sa.Column("name", sa.String(length=255), nullable=True))
    op.add_column("users", sa.Column("ghost_status", sa.String(length=50), nullable=True))
    op.add_column("users", sa.Column("ghost_subscribed", sa.Boolean(), nullable=False, server_default=sa.true()))
    op.add_column("users", sa.Column("ghost_note", sa.Text(), nullable=True))
    op.add_column("users", sa.Column("avatar_image", sa.Text(), nullable=True))
    op.add_column("users", sa.Column("stripe_customer_id", sa.String(length=255), nullable=True))
    op.add_column("users", sa.Column("ghost_raw", postgresql.JSONB(astext_type=sa.Text()), nullable=True))
    op.create_index("ix_users_ghost_id", "users", ["ghost_id"], unique=True)
    op.create_index("ix_users_stripe_customer_id", "users", ["stripe_customer_id"])

    # Labels
    op.create_table(
        "ghost_labels",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("ghost_id", sa.String(length=64), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("slug", sa.String(length=255), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
    )
    op.create_index("ix_ghost_labels_ghost_id", "ghost_labels", ["ghost_id"], unique=True)

    op.create_table(
        "user_labels",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
        sa.Column("label_id", sa.Integer(), sa.ForeignKey("ghost_labels.id", ondelete="CASCADE"), nullable=False),
        sa.UniqueConstraint("user_id", "label_id", name="uq_user_label"),
    )

    # Newsletters
    op.create_table(
        "ghost_newsletters",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("ghost_id", sa.String(length=64), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("slug", sa.String(length=255), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
    )
    op.create_index("ix_ghost_newsletters_ghost_id", "ghost_newsletters", ["ghost_id"], unique=True)

    op.create_table(
        "user_newsletters",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
        sa.Column("newsletter_id", sa.Integer(), sa.ForeignKey("ghost_newsletters.id", ondelete="CASCADE"), nullable=False),
        sa.Column("subscribed", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.UniqueConstraint("user_id", "newsletter_id", name="uq_user_newsletter"),
    )

    # Tiers
    op.create_table(
        "ghost_tiers",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("ghost_id", sa.String(length=64), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("slug", sa.String(length=255), nullable=True),
        sa.Column("type", sa.String(length=50), nullable=True),
        sa.Column("visibility", sa.String(length=50), nullable=True),
    )
    op.create_index("ix_ghost_tiers_ghost_id", "ghost_tiers", ["ghost_id"], unique=True)

    # Subscriptions
    op.create_table(
        "ghost_subscriptions",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("ghost_id", sa.String(length=64), nullable=False),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
        sa.Column("status", sa.String(length=50), nullable=True),
        sa.Column("tier_id", sa.Integer(), sa.ForeignKey("ghost_tiers.id", ondelete="SET NULL"), nullable=True),
        sa.Column("cadence", sa.String(length=50), nullable=True),
        sa.Column("price_amount", sa.Integer(), nullable=True),
        sa.Column("price_currency", sa.String(length=10), nullable=True),
        sa.Column("stripe_customer_id", sa.String(length=255), nullable=True),
        sa.Column("stripe_subscription_id", sa.String(length=255), nullable=True),
        sa.Column("raw", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    )
    op.create_index("ix_ghost_subscriptions_ghost_id", "ghost_subscriptions", ["ghost_id"], unique=True)
    op.create_index("ix_ghost_subscriptions_user_id", "ghost_subscriptions", ["user_id"])
    op.create_index("ix_ghost_subscriptions_tier_id", "ghost_subscriptions", ["tier_id"])
    op.create_index("ix_ghost_subscriptions_stripe_customer_id", "ghost_subscriptions", ["stripe_customer_id"])
    op.create_index("ix_ghost_subscriptions_stripe_subscription_id", "ghost_subscriptions", ["stripe_subscription_id"])

def downgrade():
    op.drop_index("ix_ghost_subscriptions_stripe_subscription_id", table_name="ghost_subscriptions")
    op.drop_index("ix_ghost_subscriptions_stripe_customer_id", table_name="ghost_subscriptions")
    op.drop_index("ix_ghost_subscriptions_tier_id", table_name="ghost_subscriptions")
    op.drop_index("ix_ghost_subscriptions_user_id", table_name="ghost_subscriptions")
    op.drop_index("ix_ghost_subscriptions_ghost_id", table_name="ghost_subscriptions")
    op.drop_table("ghost_subscriptions")

    op.drop_index("ix_ghost_tiers_ghost_id", table_name="ghost_tiers")
    op.drop_table("ghost_tiers")

    op.drop_table("user_newsletters")
    op.drop_index("ix_ghost_newsletters_ghost_id", table_name="ghost_newsletters")
    op.drop_table("ghost_newsletters")

    op.drop_table("user_labels")
    op.drop_index("ix_ghost_labels_ghost_id", table_name="ghost_labels")
    op.drop_table("ghost_labels")

    op.drop_index("ix_users_stripe_customer_id", table_name="users")
    op.drop_index("ix_users_ghost_id", table_name="users")
    op.drop_column("users", "ghost_raw")
    op.drop_column("users", "stripe_customer_id")
    op.drop_column("users", "avatar_image")
    op.drop_column("users", "ghost_note")
    op.drop_column("users", "ghost_subscribed")
    op.drop_column("users", "ghost_status")
    op.drop_column("users", "name")
    op.drop_column("users", "ghost_id")
