"""Add psa_post_log table for PSA note posting audit trail.

Revision ID: 060
Revises: 059
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision = "060"
down_revision = "059"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "psa_post_log",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column(
            "session_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("sessions.id", ondelete="CASCADE"),
            nullable=False,
            index=True,
        ),
        sa.Column(
            "psa_connection_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("psa_connections.id", ondelete="SET NULL"),
            nullable=True,
        ),
        sa.Column("ticket_id", sa.String(100), nullable=False),
        sa.Column("note_type", sa.String(50), nullable=False),
        sa.Column("content_posted", sa.Text(), nullable=False),
        sa.Column("external_note_id", sa.String(100), nullable=True),
        sa.Column("status", sa.String(20), nullable=False),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("status_changed_from", sa.String(100), nullable=True),
        sa.Column("status_changed_to", sa.String(100), nullable=True),
        sa.Column(
            "posted_by",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("users.id"),
            nullable=False,
        ),
        sa.Column(
            "posted_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.now(),
        ),
    )


def downgrade() -> None:
    op.drop_table("psa_post_log")
