from sqlalchemy import Integer, String, Text, text from sqlalchemy.orm import Mapped, mapped_column from api.v1.common.base_models import TenantScopedMixin, TimestampMixin from core.database import Base class Account(Base, TenantScopedMixin, TimestampMixin): """Cuenta CRM: empresa cliente o prospecto. Modela importadores, IMMEX, agencias aduanales, transportistas, etc. Los campos aduaneros (RFC, patente) son opcionales para no forzar datos en prospectos que aún no comparten información fiscal. """ __tablename__ = "accounts" __table_args__ = {"schema": "crm"} id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) # Razón social (nombre legal) y nombre comercial name: Mapped[str] = mapped_column(String(255), nullable=False) trade_name: Mapped[str | None] = mapped_column(String(255), nullable=True) rfc: Mapped[str | None] = mapped_column(String(13), nullable=True, index=True) # Tipo de cuenta: immex | agencia_aduanal | importador | exportador | transportista | otro account_type: Mapped[str | None] = mapped_column(String(40), nullable=True) industry: Mapped[str | None] = mapped_column(String(120), nullable=True) email: Mapped[str | None] = mapped_column(String(255), nullable=True) phone: Mapped[str | None] = mapped_column(String(40), nullable=True) website: Mapped[str | None] = mapped_column(String(255), nullable=True) address: Mapped[str | None] = mapped_column(Text, nullable=True) city: Mapped[str | None] = mapped_column(String(120), nullable=True) state: Mapped[str | None] = mapped_column(String(120), nullable=True) country: Mapped[str | None] = mapped_column(String(2), nullable=True, server_default=text("'MX'")) # Patente del agente aduanal (dato aduanero, no se traduce) patente_aduanal: Mapped[str | None] = mapped_column(String(20), nullable=True) # Estado comercial: active | inactive | prospect status: Mapped[str] = mapped_column(String(20), nullable=False, server_default=text("'active'")) # Vendedor responsable (id de usuario Keycloak / sub) owner_user_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True) notes: Mapped[str | None] = mapped_column(Text, nullable=True)