"""Create ExternalEvent

Revision ID: 358d9fbe3de4
Revises: 65aeef3d240b
Create Date: 2025-03-17 11:43:41.361589

"""

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

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "358d9fbe3de4"
down_revision = "65aeef3d240b"
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(
        "external_events",
        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.Column("source", sa.String(), nullable=False),
        sa.Column("external_id", sa.String(), nullable=False),
        sa.Column("task_name", sa.String(), nullable=False),
        sa.Column("handled_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("data", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("external_events_pkey")),
    )
    op.create_index(
        op.f("ix_external_events_created_at"),
        "external_events",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_external_events_deleted_at"),
        "external_events",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_external_events_handled_at"),
        "external_events",
        ["handled_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_external_events_modified_at"),
        "external_events",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_external_events_source"), "external_events", ["source"], unique=False
    )
    op.create_index(
        op.f("ix_external_events_task_name"),
        "external_events",
        ["task_name"],
        unique=False,
    )
    op.create_index(
        op.f("ix_external_events_external_id"),
        "external_events",
        ["external_id"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_external_events_external_id"), table_name="external_id")
    op.drop_index(op.f("ix_external_events_task_name"), table_name="external_events")
    op.drop_index(op.f("ix_external_events_source"), table_name="external_events")
    op.drop_index(op.f("ix_external_events_modified_at"), table_name="external_events")
    op.drop_index(op.f("ix_external_events_handled_at"), table_name="external_events")
    op.drop_index(op.f("ix_external_events_deleted_at"), table_name="external_events")
    op.drop_index(op.f("ix_external_events_created_at"), table_name="external_events")
    op.drop_table("external_events")
    # ### end Alembic commands ###
