#from django.db import models
from django.contrib.gis.db import models
from django_google_maps import fields as map_fields
from colorfield.fields import ColorField
#from smart_selects.db_fields import GroupedForeignKey
from smart_selects.db_fields import (
    ChainedForeignKey,
    ChainedManyToManyField,
    GroupedForeignKey,
)
# Create your models here.
GENDER_CHOICES = (
    ('นางสาว','นางสาว'),
    ('นาย','นาย'),
    ('นาง', 'นาง'),
    ("ด.ช.","เด็กชาย"),
    ("ด.ญ.","เด็กหญิง"),
)

class Ambulance(models.Model):
    code = models.CharField(max_length=100)
    license_plate = models.CharField(max_length=100)
    brand = models.CharField(max_length=100)
    model_name = models.CharField(max_length=100)
    comment  = models.TextField(blank=True, null=True)

    color = ColorField(default='#FF0000')
    status = models.CharField(
        max_length=30,
        choices=(("working", "Working"), ("free", "Free"), ("ma", "MA")),
        null=True,
    )

    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.license_plate} ({self.get_status_display()}) {self.code} {self.brand} / {self.model_name}"

class Driver(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    age = models.IntegerField()
    idcard = models.CharField(max_length=20, null=True, blank=False)
    prefix = models.CharField(
        max_length=30,
        choices=GENDER_CHOICES,
        null=True,
    )
    sex = models.CharField(
        max_length=30,
        choices=(("male", "Male"), ("female", "Female"),),
        null=True,
    )
    photo  = models.FileField(upload_to="uploads/%Y/%m/%d/", blank=True, verbose_name="Photo")

    address  = models.TextField(blank=True, null=True)
    #test
    status = models.CharField(
        max_length=30,
        choices=(("working", "Working"), ("free", "Free"), ("block", "Block")),
        null=True,
    )
    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_at = models.DateTimeField(auto_now=True)

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

class AmbulanceTicket(models.Model):
    driver = models.ForeignKey(Driver, on_delete=models.SET_NULL, null=True)
    ambulance = models.ForeignKey(Ambulance, on_delete=models.SET_NULL, null=True)

    status = models.CharField(
        max_length=30,
        choices=(("working", "Working"), ("free", "Free"), ("ma", "MA")),
        null=True,
    )

    checkin_at = models.DateTimeField(null=True, blank=True)
    checkout_at = models.DateTimeField(null=True, blank=True)


    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_at = models.DateTimeField(auto_now=True)


    def save(self, *args, **kwargs):
        super(AmbulanceTicket, self).save(*args, **kwargs)
        self.ambulance.status = self.status
        self.ambulance.save()
        self.driver.status = "working"
        self.driver.save()

    def __str__(self):
        return f"{self.driver}@{self.ambulance}"

class Patient(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    age = models.IntegerField()
    idcard = models.CharField(max_length=20, null=True, blank=False)
    prefix = models.CharField(
        max_length=30,
        choices=GENDER_CHOICES,
        null=True,
    )
    sex = models.CharField(
        max_length=30,
        choices=(("male", "Male"), ("female", "Female"),),
        null=True,
    )
    photo  = models.FileField(upload_to="uploads/%Y/%m/%d/", blank=True, verbose_name="Photo")

    address = map_fields.AddressField(max_length=200, null=True)
    geolocation = map_fields.GeoLocationField(max_length=100, null=True)
    condition_level = models.CharField(
        max_length=30,
        choices=(("green", "Green"), ("yellow", "Yellow"), ("red", "Red")),
        null=True,
    )
    comment  = models.TextField(blank=True, null=True)
    #test
    def __str__(self):
        return f"{self.first_name} {self.last_name}"

class Place(models.Model):
    address = map_fields.AddressField(max_length=200)
    geolocation = map_fields.GeoLocationField(max_length=100)

    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_at = models.DateTimeField(auto_now=True)

    more_info  = models.JSONField(null=True, blank=True)

class Hospital(models.Model):
    title = models.CharField(max_length=200)
    location = models.PointField(blank=True, null=True)
    address_text = models.TextField(blank=True, null=True)
    #address = models.CharField(max_length=100)
    address = map_fields.AddressField(max_length=200)
    geolocation = map_fields.GeoLocationField(max_length=100)

    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.title} {self.address_text}"


class Bed(models.Model):
    code = models.CharField(max_length=30)
    occupy = models.BooleanField(default=False)
    patient = models.ForeignKey(Patient, on_delete=models.SET_NULL, null=True, blank=True)
    hospital = models.ForeignKey(Hospital, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_at = models.DateTimeField(auto_now=True)


    def __str__(self):
        return self.code


class PatientLog(models.Model):
    patient = models.ForeignKey(Patient, on_delete=models.SET_NULL, null=True)
    hospital = models.ForeignKey(Hospital, on_delete=models.CASCADE, null=True)
    bed = ChainedForeignKey(
        "Bed",
        chained_field="hospital",
        chained_model_field="hospital",
        show_all=False,
        auto_choose=True,
        null=True
    )
    notes  = models.TextField(blank=True, null=True)
    condition_level = models.CharField(
        max_length=30,
        choices=(("green", "Green"), ("yellow", "Yellow"), ("red", "Red")),
        null=True,
    )
    status = models.CharField(
        max_length=30,
        choices=(("active", "Active"), ("inactive", "Inactive"), ("transfer", "Transfer")),
        null=True,
    )
    checkin_at = models.DateTimeField(null=True, blank=True)
    checkout_at = models.DateTimeField(null=True, blank=True)

    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_at = models.DateTimeField(auto_now=True)


