from typing import Optional
from sqlalchemy import Integer, String, UniqueConstraint, Enum
from sqlalchemy.orm import Mapped, mapped_column
from api.v1.common.base_models import TenantScopedMixin, TimestampMixin
from core.database import Base
import enum


class PortType(str, enum.Enum):
    ENTRY = "ENTRY"
    EXIT = "EXIT"
    DESTINATION = "DESTINATION"


class Port(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "ports"
    __table_args__ = (
        UniqueConstraint("port_code", "location_code", "tenant_id", "company_id",
                         name="uq_port_location"),
        {"schema": "a76"}
    )

    id: Mapped[int] = mapped_column(
        Integer, primary_key=True, autoincrement=True)
    port_code: Mapped[str] = mapped_column(String(6), nullable=False)  # PUERTO
    description: Mapped[Optional[str]] = mapped_column(
        String(20), nullable=True)  # DESCRIPCION
    location_code: Mapped[str] = mapped_column(
        String(4), nullable=False)  # LOCALIZACION
    location_description: Mapped[Optional[str]] = mapped_column(
        String(20), nullable=True)  # DESCLOCALIZACION

    # New column requested
    port_type: Mapped[PortType] = mapped_column(
        String(15), nullable=False, server_default=PortType.ENTRY)
