"""feedback review fields

Revision ID: feedback_review_fields
Revises: ceb286289426
Create Date: 2026-06-22 20:00:00.000000

"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

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


def upgrade() -> None:
    with op.batch_alter_table("feedback", schema=None) as batch_op:
        batch_op.add_column(sa.Column("reject_reason", sa.String(length=256), nullable=True))
        batch_op.add_column(sa.Column("reward_coins", sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column("review_note", sa.String(length=256), nullable=True))
        batch_op.add_column(sa.Column("reviewed_by_admin_id", sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column("reviewed_at", sa.DateTime(timezone=True), nullable=True))
        batch_op.create_index(batch_op.f("ix_feedback_status"), ["status"], unique=False)
        batch_op.create_foreign_key(
            batch_op.f("fk_feedback_reviewed_by_admin_id_admin_user"),
            "admin_user",
            ["reviewed_by_admin_id"],
            ["id"],
        )

    feedback = sa.table(
        "feedback",
        sa.column("status", sa.String(length=16)),
    )
    op.execute(
        feedback.update()
        .where(feedback.c.status == "new")
        .values(status="pending")
    )
    op.execute(
        feedback.update()
        .where(feedback.c.status == "handled")
        .values(status="adopted")
    )


def downgrade() -> None:
    feedback = sa.table(
        "feedback",
        sa.column("status", sa.String(length=16)),
    )
    op.execute(
        feedback.update()
        .where(feedback.c.status == "pending")
        .values(status="new")
    )
    op.execute(
        feedback.update()
        .where(feedback.c.status == "adopted")
        .values(status="handled")
    )

    with op.batch_alter_table("feedback", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("fk_feedback_reviewed_by_admin_id_admin_user"),
            type_="foreignkey",
        )
        batch_op.drop_index(batch_op.f("ix_feedback_status"))
        batch_op.drop_column("reviewed_at")
        batch_op.drop_column("reviewed_by_admin_id")
        batch_op.drop_column("review_note")
        batch_op.drop_column("reward_coins")
        batch_op.drop_column("reject_reason")
