"""Add event types table

Revision ID: 16322c272e89
Revises: 2dfff693f142
Create Date: 2025-11-13 11:01:58.989357

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "16322c272e89"
down_revision = "2dfff693f142"
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(
        "event_types",
        sa.Column("name", sa.String(length=128), nullable=False),
        sa.Column("label", sa.String(length=128), nullable=False),
        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("event_types_organization_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("event_types_pkey")),
        sa.UniqueConstraint(
            "name",
            "organization_id",
            name=op.f("event_types_name_organization_id_key"),
        ),
    )
    op.create_index(
        op.f("ix_event_types_created_at"),
        "event_types",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_event_types_deleted_at"),
        "event_types",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_event_types_name"),
        "event_types",
        ["name"],
        unique=False,
    )
    op.create_index(
        op.f("ix_event_types_organization_id"),
        "event_types",
        ["organization_id"],
        unique=False,
    )

    op.add_column("events", sa.Column("event_type_id", sa.Uuid(), nullable=True))
    op.create_index(
        op.f("ix_events_event_type_id"), "events", ["event_type_id"], unique=False
    )
    op.create_foreign_key(
        op.f("events_event_type_id_fkey"),
        "events",
        "event_types",
        ["event_type_id"],
        ["id"],
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(op.f("events_event_type_id_fkey"), "events", type_="foreignkey")
    op.drop_index(op.f("ix_events_event_type_id"), table_name="events")
    op.drop_column("events", "event_type_id")
    op.drop_index(op.f("ix_event_types_organization_id"), table_name="event_types")
    op.drop_index(op.f("ix_event_types_name"), table_name="event_types")
    op.drop_index(op.f("ix_event_types_deleted_at"), table_name="event_types")
    op.drop_index(op.f("ix_event_types_created_at"), table_name="event_types")
    op.drop_table("event_types")
    # ### end Alembic commands ###
