from api.v1.common.base_models import TenantScopedMixin, TimestampMixin
from core.database import Base
from sqlalchemy import Column, ForeignKey, ForeignKeyConstraint, Integer, String, UniqueConstraint, PrimaryKeyConstraint
from sqlalchemy.orm import relationship


class CustomsBroker(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "customs_brokers"
    __table_args__ = (
        PrimaryKeyConstraint("id", name="customs_brokers_pkey"),
        UniqueConstraint("broker_key", "tenant_id", "company_id", name="uq_broker_key_tenant_company"),
        {"schema": "a76"},
    )

    id =  Column(Integer, primary_key=True)
    type = Column(String(9), nullable=True)
    broker_key = Column(String(5), nullable=False)
    name = Column(String(80), nullable=True)
    address = Column(String(1500), nullable=True)
    postal_code = Column(String(15), nullable=True)
    city = Column(String(30), nullable=True)
    state = Column(String(30), nullable=True)
    phone = Column(String(30), nullable=True)
    fax = Column(String(30), nullable=True)
    email = Column(String(100), nullable=True)
    country = Column(String(3), nullable=True)
    tax_id = Column(String(30), nullable=True)
    personal_id = Column(String(20), nullable=True)
    position = Column(String(30), nullable=True)
    license = Column(String(4), nullable=True)
    company = Column(String(200), nullable=True)
    contact = Column(String(80), nullable=True)

    vu = relationship(
        "CustomsBrokerVU", back_populates="customs_broker", cascade="all, delete", uselist=False
    )
    personnel = relationship(
        "CustomsBrokerPersonnel", back_populates="customs_broker", cascade="all, delete"
    )


class CustomsBrokerVU(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "customs_brokers_vu"
    __table_args__ = {"schema": "a76"}

    customs_broker_id = Column(
        Integer,
        ForeignKey("a76.customs_brokers.id", ondelete="CASCADE"),
        primary_key=True,
    )
    certificate_path = Column(String(1499), nullable=True)
    key_path = Column(String(1499), nullable=True)
    access_key = Column(String(50), nullable=True)
    fiel_format = Column(String(19), nullable=True)
    signature_read_path = Column(String(1499), nullable=True)
    archive_path = Column(String(1499), nullable=True)
    fiel_access_key = Column(String(50), nullable=True)
    web_service_user = Column(String(100), nullable=True)
    web_service_access_key = Column(String(100), nullable=True)
    vu_email = Column(String(800), nullable=True)
    vu_figure_type = Column(String(29), nullable=True)
    xml_files_path = Column(String(1499), nullable=True)
    query_tax_id = Column(String(30), nullable=True)
    doda_certificate_path = Column(String(1499), nullable=True)
    doda_key_path = Column(String(1499), nullable=True)
    doda_web_service_user = Column(String(100), nullable=True)
    doda_web_service_access_key = Column(String(100), nullable=True)
    doda_fiel_access_key = Column(String(50), nullable=True)
    doda_xml_files_path = Column(String(1499), nullable=True)

    customs_broker = relationship("CustomsBroker", back_populates="vu")


class CustomsBrokerPersonnel(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "customs_brokers_personnel"
    __table_args__ = {"schema": "a76"}

    customs_broker_id = Column(
        Integer,
        ForeignKey("a76.customs_brokers.id", ondelete="CASCADE"),
        primary_key=True,
    )
    line = Column(Integer, primary_key=True, nullable=False)
    name = Column(String(80), nullable=True)
    tax_id = Column(String(30), nullable=True)
    personal_id = Column(String(20), nullable=True)
    position = Column(String(30), nullable=True)
    license = Column(String(4), nullable=True)
    first_name = Column(String(80), nullable=True)
    last_name = Column(String(80), nullable=True)
    middle_name = Column(String(80), nullable=True)
    email = Column(String(100), nullable=True)

    customs_broker = relationship("CustomsBroker", back_populates="personnel")
