import datetime
from django.db import models
from io import BytesIO
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

# Employee Directory
class Employee(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    position = models.CharField(max_length=100)
    email = models.EmailField(unique=True)
    hired_date = models.DateField()
    work_schedule = models.JSONField(default=dict)

    def __str__(self):
        return f"{self.first_name} {self.last_name}"

# Time Tracking System
class WorkTimeLog(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    date = models.DateField()
    check_in = models.TimeField(null=True, blank=True)
    check_out = models.TimeField(null=True, blank=True)
    worked_hours = models.FloatField(null=True, blank=True)

    def calculate_worked_hours(self):
        if self.check_in and self.check_out:
            delta = datetime.combine(datetime.date.min, self.check_out) - datetime.combine(datetime.date.min, self.check_in)
            self.worked_hours = delta.total_seconds() / 3600
        else:
            self.worked_hours = 0

    def save(self, *args, **kwargs):
        self.calculate_worked_hours()
        super().save(*args, **kwargs)

    def __str__(self):
        return f"{self.employee}: {self.date}"

# Leave Management
class Leave(models.Model):
    LEAVE_TYPE_CHOICES = [
        ("vacation", "Vacation"),
        ("sick", "Sick Leave"),
    ]

    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    leave_type = models.CharField(max_length=10, choices=LEAVE_TYPE_CHOICES)
    start_date = models.DateField()
    end_date = models.DateField()
    reason = models.TextField(blank=True)

    def __str__(self):
        return f"{self.employee}: {self.leave_type} ({self.start_date} - {self.end_date})"

    def duration(self):
        return (self.end_date - self.start_date).days + 1

# Overtime Reporting
class OvertimeReport(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    date = models.DateField()
    worked_hours = models.FloatField()
    required_hours = models.FloatField(default=8.0)
    overtime = models.FloatField()
    comment = models.TextField(blank=True)

    def calculate_overtime(self):
        self.overtime = self.worked_hours - self.required_hours

    def save(self, *args, **kwargs):
        self.calculate_overtime()
        super().save(*args, **kwargs)

    def __str__(self):
        return f"{self.employee}: {self.overtime} hours on {self.date}"

# PDF Generation for HR Reports
class Report(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    report_date = models.DateField(auto_now_add=True)

    def generate_pdf(self):
        buffer = BytesIO()
        c = canvas.Canvas(buffer, pagesize=letter)
        c.drawString(100, 750, f"Employee: {self.employee.first_name} {self.employee.last_name}")
        c.drawString(100, 730, f"Date: {self.report_date}")
        c.showPage()
        c.save()
        buffer.seek(0)
        return buffer
