"""Add OrganizationAccessToken

Revision ID: e9f5351f567c
Revises: 1c41b2824616
Create Date: 2025-02-04 18:11:18.321901

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "e9f5351f567c"
down_revision = "1c41b2824616"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "organization_access_tokens",
        sa.Column("token", sa.CHAR(length=64), nullable=False),
        sa.Column("scope", sa.Text(), nullable=False),
        sa.Column("expires_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("comment", sa.String(), nullable=False),
        sa.Column("last_used_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("organization_id", sa.Uuid(), nullable=False),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name=op.f("organization_access_tokens_organization_id_fkey"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("organization_access_tokens_pkey")),
        sa.UniqueConstraint("token", name=op.f("organization_access_tokens_token_key")),
    )
    op.create_index(
        op.f("ix_organization_access_tokens_created_at"),
        "organization_access_tokens",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_access_tokens_deleted_at"),
        "organization_access_tokens",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_access_tokens_expires_at"),
        "organization_access_tokens",
        ["expires_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_access_tokens_modified_at"),
        "organization_access_tokens",
        ["modified_at"],
        unique=False,
    )
    op.alter_column(
        "personal_access_tokens",
        "token",
        existing_type=sa.VARCHAR(length=255),
        type_=sa.CHAR(length=64),
        existing_nullable=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column(
        "personal_access_tokens",
        "token",
        existing_type=sa.CHAR(length=64),
        type_=sa.VARCHAR(length=255),
        existing_nullable=False,
    )
    op.drop_index(
        op.f("ix_organization_access_tokens_modified_at"),
        table_name="organization_access_tokens",
    )
    op.drop_index(
        op.f("ix_organization_access_tokens_expires_at"),
        table_name="organization_access_tokens",
    )
    op.drop_index(
        op.f("ix_organization_access_tokens_deleted_at"),
        table_name="organization_access_tokens",
    )
    op.drop_index(
        op.f("ix_organization_access_tokens_created_at"),
        table_name="organization_access_tokens",
    )
    op.drop_table("organization_access_tokens")
    # ### end Alembic commands ###
