"""memory records and fts5

Revision ID: 6c384104b66a
Revises: 87b619c4e7ac
Create Date: 2026-07-28 13:28:24.190934

"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

from acr.memory.fts import CREATE_STATEMENTS, DROP_STATEMENTS

# revision identifiers, used by Alembic.
revision: str = "6c384104b66a"
down_revision: str | Sequence[str] | None = "87b619c4e7ac"
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(
        "memory_records",
        sa.Column("id", sa.String(), nullable=False),
        sa.Column(
            "type",
            sa.Enum(
                "SEMANTIC",
                "EPISODIC",
                "PROCEDURAL",
                "FAILURE",
                "DECISION",
                "PREFERENCE",
                "ENVIRONMENT",
                "SECURITY",
                "TEMPORARY",
                name="memorytype",
            ),
            nullable=False,
        ),
        sa.Column(
            "scope",
            sa.Enum(
                "GLOBAL",
                "ORGANIZATION",
                "USER",
                "PROJECT",
                "REPOSITORY",
                "TASK",
                "AGENT",
                "SESSION",
                name="memoryscope",
            ),
            nullable=False,
        ),
        sa.Column("subject", sa.String(), nullable=False),
        sa.Column("content", sa.String(), nullable=False),
        sa.Column("structured_payload", sa.JSON(), nullable=False),
        sa.Column("confidence", sa.Float(), nullable=False),
        sa.Column("importance", sa.Float(), nullable=False),
        sa.Column("utility_score", sa.Float(), nullable=False),
        sa.Column("source_type", sa.String(), nullable=False),
        sa.Column("source_id", sa.String(), nullable=True),
        sa.Column("evidence", sa.String(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.Column("observed_at", sa.DateTime(), nullable=False),
        sa.Column("valid_from", sa.DateTime(), nullable=False),
        sa.Column("valid_until", sa.DateTime(), nullable=True),
        sa.Column("last_accessed", sa.DateTime(), nullable=True),
        sa.Column("access_count", sa.Integer(), nullable=False),
        sa.Column("successful_uses", sa.Integer(), nullable=False),
        sa.Column("failed_uses", sa.Integer(), nullable=False),
        sa.Column("supersedes", sa.String(), nullable=True),
        sa.Column("superseded_by", sa.String(), nullable=True),
        sa.Column(
            "status",
            sa.Enum(
                "CANDIDATE",
                "CONFIRMED",
                "SUPERSEDED",
                "ARCHIVED",
                "QUARANTINED",
                "DELETED",
                name="memorystatus",
            ),
            nullable=False,
        ),
        sa.Column("sensitivity", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["supersedes"],
            ["memory_records.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    # ### end Alembic commands ###

    # Hand-written: SQLite FTS5 virtual table + sync triggers over
    # memory_records.content, shared with tests via acr.memory.fts so the
    # two definitions never drift apart.
    for statement in CREATE_STATEMENTS:
        op.execute(statement)


def downgrade() -> None:
    """Downgrade schema."""
    for statement in DROP_STATEMENTS:
        op.execute(statement)

    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("memory_records")
    # ### end Alembic commands ###
