import enum
import uuid
from typing import TYPE_CHECKING

from sqlalchemy import Boolean, Enum, ForeignKey, Integer, String
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.database import Base

if TYPE_CHECKING:
    from app.models.company import Company


class AbsenceCategory(str, enum.Enum):
    VACATION      = "vacation"
    SICK          = "sick"
    OVERTIME_COMP = "overtime_comp"
    TRAINING      = "training"
    BUSINESS_TRIP = "business_trip"
    OTHER         = "other"


class AbsenceType(Base):
    __tablename__ = "absence_types"

    id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    company_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("companies.id", ondelete="CASCADE"), nullable=False, index=True
    )
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    color: Mapped[str] = mapped_column(String(7), default="#3B82F6")
    category: Mapped[AbsenceCategory] = mapped_column(
        Enum(AbsenceCategory, name="absencecategory", values_callable=lambda x: [e.value for e in x]),
        nullable=False, default=AbsenceCategory.OTHER,
    )
    requires_approval: Mapped[bool] = mapped_column(Boolean, default=True)
    deducts_vacation: Mapped[bool] = mapped_column(Boolean, default=False)
    affects_overtime_balance: Mapped[bool] = mapped_column(Boolean, default=False)
    requires_certificate: Mapped[bool] = mapped_column(Boolean, default=False)
    certificate_after_days: Mapped[int] = mapped_column(Integer, default=3)
    is_paid: Mapped[bool] = mapped_column(Boolean, default=True)
    max_days_per_year: Mapped[int | None] = mapped_column(Integer)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)

    company: Mapped["Company"] = relationship("Company", lazy="noload")

    def __repr__(self) -> str:
        return f"<AbsenceType {self.name} [{self.category}]>"
