from datetime import datetime

from sqlalchemy import DateTime, ForeignKey, SmallInteger, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column

from app.core.database import Base


class GenlockeTransfer(Base):
    __tablename__ = "genlocke_transfers"
    __table_args__ = (
        UniqueConstraint("target_encounter_id", name="uq_genlocke_transfers_target"),
    )

    id: Mapped[int] = mapped_column(primary_key=True)
    genlocke_id: Mapped[int] = mapped_column(
        ForeignKey("genlockes.id", ondelete="CASCADE"), index=True
    )
    source_encounter_id: Mapped[int] = mapped_column(
        ForeignKey("encounters.id"), index=True
    )
    target_encounter_id: Mapped[int] = mapped_column(
        ForeignKey("encounters.id"), unique=True
    )
    source_leg_order: Mapped[int] = mapped_column(SmallInteger)
    target_leg_order: Mapped[int] = mapped_column(SmallInteger)
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now()
    )

    def __repr__(self) -> str:
        return f"<GenlockeTransfer(id={self.id}, source={self.source_encounter_id}, target={self.target_encounter_id})>"
