from sqlalchemy import Boolean, Integer, String
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import BaseModel


class AgentTier(BaseModel):
    __tablename__ = "agent_tiers"

    name: Mapped[str] = mapped_column(String(50), nullable=False, comment="Tier name")
    code: Mapped[str] = mapped_column(String(32), unique=True, index=True, nullable=False, comment="Tier code")
    cost_rate: Mapped[int] = mapped_column(Integer, nullable=False, comment="Agent cost rate in basis points")
    min_member_level: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="Minimum member level to apply")
    min_total_spend: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="Minimum total spend to apply")

    monthly_sales_required: Mapped[int] = mapped_column(
        Integer, default=0, nullable=False, comment="Rolling 30-day net sales requirement in cents"
    )
    monthly_active_customers_required: Mapped[int] = mapped_column(
        Integer, default=0, nullable=False, comment="Rolling 30-day active customer requirement"
    )
    max_refund_rate_bps: Mapped[int] = mapped_column(
        Integer, default=5000, nullable=False, comment="Risk lock refund rate threshold, basis points"
    )
    max_monthly_subsidy_cents: Mapped[int] = mapped_column(
        Integer, default=10000, nullable=False, comment="Rolling 30-day subsidy loss risk threshold"
    )
    min_balance_for_subsidy_cents: Mapped[int] = mapped_column(
        Integer, default=12800, nullable=False, comment="Risk lock balance threshold when subsidy exceeds limit"
    )
    observe_days: Mapped[int] = mapped_column(Integer, default=7, nullable=False, comment="Downgrade observation days")
    freeze_days: Mapped[int] = mapped_column(Integer, default=7, nullable=False, comment="Risk lock reference days")
    auto_downgrade_enabled: Mapped[bool] = mapped_column(
        Boolean, default=True, nullable=False, comment="Whether automatic downgrade is enabled"
    )

    max_coupon_discount_rate: Mapped[int] = mapped_column(
        Integer, default=5000, nullable=False, comment="Max coupon discount rate in basis points"
    )
    max_monthly_coupon_count: Mapped[int] = mapped_column(Integer, default=10, nullable=False, comment="Monthly coupon limit")
    sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="Sort order")
    enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False, comment="Enabled")
    description: Mapped[str | None] = mapped_column(String(500), nullable=True, comment="Description")
