from django.db import models
from django.urls import reverse
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError

from tickets.models.ticket_list import TicketList


class Ticket(models.Model):
    TICKET_TYPES = (
        (1, "ST_REQUEST", [
            [111, 121, 149, 159],
            [110, 121, 149, 159],
            [121, 131, 149, 159],
            [131, 141, 149],
            [141, 151, 110],
            [149, 151, 110],
            [159],
            [151]
        ]),
        (2, "SERVICE_REQUEST", [
            [210, 211, 251],
            [211, 212, 220, 238, 249],
            [212, 221, 229, 238, 249],
            [221, 222, 238, 249],
            [220, 211, 238, 249],
            [229, 211, 251],
            [222, 231, 238, 249],
            [231, 241, 238, 249],
            [238, 231, 239, 211, 212, 221, 220, 222, 249],
            [239, 231, 239, 211, 212, 221, 220, 222, 249],
            [241, 251],
            [249, 251],
            [251]
        ]),
        (3, "ACCESS_REQUEST", [
            [320, 321, 359],
            [321],
            [359]
        ]),
        (4, "NEG_REQUEST", [
            [420, 421, 459],
            [421],
            [459]
        ])
    )

    TICKET_STATUSES_NAMES = {
        111: "NEW",
        110: "REOPENED",
        121: "ASSIGNED",
        131: "IN PROCESS",
        149: "WONTFIX",
        141: "DONE",
        159: "DUPLICATE",
        151: "CLOSED",
        210: "TEMPLATE",
        211: "BOOKED",
        212: "ASSIGNED",
        221: "ACCEPTED",
        220: "PENDING",
        229: "DECLINED",
        222: "PRE-START",
        231: "PROCESS",
        238: "PRE-FORCEMAJEUER",
        239: "FORCEMAJEUER",
        241: "DONE",
        249: "CANCELLED",
        251: "CLOSED",
        320: "PENDING",
        321: "ACCEPTED",
        359: "DECLINED",
        420: "PENDING",
        421: "ACCEPTED",
        459: "DECLINED"
    }

    TICKET_TYPES_CHOICES = tuple((item[0], item[1]) for item in TICKET_TYPES)
    LIFE_CYCLE_DICT = dict((item[0], item[2]) for item in TICKET_TYPES)
    TICKET_TYPES_DICT = dict(TICKET_TYPES_CHOICES)

    title = models.CharField(max_length=128)
    ticket_list = models.ForeignKey(TicketList, on_delete=models.CASCADE)
    ticket_type = models.PositiveSmallIntegerField(choices=TICKET_TYPES_CHOICES, default=1)
    status = models.PositiveSmallIntegerField(null=True)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True)
    due_date = models.DateField()
    created_by = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, related_name="created_by", editable=False)
    assigned_to = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, blank=True, related_name="assigned_to")
    note = models.TextField(blank=True, null=True)
    priority = models.PositiveSmallIntegerField(default=0)

    def __str__(self):
        return self.title

    def _get_statuses(self) -> list:
        return self.LIFE_CYCLE_DICT.get(self.ticket_type)

    def get_available_statuses(self) -> list:
        for status_scenario in self._get_statuses():
            if status_scenario[0] == int(self.status):
                return status_scenario[1:] if len(status_scenario) > 1 else None

    def set_first_status(self):
        self.status = self._get_statuses()[0][0]

    def get_status_display(self):
        return self.TICKET_STATUSES_NAMES.get(self.status)

    def save(self, *args, **kwargs):
        if not self.status:
            self.set_first_status()
        
        super(Ticket, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse("tickets:ticket_detail", kwargs={"ticket_id": self.pk})

    class Meta:
        ordering = ["-priority", "created_at"]
