from datetime import datetime
from datetime import time as datetime_time
from typing import TYPE_CHECKING, Optional

from api.v1.common.base_models import TenantScopedMixin, TimestampMixin
from core.database import Base
from sqlalchemy import (
    DateTime,
    ForeignKeyConstraint,
    Index,
    Integer,
    PrimaryKeyConstraint,
    Time,
    UniqueConstraint,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship

if TYPE_CHECKING:
    from api.v1.modules.a76.pedmientos.models.pedimentos import Pedimentos


class PedimentoDates(Base, TenantScopedMixin, TimestampMixin):
    __tablename__ = "pedimento_dates"
    __table_args__ = (
        PrimaryKeyConstraint("id", name="pedimento_dates_pkey"),
        ForeignKeyConstraint(
            ["pedimento_id"],
            ["a76.pedimentos.id"],
            ondelete="CASCADE",
            name="fk_pedimento_dates",
        ),
        UniqueConstraint(
            "tenant_id",
            "company_id",
            "pedimento_id",
            name="pedimento_dates_pedimento_id_key",
        ),
        Index("idx_pedimento_dates_pedimento_id", "pedimento_id"),
        {"schema": "a76"},
    )

    id: Mapped[int] = mapped_column(Integer)
    pedimento_id: Mapped[int] = mapped_column(Integer, nullable=False)

    entry_date: Mapped[datetime] = mapped_column(DateTime)
    pedimento_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
    payment_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
    rectification_payment_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
    extraction_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
    submission_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
    eucan_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
    original_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
    start_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
    end_date: Mapped[datetime] = mapped_column(DateTime)
    
    capture_time: Mapped[datetime_time] = mapped_column(Time)

    pedimento: Mapped["Pedimentos"] = relationship(
        "Pedimentos", back_populates="pedimento_dates"
    )
