from datetime import datetime

from sqlalchemy import BigInteger, DateTime, Index, Integer, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base


class Marcacion(Base):
    __tablename__ = "marcaciones"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    reloj_id: Mapped[str] = mapped_column(String(32), nullable=False)
    reloj_name: Mapped[str] = mapped_column(String(64), nullable=False, default="")
    user_code: Mapped[int] = mapped_column(BigInteger, nullable=False)
    marcacion_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
    backup_type: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
    record_type: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
    work_type: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
    backup_label: Mapped[str] = mapped_column(String(32), nullable=False, default="")
    type_label: Mapped[str] = mapped_column(String(16), nullable=False, default="")
    synced_at: Mapped[datetime] = mapped_column(
        DateTime, nullable=False, default=datetime.utcnow
    )

    __table_args__ = (
        UniqueConstraint(
            "reloj_id",
            "user_code",
            "marcacion_at",
            "record_type",
            name="uq_marcacion",
        ),
        Index("ix_marcacion_at", "marcacion_at"),
        Index("ix_reloj_id", "reloj_id"),
    )
