"""add_chat_message_table

Revision ID: 93f034d69301
Revises: d8f3e9a1b2c4
Create Date: 2025-09-18 19:02:47.533454

"""

from collections.abc import Sequence

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "93f034d69301"
down_revision: str | None = "d8f3e9a1b2c4"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "chat_message",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("chat_id", sa.UUID(), nullable=False),
        sa.Column("kind", sa.String(), nullable=False),
        sa.Column("data", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.ForeignKeyConstraint(
            ["chat_id"],
            ["chat.id"],
            ondelete="CASCADE",
            name="chat_message_chat_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="chat_message_pkey"),
    )
    op.create_index(op.f("ix_chat_message_id"), "chat_message", ["id"], unique=True)
    op.drop_constraint("uq_runbook_alias_owner_id", "runbook", type_="unique")
    op.create_unique_constraint(
        "uq_prompt_alias_owner_id", "runbook", ["alias", "owner_id"]
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint("uq_prompt_alias_owner_id", "runbook", type_="unique")
    op.create_unique_constraint(
        "uq_runbook_alias_owner_id",
        "runbook",
        ["alias", "owner_id"],
        postgresql_nulls_not_distinct=False,
    )
    op.drop_index(op.f("ix_chat_message_id"), table_name="chat_message")
    op.drop_table("chat_message")
    # ### end Alembic commands ###
