"""Remove Article

Revision ID: 59538121ff3b
Revises: 39ce508b936c
Create Date: 2024-12-02 15:43:51.526183

"""

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

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "59538121ff3b"
down_revision = "39ce508b936c"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index("ix_articles_created_at", table_name="articles")
    op.drop_index("ix_articles_deleted_at", table_name="articles")
    op.drop_index("ix_articles_modified_at", table_name="articles")
    op.drop_table("articles")
    op.drop_index(
        "ix_article_subscriptions_created_at", table_name="article_subscriptions"
    )
    op.drop_index(
        "ix_article_subscriptions_deleted_at", table_name="article_subscriptions"
    )
    op.drop_index(
        "ix_article_subscriptions_modified_at", table_name="article_subscriptions"
    )
    op.drop_index(
        "ix_article_subscriptions_organization_id", table_name="article_subscriptions"
    )
    op.drop_index(
        "ix_article_subscriptions_user_id", table_name="article_subscriptions"
    )
    op.drop_table("article_subscriptions")

    op.execute("DELETE FROM benefits WHERE type = 'articles'")

    op.execute(
        "UPDATE personal_access_tokens SET scope = REPLACE(scope, 'articles:read', '') WHERE scope LIKE '%articles:read%'"
    )
    op.execute(
        "UPDATE personal_access_tokens SET scope = REPLACE(scope, 'articles:write', '') WHERE scope LIKE '%articles:write%'"
    )

    op.execute(
        "UPDATE oauth2_tokens SET scope = REPLACE(scope, 'articles:read', '') WHERE scope LIKE '%articles:read%'"
    )
    op.execute(
        "UPDATE oauth2_tokens SET scope = REPLACE(scope, 'articles:write', '') WHERE scope LIKE '%articles:write%'"
    )

    op.execute(
        "UPDATE oauth2_grants SET scope = REPLACE(scope, 'articles:read', '') WHERE scope LIKE '%articles:read%'"
    )
    op.execute(
        "UPDATE oauth2_grants SET scope = REPLACE(scope, 'articles:write', '') WHERE scope LIKE '%articles:write%'"
    )

    op.execute(
        "UPDATE oauth2_clients SET client_metadata = REPLACE(client_metadata, 'articles:read', '') WHERE client_metadata LIKE '%articles:read%'"
    )
    op.execute(
        "UPDATE oauth2_clients SET client_metadata = REPLACE(client_metadata, 'articles:write', '') WHERE client_metadata LIKE '%articles:write%'"
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "article_subscriptions",
        sa.Column("paid_subscriber", sa.BOOLEAN(), autoincrement=False, nullable=False),
        sa.Column("organization_id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column(
            "emails_unsubscribed_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column("user_id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column("id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column(
            "created_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "modified_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "deleted_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name="article_subscriptions_organization_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
            name="article_subscriptions_user_id_fkey",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name="article_subscriptions_pkey"),
        sa.UniqueConstraint(
            "organization_id",
            "user_id",
            name="article_subscriptions_organization_id_user_id_key",
        ),
    )
    op.create_index(
        "ix_article_subscriptions_user_id",
        "article_subscriptions",
        ["user_id"],
        unique=False,
    )
    op.create_index(
        "ix_article_subscriptions_organization_id",
        "article_subscriptions",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        "ix_article_subscriptions_modified_at",
        "article_subscriptions",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        "ix_article_subscriptions_deleted_at",
        "article_subscriptions",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        "ix_article_subscriptions_created_at",
        "article_subscriptions",
        ["created_at"],
        unique=False,
    )
    op.create_table(
        "articles",
        sa.Column("slug", sa.VARCHAR(length=255), autoincrement=False, nullable=False),
        sa.Column("title", sa.VARCHAR(length=255), autoincrement=False, nullable=False),
        sa.Column("body", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("user_id", sa.UUID(), autoincrement=False, nullable=True),
        sa.Column("organization_id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column(
            "published_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column("byline", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("visibility", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column(
            "paid_subscribers_only", sa.BOOLEAN(), autoincrement=False, nullable=False
        ),
        sa.Column(
            "paid_subscribers_only_ends_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "notifications_sent_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "notify_subscribers", sa.BOOLEAN(), autoincrement=False, nullable=False
        ),
        sa.Column(
            "email_sent_to_count", sa.INTEGER(), autoincrement=False, nullable=True
        ),
        sa.Column(
            "email_open_count", sa.INTEGER(), autoincrement=False, nullable=False
        ),
        sa.Column("is_pinned", sa.BOOLEAN(), autoincrement=False, nullable=False),
        sa.Column("og_image_url", sa.VARCHAR(), autoincrement=False, nullable=True),
        sa.Column("og_description", sa.VARCHAR(), autoincrement=False, nullable=True),
        sa.Column("id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column(
            "created_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "modified_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "deleted_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name="articles_organization_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"], ["users.id"], name="articles_user_id_fkey"
        ),
        sa.PrimaryKeyConstraint("id", name="articles_pkey"),
        sa.UniqueConstraint(
            "organization_id", "slug", name="articles_organization_id_slug_key"
        ),
    )
    op.create_index(
        "ix_articles_modified_at", "articles", ["modified_at"], unique=False
    )
    op.create_index("ix_articles_deleted_at", "articles", ["deleted_at"], unique=False)
    op.create_index("ix_articles_created_at", "articles", ["created_at"], unique=False)
    # ### end Alembic commands ###
