from sqlalchemy import String, Time, ForeignKey, Date, Boolean
from sqlalchemy.dialects.mysql import CHAR
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.core.models import BaseModel

class Shift(BaseModel):
    """
    Defines a reusable working shift configuration.
    
    Contains start and end times, and whether overtime is permitted 
    for employees assigned to this shift.
    """
    __tablename__ = 'hris_shift'

    name: Mapped[str] = mapped_column(String(50), nullable=False)
    start_time: Mapped[Time] = mapped_column(Time, nullable=False)
    end_time: Mapped[Time] = mapped_column(Time, nullable=False)

    is_overtime_allowed: Mapped[bool] = mapped_column(Boolean, default=True)

    def __repr__(self):
        return f"<Shift {self.name} {self.start_time}-{self.end_time}>"

class EmployeeSchedule(BaseModel):
    """
    Maps a specific Shift to an Employee for a particular date.
    
    This dynamic scheduling allows for varying work patterns (e.g., Saturday shifts)
    which are used to determine working days during payroll and leave calculations.
    """
    __tablename__ = 'hris_employee_schedule'

    employee_id: Mapped[str] = mapped_column(CHAR(36), ForeignKey('core_employee.id'), nullable=False)
    shift_id: Mapped[str] = mapped_column(CHAR(36), ForeignKey('hris_shift.id'), nullable=False)

    date: Mapped[Date] = mapped_column(Date, nullable=False)

    # Relationships
    employee = relationship('Employee', backref='schedules')
    shift = relationship('Shift')

    def __repr__(self):
        return f"<Schedule {self.employee_id} {self.date} {self.shift_id}>"
