from __future__ import annotations

from datetime import datetime

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

from app.models.base import Base, TimestampMixin

PRIMARY_KEY_TYPE = BigInteger().with_variant(Integer, "sqlite")


class EntitlementPlan(Base, TimestampMixin):
    __tablename__ = "sys_entitlement_plan"

    id: Mapped[int] = mapped_column(PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True)
    name: Mapped[str] = mapped_column(String(80), nullable=False)
    plan_type: Mapped[str] = mapped_column(String(30), index=True, nullable=False)
    description: Mapped[str | None] = mapped_column(String(255), nullable=True)
    validity_days: Mapped[int | None] = mapped_column(Integer, nullable=True)
    monthly_topic_limit: Mapped[int | None] = mapped_column(Integer, nullable=True)
    enable_growth_profile: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    enable_periodic_reports: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    allow_help_card: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
    allow_share_draft: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
    deduct_quota: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
    status: Mapped[int] = mapped_column(Integer, default=1, index=True, nullable=False)
    sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False)


class UserEntitlement(Base, TimestampMixin):
    __tablename__ = "sys_user_entitlement"
    __table_args__ = (Index("ix_entitlement_status_expiry", "status", "expired_at", "user_id"),)

    id: Mapped[int] = mapped_column(PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True)
    user_id: Mapped[int] = mapped_column(ForeignKey("sys_user.id"), index=True, nullable=False)
    plan_id: Mapped[int] = mapped_column(ForeignKey("sys_entitlement_plan.id"), index=True, nullable=False)
    status: Mapped[str] = mapped_column(String(20), default="active", index=True, nullable=False)
    effective_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
    expired_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
    assigned_by: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)


class UserEntitlementLog(Base):
    __tablename__ = "sys_user_entitlement_log"

    id: Mapped[int] = mapped_column(PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True)
    user_id: Mapped[int] = mapped_column(BigInteger, index=True, nullable=False)
    entitlement_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    from_plan_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    to_plan_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    action: Mapped[str] = mapped_column(String(30), nullable=False)
    request_key: Mapped[str | None] = mapped_column(String(160), unique=True, nullable=True)
    detail_json: Mapped[str | None] = mapped_column(Text, nullable=True)
    operated_by: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
