"""add message feedback management

Revision ID: 0032_message_feedback
Revises: 0031_admin_permissions
"""
from alembic import op
import sqlalchemy as sa

revision = "0032_message_feedback"
down_revision = "0031_admin_permissions"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "sys_message_feedback",
        sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
        sa.Column("user_id", sa.BigInteger(), sa.ForeignKey("sys_user.id"), nullable=False),
        sa.Column("session_id", sa.BigInteger(), sa.ForeignKey("sys_chat_session.id"), nullable=False),
        sa.Column("message_id", sa.BigInteger(), sa.ForeignKey("sys_chat_message.id"), nullable=False),
        sa.Column("content", sa.String(200), nullable=False),
        sa.Column("is_read", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("read_by", sa.BigInteger(), sa.ForeignKey("sys_admin.id"), nullable=True),
        sa.Column("read_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
        sa.UniqueConstraint("user_id", "message_id", name="uq_message_feedback_user_message"),
    )
    op.create_index("ix_message_feedback_user_id", "sys_message_feedback", ["user_id"])
    op.create_index("ix_message_feedback_session_id", "sys_message_feedback", ["session_id"])
    op.create_index("ix_message_feedback_message_id", "sys_message_feedback", ["message_id"])
    op.create_index("ix_message_feedback_read_created", "sys_message_feedback", ["is_read", "created_at"])


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