"""
Modelo de certificaciones de empresa
"""
from typing import Optional, TYPE_CHECKING
from sqlalchemy import Integer, String, SmallInteger, ForeignKey, Boolean
from sqlalchemy.orm import Mapped, mapped_column, relationship
from core.database import Base
from api.v1.common.base_models import TimestampMixin

if TYPE_CHECKING:
    from ..models import Company


class CompanyCertification(Base, TimestampMixin):
    """Certificaciones de la empresa"""
    
    __tablename__ = "company_certification"
    __table_args__ = {"schema": "a76"}
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    company_id: Mapped[int] = mapped_column(
        ForeignKey("a76.company.id", ondelete="CASCADE"),
        nullable=False,
        unique=True,
        index=True
    )
    
    # Certificación general
    is_certified_company: Mapped[Optional[str]] = mapped_column(String(1))
    certified_company_registration: Mapped[Optional[str]] = mapped_column(String(40))
    certified_company_start_date: Mapped[Optional[int]] = mapped_column(Integer)
    certified_company_end_date: Mapped[Optional[int]] = mapped_column(Integer)
    
    # Anexo 31
    annex31_certification_date: Mapped[Optional[int]] = mapped_column(Integer)
    annex31_certification_number: Mapped[Optional[str]] = mapped_column(String(50))
    annex31_modality: Mapped[Optional[str]] = mapped_column(String(50))
    annex31_company_type: Mapped[Optional[str]] = mapped_column(String(50))
    annex31_renewal_date: Mapped[Optional[int]] = mapped_column(Integer)
    annex31_final_certification_date: Mapped[Optional[int]] = mapped_column(Integer)
    
    # Otras certificaciones
    is_oea_company: Mapped[Optional[int]] = mapped_column(SmallInteger)
    ctpat_svi: Mapped[Optional[str]] = mapped_column(String(100))
    trusted_exporter_number: Mapped[Optional[str]] = mapped_column(String(50))
    neec_company: Mapped[Optional[int]] = mapped_column(Integer)
    
    # Relación inversa
    company: Mapped["Company"] = relationship(
        "Company",
        back_populates="certification"
    )