"""
Modelos ORM para fracciones de alerta
"""

from typing import Optional

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

from api.v1.common.base_models import TenantScopedMixin, TimestampMixin
from core.database import Base


class WarningFraction(Base, TenantScopedMixin, TimestampMixin):
    """
    Modelo para fracciones arancelarias con alerta.
    Fracciones que requieren atención especial o documentación adicional.
    """

    __tablename__ = "warning_fractions"
    __table_args__ = (
        PrimaryKeyConstraint("id", name="warning_fractions_pkey"),
        UniqueConstraint(
            "fraction", "company_id", name="uq_warning_fractions_fraction_company"
        ),
        {"schema": "public"},
    )

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)

    # Código de la fracción arancelaria (ej: 01012101)
    fraction: Mapped[str] = mapped_column(String(15), nullable=False, index=True)

    # Descripción de la alerta
    description: Mapped[Optional[str]] = mapped_column(String(1000))

    # Tipo de alerta (ej: PERMISO, RESTRICCION, NOM, etc.)
    warning_type: Mapped[Optional[str]] = mapped_column(String(50), index=True)

    # Indica si la alerta está activa
    is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default="true")

    def __repr__(self) -> str:
        return (
            f"<WarningFraction(fraction='{self.fraction}', "
            f"warning_type='{self.warning_type}', is_active={self.is_active})>"
        )
