from django.db import models
from BaseModels.base_models import BaseModel
from django.utils.translation import gettext_lazy as _

msg_type_choices = (
    ('support', 'техподдержка'),
    ('private', 'личное')
)

msg_status_choices = (
    ('sended', 'Отправлено'),
    ('seen', 'Просмотрено')
)

grp_msg_status = (
    ('open', 'Открыт'),
    ('answered', 'Отвечен'),
    ('closed', 'Закрыт')
)


grp_msg_department = (
    ('support', 'Отдел: Техническая поддержка'),
    ('finance', 'Отдел: Финансовый департамент'),
)


class MsgGroup(BaseModel):
    from AuthApp.models import User

    department = models.CharField(verbose_name='Отдел', default='support', choices=grp_msg_department)
    status = models.CharField(verbose_name='Статус', default='open', choices=grp_msg_status)

    text = models.TextField(verbose_name='Сообщение')

    owner = models.ForeignKey(User, verbose_name=_('Владелец'), related_name='rel_msgGroups_for_owner',
                              on_delete=models.SET_NULL, null=True)
    manager = models.ForeignKey(User, verbose_name=_('Менеджер'), related_name='rel_msgGroups_for_manager',
                              on_delete=models.SET_NULL, null=True)

    class Meta:
        verbose_name = _('Тикет')
        verbose_name_plural = _('Тикеты')


class Message(BaseModel):
    from AuthApp.models import User

    msg_type = models.CharField(max_length=50, verbose_name='Тип сообщения', default='private', choices=msg_type_choices)
    group = models.ForeignKey(
        MsgGroup, verbose_name='Группа сообщений', related_name='rel_messages_for_group',
        on_delete=models.SET_NULL, null=True)

    sender = models.ForeignKey(
        User, verbose_name='Отправитель', on_delete=models.CASCADE, related_name='rel_messages_for_sender'
    )
    receiver = models.ForeignKey(
        User, verbose_name='Получатель', on_delete=models.CASCADE, related_name='rel_messages_for_receiver'
    )

    text = models.TextField(verbose_name='Сообщение')

    status = models.CharField(verbose_name='Статус', default='sended', choices=msg_status_choices)

    files = models.JSONField(verbose_name='Прикрепленные файлы', default=dict)

    class Meta:
        verbose_name = _('Сообщение')
        verbose_name_plural = _('Сообщения')

