"""
Modelos ORM para gestión de licencias
"""

import enum

from api.v1.common.base_models import TimestampMixin
from core.database import Base
from sqlalchemy import Boolean, Column, DateTime
from sqlalchemy import Enum as SQLEnum
from sqlalchemy import ForeignKey, Integer


class LicensePlan(enum.Enum):
    """Planes de licencia disponibles"""

    FREE = "free"
    BASIC = "basic"
    PROFESSIONAL = "professional"
    ENTERPRISE = "enterprise"


class LicenseStatus(enum.Enum):
    """Estados de licencia"""

    ACTIVE = "active"
    EXPIRED = "expired"
    SUSPENDED = "suspended"
    PENDING = "pending"
    CANCELLED = "cancelled"


class License(Base, TimestampMixin):
    """
    Modelo de Licencia - Control de planes y límites por tenant
    """

    __tablename__ = "licenses"
    __table_args__ = {"schema": "core"}

    id = Column(Integer, primary_key=True, index=True)
    tenant_id = Column(
        Integer, ForeignKey("core.tenants.id"), nullable=False, unique=True, index=True
    )

    # Plan y características
    plan = Column(
        SQLEnum(LicensePlan),
        default=LicensePlan.FREE,
        server_default="FREE",
        nullable=False,
    )
    status = Column(
        SQLEnum(LicenseStatus),
        default=LicenseStatus.PENDING,
        server_default="PENDING",
        nullable=False,
    )

    # Límites del plan
    max_users = Column(Integer, server_default="5", nullable=False)
    max_storage_gb = Column(Integer, server_default="10", nullable=False)
    max_monthly_operations = Column(Integer, server_default="1000", nullable=False)

    # Features habilitadas (booleans)
    feature_api_access = Column(Boolean, default=True, server_default="true")
    feature_advanced_reports = Column(Boolean, default=False, server_default="false")
    feature_integrations = Column(Boolean, default=False, server_default="false")
    feature_dedicated_support = Column(Boolean, default=False, server_default="false")

    # Vigencia
    starts_at = Column(DateTime(timezone=True), nullable=False)
    expires_at = Column(DateTime(timezone=True), nullable=False)

    def __repr__(self):
        return f"<License(tenant_id={self.tenant_id}, plan={self.plan.value}, status={self.status.value})>"


class LicenseUsage(Base, TimestampMixin):
    """
    Modelo para tracking de uso de licencia
    """

    __tablename__ = "license_usage"
    __table_args__ = {"schema": "core"}

    id = Column(Integer, primary_key=True, index=True)
    tenant_id = Column(
        Integer, ForeignKey("core.tenants.id"), nullable=False, index=True
    )

    # Métricas de uso
    period_start = Column(DateTime(timezone=True), nullable=False)
    period_end = Column(DateTime(timezone=True), nullable=False)

    active_users = Column(Integer, default=0, server_default="0")
    storage_used_gb = Column(Integer, default=0, server_default="0")
    operations_count = Column(Integer, default=0, server_default="0")
    api_calls_count = Column(Integer, default=0, server_default="0")

    def __repr__(self):
        return f"<LicenseUsage(tenant_id={self.tenant_id}, operations={self.operations_count})>"
