from sqlalchemy import Float, ForeignKey, Index, Integer, String
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base, TimestampMixin


class CreditRecord(Base, TimestampMixin):
    __tablename__ = "credit_records"
    __table_args__ = (
        # 正式计费幂等键：同一用户同一个业务流水只能写入一次。
        # PostgreSQL/MySQL/SQLite 对 nullable unique 的处理都允许多条 NULL，兼容历史数据。
        Index("uq_credit_records_user_biz_key", "user_id", "biz_key", unique=True),
        Index("ix_credit_records_user_refund_for_biz_key", "user_id", "refund_for_biz_key"),
        Index("ix_credit_records_related_type", "related_id", "type"),
        Index("ix_credit_records_owner", "owner_type", "owner_id"),
        Index("ix_credit_records_subject_media", "credit_subject", "media_type"),
        Index("ix_credit_records_source_module_scene", "source_module", "billing_scene"),
        Index("ix_credit_records_user_kind_time", "user_type_snapshot", "frontend_user_kind_snapshot", "created_at"),
    )

    id: Mapped[str] = mapped_column(String(32), primary_key=True)
    user_id: Mapped[str] = mapped_column(
        String(32), ForeignKey("users.id", ondelete="CASCADE"), index=True
    )
    type: Mapped[str] = mapped_column(String(16), index=True)
    amount: Mapped[float] = mapped_column(Float)
    balance_after: Mapped[float] = mapped_column(Float)
    description: Mapped[str] = mapped_column(String(256))
    related_id: Mapped[str | None] = mapped_column(String(64), nullable=True)

    # 当前积分流水自己的业务幂等键。
    # 例如：generation_record:{record_id}:attempt:1:media:charge
    biz_key: Mapped[str | None] = mapped_column(String(160), nullable=True, index=True)

    # 如果当前流水是退款，记录它退的是哪一次扣费。
    # 例如：generation_record:{record_id}:attempt:1:media:charge
    refund_for_biz_key: Mapped[str | None] = mapped_column(String(160), nullable=True, index=True)

    # 账务快照字段：保证业务步骤/资源软删后，流水仍可独立展示。
    owner_type: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
    owner_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
    attempt_no: Mapped[int | None] = mapped_column(Integer, nullable=True)

    charge_kind: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
    charge_action: Mapped[str | None] = mapped_column(String(16), nullable=True)
    credit_subject: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
    media_type: Mapped[str | None] = mapped_column(String(16), nullable=True, index=True)
    billing_scene: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)

    source_module: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
    source_project_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
    source_step_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
    source_step_code: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)

    token_usage_id: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
    input_tokens: Mapped[int | None] = mapped_column(Integer, nullable=True)
    output_tokens: Mapped[int | None] = mapped_column(Integer, nullable=True)
    total_tokens: Mapped[int | None] = mapped_column(Integer, nullable=True)


    engine_type: Mapped[str | None] = mapped_column(String(16), nullable=True)
    engine_id: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
    engine_name: Mapped[str | None] = mapped_column(String(128), nullable=True)
    engine_provider: Mapped[str | None] = mapped_column(String(64), nullable=True)
    engine_model_name: Mapped[str | None] = mapped_column(String(128), nullable=True)

    user_type_snapshot: Mapped[str | None] = mapped_column(String(16), nullable=True, index=True)
    frontend_user_kind_snapshot: Mapped[str | None] = mapped_column(String(16), nullable=True, index=True)
