from sqlalchemy import String, Text, ForeignKey, Integer, Float, Date, Time, DateTime, Boolean, UniqueConstraint
from sqlalchemy.dialects.mysql import CHAR
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.core.models import BaseModel
from app.core.models.status import Status
from app.core.models.user import User

# Import explicitly to allow HRIS models to relate to it
from app.core.models.employee import Employee

class Attendance(BaseModel):
    """
    Represents a daily attendance record for an employee.
    
    Stores check-in/out timestamps, GPS coordinates, and a verification photo.
    """
    __tablename__ = 'hris_attendance'
    __table_args__ = (
        UniqueConstraint('employee_id', 'date', name='uix_attendance_employee_date'),
    )

    employee_id: Mapped[str] = mapped_column(CHAR(36), ForeignKey('core_employee.id'), nullable=False)

    date: Mapped[Date] = mapped_column(Date, nullable=False)
    check_in: Mapped[DateTime | None] = mapped_column(DateTime, nullable=True)
    check_out: Mapped[DateTime | None] = mapped_column(DateTime, nullable=True)

    status_id: Mapped[str] = mapped_column(CHAR(36), ForeignKey('status.id'), nullable=False) # e.g., 'attn_present', 'attn_late'

    # Verification and Metadata
    location_lat: Mapped[str | None] = mapped_column(String(20), nullable=True)
    location_long: Mapped[str | None] = mapped_column(String(20), nullable=True)
    photo_url: Mapped[str | None] = mapped_column(String(255), nullable=True)
    
    late_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
    late_attachment: Mapped[str | None] = mapped_column(String(255), nullable=True)

    # Relationships
    employee = relationship('Employee', primaryjoin="Attendance.employee_id == Employee.id", backref="attendances")
    status = relationship('Status')

    def __repr__(self):
        return f"<Attendance {self.date} - {self.employee_id}>"

class Leave(BaseModel):
    """
    Represents a leave request (cuti) submitted by an employee.
    """
    __tablename__ = 'hris_leave'

    employee_id: Mapped[str] = mapped_column(CHAR(36), ForeignKey('core_employee.id'), nullable=False)
    leave_type_id: Mapped[str] = mapped_column(CHAR(36), ForeignKey('status.id'), nullable=False) # e.g., 'Annual Leave'

    start_date: Mapped[Date] = mapped_column(Date, nullable=False)
    end_date: Mapped[Date] = mapped_column(Date, nullable=False)
    reason: Mapped[str | None] = mapped_column(Text, nullable=True)
    attachment_path: Mapped[str | None] = mapped_column(String(255), nullable=True)

    status_id: Mapped[str] = mapped_column(CHAR(36), ForeignKey('status.id'), nullable=False) # e.g., 'leave_pending', 'leave_approved'
    approved_by_id: Mapped[str | None] = mapped_column(CHAR(36), ForeignKey('user.id'), nullable=True)

    # Relationships
    employee = relationship('Employee', primaryjoin="Leave.employee_id == Employee.id", backref="leaves")
    leave_type = relationship('Status', foreign_keys=[leave_type_id])
    status = relationship('Status', foreign_keys=[status_id])
    approved_by = relationship('User')

    def __repr__(self):
        return f"<Leave {self.leave_type_id} {self.start_date}>"
