"""
Modelo de direcciones de empresa
"""
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 CompanyAddress(Base, TimestampMixin):
    """Direcciones de la empresa (principal, industrial 1 y 2)"""
    
    __tablename__ = "company_address"
    __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
    )
    address_type: Mapped[str] = mapped_column(String(20))  # 'main', 'industrial', 'industrial2'
    
    # Campos de dirección
    street: Mapped[Optional[str]] = mapped_column(String(100))
    exterior_number: Mapped[Optional[str]] = mapped_column(String(20))
    interior_number: Mapped[Optional[str]] = mapped_column(String(20))
    postal_code: Mapped[Optional[str]] = mapped_column(String(15))
    neighborhood: Mapped[Optional[str]] = mapped_column(String(40))
    city: Mapped[Optional[str]] = mapped_column(String(40))
    municipality: Mapped[Optional[str]] = mapped_column(String(50))
    state: Mapped[Optional[str]] = mapped_column(String(30))
    country: Mapped[Optional[str]] = mapped_column(String(4))
    phone: Mapped[Optional[str]] = mapped_column(String(30))
    fax: Mapped[Optional[str]] = mapped_column(String(30))
    email: Mapped[Optional[str]] = mapped_column(String(100))
    
    # Relación inversa
    company: Mapped["Company"] = relationship(
        "Company",
        back_populates="addresses"
    )