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}"


class Report(models.Model):
    employee = models.ForeignKey('Employee', on_delete=models.CASCADE)
    report_date = models.DateField(auto_now_add=True)
    start_date = models.DateField(verbose_name="Начальная дата")
    end_date = models.DateField(verbose_name="Конечная дата")

    def generate_pdf(self):
        buffer = BytesIO()
        c = canvas.Canvas(buffer, pagesize=letter)

        c.setFont("Helvetica-Bold", 16)
        c.drawString(100, 750, f"Отчет по сотруднику: {self.employee.first_name} {self.employee.last_name}")
        c.setFont("Helvetica", 12)
        c.drawString(100, 730, f"Должность: {self.employee.position}")
        c.drawString(100, 710, f"Email: {self.employee.email}")
        c.drawString(100, 690, f"Дата найма: {self.employee.hired_date}")
        c.drawString(100, 670, f"Период отчета: {self.start_date} - {self.end_date}")

        # Рабочее время
        work_logs = WorkTimeLog.objects.filter(
            employee=self.employee, date__range=(self.start_date, self.end_date)
        ).order_by('-date')
        y = 640
        c.setFont("Helvetica-Bold", 14)
        c.drawString(100, y, "Рабочее время:")
        y -= 20
        for log in work_logs:
            c.setFont("Helvetica", 12)
            c.drawString(100, y, f"{log.date}: {log.check_in} - {log.check_out} (Часы: {log.worked_hours})")
            y -= 20

        # Отпуска
        leaves = Leave.objects.filter(
            employee=self.employee, start_date__lte=self.end_date, end_date__gte=self.start_date
        ).order_by('-start_date')
        c.setFont("Helvetica-Bold", 14)
        c.drawString(100, y, "Отпуска и больничные:")
        y -= 20
        for leave in leaves:
            c.setFont("Helvetica", 12)
            c.drawString(100, y, f"{leave.leave_type.capitalize()}: {leave.start_date} - {leave.end_date} (Причина: {leave.reason})")
            y -= 20

        # Переработки
        overtime_reports = OvertimeReport.objects.filter(
            employee=self.employee, date__range=(self.start_date, self.end_date)
        ).order_by('-date')
        c.setFont("Helvetica-Bold", 14)
        c.drawString(100, y, "Переработки:")
        y -= 20
        for report in overtime_reports:
            c.setFont("Helvetica", 12)
            c.drawString(100, y, f"{report.date}: {report.overtime} ч. (Комментарий: {report.comment})")
            y -= 20

        c.showPage()
        c.save()
        buffer.seek(0)
        return buffer

