from datetime import datetime

from sqlalchemy import BigInteger, DateTime, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import JSON

from app.models.base import BaseModel


class AgentEvaluationLog(BaseModel):
    __tablename__ = "agent_evaluation_logs"

    agent_id: Mapped[int] = mapped_column(Integer, ForeignKey("user_agents.id", ondelete="CASCADE"), nullable=False, index=True)
    user_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
    tier_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
    period_start: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
    period_end: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
    net_sales: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    gross_sales: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    refund_amount: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    refund_rate_bps: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    active_customers: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    subsidy_loss: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    agent_balance: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    passed: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
    risk_locked: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    action: Mapped[str] = mapped_column(String(32), default="none", nullable=False, index=True)
    reason: Mapped[str | None] = mapped_column(Text, nullable=True)
    snapshot: Mapped[dict | None] = mapped_column(JSON, nullable=True)
