from decimal import Decimal
from django.db import models
from django.core.validators import MinValueValidator
from django.utils import timezone

class ExchangeRate(models.Model):
    date = models.DateField(auto_now_add=True)
    rate = models.DecimalField(max_digits=10, decimal_places=2)  # نرخ دینار به ازای هر دلار

    def __str__(self):
        return f"{self.date}: {self.rate}"

class ServiceCategory(models.Model):

    name = models.CharField(max_length=100, unique=True,
                            blank=True, null=True)
    description = models.TextField(blank=True, null=True)

    created_at = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.name


class Service(models.Model):
    service_category = models.ForeignKey(
        ServiceCategory, on_delete=models.SET_NULL, blank=True, null=True)

    STATUS_CHOICES = [
        ('active', 'Active'),
        ('inactive', 'Inactive'),
    ]

    name = models.CharField(max_length=100, unique=True,
                            blank=True, null=True)
    hours_duration = models.IntegerField(blank=True, null=True, default=0)
    minutes_duration = models.IntegerField(blank=True, null=True, default=0)
    unit_price = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        validators=[MinValueValidator(0.0)], blank=True, null=True
    )
    status = models.CharField(
        max_length=8,
        choices=STATUS_CHOICES,
        default='active'
    )
    description = models.TextField(blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name
    class Meta:
        ordering = ['-created_at']


class PatientService(models.Model):
    patient = models.ForeignKey(
        'patient.Patient', on_delete=models.SET_NULL, blank=True, null=True)
    doctor = models.ForeignKey(
        'doctor.Doctor', on_delete=models.SET_NULL, blank=True, null=True)
    technician = models.ForeignKey(
        'doctor.Technician', on_delete=models.SET_NULL, null=True, blank=True)

    additional_notes = models.TextField(blank=True, default="")
    created_at = models.DateTimeField(default=timezone.now)

    #using this field to store the date of the service before created_At is good but becuse prodution enviroment we not using it
    # service_date = models.DateTimeField(default=timezone.now)  # Add this field

    def total_price(self):
        total = sum(item.total_price() for item in self.patientserviceitem_set.all())
        return total

    def __str__(self):
        if self.patient:
            return f"{self.patient.first_name} {self.patient.last_name} - {self.created_at}"
        return f"PatientService {self.id} - {self.created_at}"


class PatientServiceItem(models.Model):
    patient_service = models.ForeignKey(PatientService, on_delete=models.SET_NULL, null=True)
    service = models.ForeignKey(Service, on_delete=models.SET_NULL, null=True)
    number_of_units = models.PositiveIntegerField(default=1)
    discount = models.DecimalField(max_digits=10, decimal_places=2, default=0)  # Add discount field

    def total_price(self):
        if self.service.unit_price:
            total = self.service.unit_price * self.number_of_units
            return total - self.discount
        return 0  # Return 0 if there's no unit_price set for the service

    def __str__(self):
        if self.patient_service and self.patient_service.patient:
            return f"{self.patient_service.patient.first_name} {self.patient_service.patient.last_name} - {self.service.name}"
        elif self.service:
            return f"{self.service.name} - No Patient"
        return "No Service - No Patient"

    class Meta:
        unique_together = ('patient_service', 'service')