from django.db import models
from authentication.models import (
    SystemUserProfile,
    BaseModel,
    Group
)
from panel.models import ProvinceOperator


# Create your models here.

class Ticket(BaseModel):
    customer = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name='ticket_customer',
        null=True
    )
    operator = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name='ticket_operator',
        null=True
    )
    question = models.ManyToManyField(
        'Question',
        related_name="ticket_question",
    )
    respond = models.ManyToManyField(
        'Answer',
        related_name="ticket_respond"
    )
    support_unit = models.ForeignKey(
        'SupportUnit',
        on_delete=models.CASCADE,
        related_name="ticket_unit",
        null=True
    )
    ticket_id = models.BigIntegerField(default=0)
    state = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(Ticket, self).save(*args, **kwargs)


class TicketContent(BaseModel):
    title = models.CharField(max_length=200, null=True)
    content = models.TextField(max_length=2000, null=True)
    image = models.JSONField(default=dict)

    def save(self, *args, **kwargs):
        super(TicketContent, self).save(*args, **kwargs)


class Question(BaseModel):
    questioner = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="questioner",
        null=True
    )
    content = models.ForeignKey(
        TicketContent,
        on_delete=models.CASCADE,
        related_name="questioner_content"
    )
    state = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(Question, self).save(*args, **kwargs)


class Answer(BaseModel):
    responder = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.CASCADE,
        related_name="answer",
        null=True
    )
    question = models.ForeignKey(
        Question,
        on_delete=models.CASCADE,
        related_name='answer_question',
        null=True
    )
    content = models.ForeignKey(
        TicketContent,
        on_delete=models.CASCADE,
        related_name='answer_content',
        null=True
    )
    state = models.CharField(max_length=100, null=True)

    def save(self, *args, **kwargs):
        super(Answer, self).save(*args, **kwargs)


class SupportUnit(BaseModel):
    name = models.CharField(max_length=100, null=True)
    unit = models.ForeignKey(
        Group,
        on_delete=models.CASCADE,
        related_name="support_unit",
        null=True
    )

    def save(self, *args, **kwargs):
        super(SupportUnit, self).save(*args, **kwargs)


class TicketSupport(BaseModel):
    user = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.SET_NULL,
        related_name='ticket_support',
        null=True
    )
    referred_to = models.ManyToManyField(
        SystemUserProfile,
        related_name='referred_tickets',
    )
    referred_by = models.ForeignKey(
        SystemUserProfile,
        on_delete=models.SET_NULL,
        related_name='referred_by_me',
        null=True,
    )
    to_user=models.ManyToManyField(
        SystemUserProfile,
        related_name='to_user_ticket',
    )

    to_role = models.ManyToManyField(
        Group,
        related_name='to_role_ticket',
    )

    parent=models.ForeignKey('TicketSupport',on_delete=models.CASCADE,null=True)
    title = models.CharField(max_length=100, null=True)
    ticket_id = models.IntegerField(unique=True, null=True)
    status = models.CharField(max_length=100, default='open')
    last_message = models.CharField(max_length=100, null=True)
    type_ticket = models.CharField(default='single',max_length=100)
    read_only = models.BooleanField(default=True)
    role = models.CharField(max_length=100, null=True)
    unread_message=models.BooleanField(default=False)
    is_referred = models.BooleanField(default=False)
    referred_date = models.DateTimeField(auto_now=True,null=True)


    def save(self, *args, **kwargs):
        if not self.ticket_id:
            last_ticket = TicketSupport.objects.order_by('-ticket_id').first()
            self.ticket_id = last_ticket.ticket_id + 1 if last_ticket else 1
        super(TicketSupport, self).save(*args, **kwargs)


class MessageSupport(models.Model):
    ticket = models.ForeignKey(TicketSupport, related_name='messages', on_delete=models.CASCADE,null=True)
    created_by = models.ForeignKey(SystemUserProfile, on_delete=models.CASCADE)
    parent = models.ForeignKey('self', null=True, blank=True, related_name='replies', on_delete=models.CASCADE)
    message = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    sender = models.CharField(max_length=50,default='user')
    picture = models.CharField(max_length=250, null=True)
    send_message = models.BooleanField(default=False)
    last_seen=models.DateTimeField(null=True)
    file=models.TextField( null=True)
    read_by = models.ManyToManyField(SystemUserProfile, related_name='read_messages', blank=True)



    def save(self, *args, **kwargs):
        super(MessageSupport,self).save(*args, **kwargs)


class TicketPermission(models.Model):
    role=models.CharField(max_length=200,null=True)
    roles=models.ManyToManyField(Group,related_name='to_role')

    def save(self, *args, **kwargs):
        super(TicketPermission,self).save(*args, **kwargs)


class TicketClosePermission(models.Model):
    active = models.BooleanField(default=False)
    day = models.SmallIntegerField(default=1)

    def save(self, *args, **kwargs):
        super(TicketClosePermission, self).save(*args, **kwargs)
