"""add monetization tables: diagnosis_records, attribution_records, payment_orders

Revision ID: f063b3da67b6
Revises: g1h2i3j4kl56
Create Date: 2026-06-01 07:40:07.419407

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision: str = "f063b3da67b6"
down_revision: Union[str, Sequence[str], None] = "g1h2i3j4kl56"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "diagnosis_records",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("brand_id", sa.Uuid(), nullable=False),
        sa.Column("user_id", sa.Uuid(), nullable=False),
        sa.Column("diagnosis_type", sa.String(length=20), nullable=False),
        sa.Column("status", sa.String(length=20), nullable=False),
        sa.Column("overall_score", sa.Float(), nullable=True),
        sa.Column("result_json", sa.JSON(), nullable=True),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("collection_metadata", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
        sa.Column("completed_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(["brand_id"], ["brands.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("idx_diagnosis_records_brand_id", "diagnosis_records", ["brand_id"])
    op.create_index("idx_diagnosis_records_user_id", "diagnosis_records", ["user_id"])
    op.create_index("idx_diagnosis_records_status", "diagnosis_records", ["status"])
    op.create_index("idx_diagnosis_records_created_at", "diagnosis_records", ["created_at"])

    op.create_table(
        "attribution_records",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("user_id", sa.Text(), nullable=False),
        sa.Column("brand_id", sa.Uuid(), nullable=False),
        sa.Column("content_id", sa.Uuid(), nullable=True),
        sa.Column("baseline_score", sa.Float(), nullable=False),
        sa.Column("current_score", sa.Float(), nullable=True),
        sa.Column("score_delta", sa.Float(), nullable=True),
        sa.Column("attribution_window_days", sa.Integer(), server_default="28", nullable=False),
        sa.Column("published_at", sa.DateTime(), nullable=True),
        sa.Column("window_end_at", sa.DateTime(), nullable=True),
        sa.Column("status", sa.String(length=20), server_default="tracking", nullable=False),
        sa.Column("attributed_dimensions", sa.JSON(), nullable=True),
        sa.Column("roi_percentage", sa.Float(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["brand_id"], ["brands.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["content_id"], ["contents.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("idx_attribution_records_brand_id", "attribution_records", ["brand_id"])
    op.create_index("idx_attribution_records_user_id", "attribution_records", ["user_id"])
    op.create_index("idx_attribution_records_status", "attribution_records", ["status"])
    op.create_index("idx_attribution_records_content_id", "attribution_records", ["content_id"])

    op.drop_table("payment_orders")
    op.create_table(
        "payment_orders",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("user_id", sa.String(length=36), nullable=False),
        sa.Column("plan", sa.String(length=20), nullable=False),
        sa.Column("amount", sa.Float(), nullable=False),
        sa.Column("currency", sa.String(length=10), nullable=False),
        sa.Column("payment_provider", sa.String(length=20), nullable=False),
        sa.Column("payment_id", sa.String(length=255), nullable=True),
        sa.Column("status", sa.String(length=20), nullable=False),
        sa.Column("pay_url", sa.String(length=1024), nullable=True),
        sa.Column("callback_data", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
        sa.Column("paid_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )


def downgrade() -> None:
    op.drop_table("payment_orders")
    op.create_table(
        "payment_orders",
        sa.Column("id", sa.TEXT(), nullable=False),
        sa.Column("orderNo", sa.TEXT(), nullable=False),
        sa.Column("userId", sa.TEXT(), nullable=False),
        sa.Column("channelId", sa.TEXT(), nullable=True),
        sa.Column("subject", sa.TEXT(), nullable=False),
        sa.Column("body", sa.TEXT(), nullable=True),
        sa.Column("amount", sa.NUMERIC(precision=12, scale=2), nullable=False),
        sa.Column("currency", sa.TEXT(), server_default="'CNY'", nullable=False),
        sa.Column("status", sa.TEXT(), server_default="'pending'", nullable=False),
        sa.Column("clientIp", sa.TEXT(), nullable=True),
        sa.Column("userAgent", sa.TEXT(), nullable=True),
        sa.Column("metadata", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("channelOrderNo", sa.TEXT(), nullable=True),
        sa.Column("createdAt", postgresql.TIMESTAMP(precision=3), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=False),
        sa.Column("updatedAt", postgresql.TIMESTAMP(precision=3), nullable=False),
        sa.Column("paidAt", postgresql.TIMESTAMP(precision=3), nullable=True),
        sa.Column("cancelledAt", postgresql.TIMESTAMP(precision=3), nullable=True),
        sa.ForeignKeyConstraint(["channelId"], ["payment_channels.id"], onupdate="CASCADE", ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["userId"], ["users.id"], onupdate="CASCADE", ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("payment_orders_userId_idx"), "payment_orders", ["userId"])
    op.create_index(op.f("payment_orders_status_idx"), "payment_orders", ["status"])
    op.create_index(op.f("payment_orders_orderNo_key"), "payment_orders", ["orderNo"], unique=True)
    op.create_index(op.f("payment_orders_orderNo_idx"), "payment_orders", ["orderNo"])
    op.create_index(op.f("payment_orders_createdAt_idx"), "payment_orders", ["createdAt"])

    op.drop_index("idx_attribution_records_content_id", table_name="attribution_records")
    op.drop_index("idx_attribution_records_status", table_name="attribution_records")
    op.drop_index("idx_attribution_records_user_id", table_name="attribution_records")
    op.drop_index("idx_attribution_records_brand_id", table_name="attribution_records")
    op.drop_table("attribution_records")

    op.drop_index("idx_diagnosis_records_created_at", table_name="diagnosis_records")
    op.drop_index("idx_diagnosis_records_status", table_name="diagnosis_records")
    op.drop_index("idx_diagnosis_records_user_id", table_name="diagnosis_records")
    op.drop_index("idx_diagnosis_records_brand_id", table_name="diagnosis_records")
    op.drop_table("diagnosis_records")
