from django.db import models
from userauth import models as userauth_models
from django.core.exceptions import ValidationError
from core.validators import validate_future_date
from core.models import Service

GENDER = (
    ("Male", "Male"),
    ("Female", "Female"),
    ("None", "None"),
)


class Doctor(models.Model):
    GENDER = (
        ("Male", "Male"),
        ("Female", "Female"),
        ("None", "None"),
    )
    user = models.OneToOneField(userauth_models.User, on_delete=models.CASCADE, null=True, blank=True)
    first_name = models.CharField(max_length=100,  blank=True)
    last_name = models.CharField(max_length=100,  blank=True)
    phone_number = models.CharField(max_length=100,  blank=True)
    country_code = models.CharField(max_length=100,  blank=True)
    date_of_birth = models.DateField(validators=[validate_future_date], null=False, blank=False)
    specialty = models.CharField(max_length=100,  blank=True)
    gender = models.CharField(max_length=100, choices=GENDER, default="None")
    address = models.CharField(max_length=100,  blank=True)
    profile_picture = models.FileField(upload_to="images/doctor_profile", null=True, blank=True)
    wallet = models.DecimalField(default=0,max_digits=10, decimal_places=2,null=True, blank=True)
    age = models.IntegerField(null=True, blank=True)
    identifier = models.IntegerField(null=True, blank=True)
    biography = models.TextField(blank=True, null=True)
    STATUS_CHOICES = [
        ('active', 'Active'),
        ('inactive', 'Inactive'),
    ]
    status = models.CharField(
        max_length=8,
        choices=STATUS_CHOICES,
        default='active'
    )
    created_at = models.DateTimeField(auto_now_add=True,blank=True, null=True)

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

    def get_full_name(self):
        return f"{self.first_name} - {self.last_name}"
    def get_short_name(self):
        name = self.first_name
        name = name[0]
        return   f" {name} - {self.last_name[0]}"

    def clean(self):
        super().clean()

        # Additional phone number validation (if needed)
        if self.phone_number:
            if len(self.phone_number) < 10 or len(self.phone_number) > 15:
                raise ValidationError("Phone number must be between 10 and 15 digits.")

class Technician(models.Model):
    GENDER = (
        ("Male", "Male"),
        ("Female", "Female"),
        ("None", "None"),
    )
    services = models.ManyToManyField(
        Service,
        related_name='technicians',
        blank=True
    )
    user = models.OneToOneField(userauth_models.User, on_delete=models.CASCADE, null=True, blank=True)
    first_name = models.CharField(max_length=100,  blank=True)
    last_name = models.CharField(max_length=100, blank=True)
    phone_number = models.CharField(max_length=100,  blank=True)
    country_code = models.CharField(max_length=100,  blank=True)
    date_of_birth = models.DateField(validators=[validate_future_date], null=False, blank=False)
    specialty = models.CharField(max_length=100,  blank=True)
    gender = models.CharField(max_length=100, choices=GENDER, default="None")
    address = models.CharField(max_length=100,  blank=True)
    profile_picture = models.FileField(upload_to="images/technician_profile", null=True, blank=True)
    identifier = models.IntegerField(null=True, blank=True)
    biography = models.TextField(blank=True, null=True)
    wallet = models.DecimalField(default=0,max_digits=10, decimal_places=2,null=True, blank=True)
    age = models.IntegerField(null=True, blank=True)
    STATUS_CHOICES = [
        ('active', 'Active'),
        ('inactive', 'Inactive'),
    ]
    status = models.CharField(
        max_length=8,
        choices=STATUS_CHOICES,
        default='active'
    )
    created_at = models.DateTimeField(auto_now_add=True,blank=True, null=True)
    def __str__(self):
        return f"{self.last_name}"

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

    def clean(self):
        super().clean()

        # Additional phone number validation (if needed)
        if self.phone_number:
            if len(self.phone_number) < 10 or len(self.phone_number) > 15:
                raise ValidationError("Phone number must be between 10 and 15 digits.")