"""
Modelo de certificados digitales (FIEL, CFDI, Cancelación)
"""
from typing import Optional, TYPE_CHECKING
from sqlalchemy import Integer, String, ForeignKey
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 CompanyDigitalCertificate(Base, TimestampMixin):
    """Certificados digitales (FIEL, CFDI)"""
    
    __tablename__ = "company_digital_certificate"
    __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,
        index=True
    )
    certificate_type: Mapped[str] = mapped_column(String(20))  # 'fiel', 'cfdi', 'cancellation'
    
    # Rutas de archivos
    cer_file_path: Mapped[Optional[str]] = mapped_column(String(5000))
    key_file_path: Mapped[Optional[str]] = mapped_column(String(5000))
    
    # Contraseñas y claves
    password: Mapped[Optional[str]] = mapped_column(String(200))
    access_key: Mapped[Optional[str]] = mapped_column(String(50))
    
    # Fechas de expiración
    cer_expiration_date: Mapped[Optional[int]] = mapped_column(Integer)
    key_expiration_date: Mapped[Optional[int]] = mapped_column(Integer)
    
    # Relación inversa
    company: Mapped["Company"] = relationship(
        "Company",
        back_populates="digital_certificates"
    )