"""
Modelo de certificaciones de empresa
"""
from typing import Optional, TYPE_CHECKING
from datetime import date

from sqlalchemy import String, ForeignKey, Boolean, Date, Integer
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[bool]] = mapped_column(Boolean)
    certified_company_registration: Mapped[Optional[str]] = mapped_column(String(40))
    certified_company_start_date: Mapped[Optional[date]] = mapped_column(Date)
    certified_company_end_date: Mapped[Optional[date]] = mapped_column(Date)
    
    # Anexo 30
    annex30_certification_date: Mapped[Optional[date]] = mapped_column(Date)
    annex30_certification_number: Mapped[Optional[str]] = mapped_column(String(50))
    annex30_modality: Mapped[Optional[str]] = mapped_column(String(3))
    annex30_company_type: Mapped[Optional[str]] = mapped_column(String(50))
    annex30_renewal_date: Mapped[Optional[date]] = mapped_column(Date)
    annex30_final_certification_date: Mapped[Optional[date]] = mapped_column(Date)
    
    # Otras certificaciones
    is_seciit_company: Mapped[Optional[bool]] = mapped_column(Boolean)
    is_oea_company: Mapped[Optional[bool]] = mapped_column(Boolean)
    ctpat_svi: Mapped[Optional[str]] = mapped_column(String(100))
    trusted_exporter_number: Mapped[Optional[str]] = mapped_column(String(50))
    neec_company: Mapped[Optional[bool]] = mapped_column(Boolean)
    
    # Relación inversa
    company: Mapped["Company"] = relationship(
        "Company",
        back_populates="certification"
    )