import enum

from sqlalchemy import Boolean, ForeignKey, Integer, Numeric, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import BaseModel


class BillingCycle(str, enum.Enum):
    """计费周期。one_time = 一次性付费（如域名注册年费除外的部分）。"""

    HOURLY = "hourly"
    DAILY = "daily"
    MONTHLY = "monthly"
    QUARTERLY = "quarterly"
    SEMI_ANNUALLY = "semi_annually"
    ANNUALLY = "annually"
    BIENNIALLY = "biennially"
    TRIENNIALLY = "triennially"
    ONE_TIME = "one_time"


class ProductPricing(BaseModel):
    """一个产品在不同计费周期下的售价。price 单位：元。"""

    __tablename__ = "product_pricings"
    __table_args__ = (
        UniqueConstraint("product_id", "cycle", name="uq_pricing_product_cycle"),
    )

    product_id: Mapped[int] = mapped_column(
        ForeignKey("products.id", ondelete="CASCADE"), nullable=False, index=True
    )
    cycle: Mapped[BillingCycle] = mapped_column(String(16), nullable=False)
    price: Mapped[float] = mapped_column(Numeric(10, 2), nullable=False, comment="周期内价格(元)")
    original_price: Mapped[float | None] = mapped_column(
        Numeric(10, 2), nullable=True, comment="划线价(元)，仅用于前台展示"
    )
    setup_fee: Mapped[float] = mapped_column(
        Numeric(10, 2), default=0, nullable=False, comment="该周期下的一次性安装费(元)"
    )
    is_enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
