"""add demand summary feedback records

Revision ID: 20260730_06
Revises: 20260730_05
Create Date: 2026-07-30
"""
from __future__ import annotations

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "20260730_06"
down_revision: str | None = "20260730_05"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.create_table(
        "demand_feedback",
        sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column("client_request_id", sa.String(length=64), nullable=False),
        sa.Column(
            "target_type",
            sa.String(length=32),
            nullable=False,
            comment="反馈对象：demand / video / hit_content",
        ),
        sa.Column("biz_dt", sa.String(length=32), nullable=False),
        sa.Column("demand_grade_id", sa.BigInteger(), nullable=False),
        sa.Column("video_id", sa.String(length=64), nullable=True),
        sa.Column("demand_video_expansion_id", sa.BigInteger(), nullable=True),
        sa.Column("feedback_action", sa.String(length=32), nullable=False),
        sa.Column("reason_code", sa.String(length=64), nullable=True),
        sa.Column("content", sa.Text(), nullable=True),
        sa.Column("target_snapshot_json", sa.Text(), nullable=False),
        sa.Column("feedback_user_id", sa.Integer(), nullable=False),
        sa.Column(
            "feedback_user_name_snapshot",
            sa.String(length=128),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "client_request_id",
            name="uk_demand_feedback_request",
        ),
        mysql_charset="utf8mb4",
    )
    op.create_index(
        "idx_demand_feedback_demand",
        "demand_feedback",
        ["demand_grade_id", "created_at"],
    )
    op.create_index(
        "idx_demand_feedback_video",
        "demand_feedback",
        ["demand_grade_id", "video_id", "created_at"],
    )
    op.create_index(
        "idx_demand_feedback_expansion",
        "demand_feedback",
        ["demand_video_expansion_id", "created_at"],
    )
    op.create_index(
        "idx_demand_feedback_user",
        "demand_feedback",
        ["feedback_user_id", "created_at"],
    )


def downgrade() -> None:
    op.drop_index("idx_demand_feedback_user", table_name="demand_feedback")
    op.drop_index("idx_demand_feedback_expansion", table_name="demand_feedback")
    op.drop_index("idx_demand_feedback_video", table_name="demand_feedback")
    op.drop_index("idx_demand_feedback_demand", table_name="demand_feedback")
    op.drop_table("demand_feedback")
