"""add session store tables

Revision ID: eedf0aa985e6
Revises: a205678752dc
Create Date: 2026-05-01 17:07:43.557498

"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

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


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "agent_session_entries",
        sa.Column("project_key", sa.String(), nullable=False),
        sa.Column("session_id", sa.String(), nullable=False),
        sa.Column("subpath", sa.String(), server_default="", nullable=False),
        sa.Column("seq", sa.BigInteger(), nullable=False),
        sa.Column("uuid", sa.String(), nullable=True),
        sa.Column("entry_type", sa.String(), nullable=False),
        sa.Column("payload", sa.JSON(), nullable=False),
        sa.Column("mtime_ms", sa.BigInteger(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("user_id", sa.String(), server_default="default", nullable=False),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("project_key", "session_id", "subpath", "seq"),
    )
    with op.batch_alter_table("agent_session_entries", schema=None) as batch_op:
        batch_op.create_index("idx_agent_entries_listing", ["project_key", "session_id", "mtime_ms"], unique=False)
        batch_op.create_index(batch_op.f("ix_agent_session_entries_user_id"), ["user_id"], unique=False)
        batch_op.create_index(
            "uq_agent_entries_uuid",
            ["project_key", "session_id", "subpath", "uuid"],
            unique=True,
            postgresql_where=sa.text("uuid IS NOT NULL"),
            sqlite_where=sa.text("uuid IS NOT NULL"),
        )

    op.create_table(
        "agent_session_summaries",
        sa.Column("project_key", sa.String(), nullable=False),
        sa.Column("session_id", sa.String(), nullable=False),
        sa.Column("mtime_ms", sa.BigInteger(), nullable=False),
        sa.Column("data", sa.JSON(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("user_id", sa.String(), server_default="default", nullable=False),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("project_key", "session_id"),
    )
    with op.batch_alter_table("agent_session_summaries", schema=None) as batch_op:
        batch_op.create_index(batch_op.f("ix_agent_session_summaries_user_id"), ["user_id"], unique=False)

    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("agent_session_summaries", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_agent_session_summaries_user_id"))

    op.drop_table("agent_session_summaries")
    with op.batch_alter_table("agent_session_entries", schema=None) as batch_op:
        batch_op.drop_index(
            "uq_agent_entries_uuid",
            postgresql_where=sa.text("uuid IS NOT NULL"),
            sqlite_where=sa.text("uuid IS NOT NULL"),
        )
        batch_op.drop_index(batch_op.f("ix_agent_session_entries_user_id"))
        batch_op.drop_index("idx_agent_entries_listing")

    op.drop_table("agent_session_entries")
    # ### end Alembic commands ###
