"""投诉工单模型：主单 + 证据图 + 升级补充说明。"""
from django.db import models
from gvsdsdk.model_base import QModel

from gongdan.constants import STATUS_PROCESSING, TYPE_CHOICES, STATUS_CHOICES


class Gongdan(QModel):
    """投诉工单主表。"""
    gongdan_id = models.CharField(max_length=32, unique=True, db_index=True, verbose_name='工单号')
    leixing = models.IntegerField(db_index=True, choices=TYPE_CHOICES, verbose_name='投诉类型')
    # 按类型选填关联业务 ID（字符串统一存，便于展示与复制）
    order_id = models.CharField(max_length=64, null=True, blank=True, db_index=True, verbose_name='订单ID')
    chongzhi_id = models.CharField(max_length=64, null=True, blank=True, db_index=True, verbose_name='充值单ID')
    fadan_id = models.CharField(max_length=32, null=True, blank=True, db_index=True, verbose_name='罚单ID')
    guanshi_id = models.CharField(max_length=32, null=True, blank=True, db_index=True, verbose_name='被投诉管事UID')

    shuoming = models.CharField(max_length=500, verbose_name='投诉说明')
    shenqingren_id = models.CharField(max_length=32, db_index=True, verbose_name='申请人UserUID')
    club_id = models.CharField(max_length=16, null=True, blank=True, db_index=True, verbose_name='俱乐部')

    zhuangtai = models.IntegerField(
        default=STATUS_PROCESSING, db_index=True, choices=STATUS_CHOICES, verbose_name='状态',
    )
    chuli_jieguo = models.CharField(max_length=500, null=True, blank=True, verbose_name='处理结果说明')
    chuliren_id = models.CharField(max_length=32, null=True, blank=True, db_index=True, verbose_name='处理人UID')
    chuliren_name = models.CharField(max_length=64, null=True, blank=True, verbose_name='处理人展示名')
    chuli_time = models.DateTimeField(null=True, blank=True, verbose_name='处理时间')

    shengji_cishu = models.IntegerField(default=0, verbose_name='升级次数')
    CreateTime = models.DateTimeField(auto_now_add=True, db_index=True)
    UpdateTime = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'gongdan'
        verbose_name = '投诉工单'
        ordering = ['-CreateTime']


class GongdanImage(QModel):
    """工单证据图（最多9张，以主单当前版本为准；升级时可整体替换）。"""
    gongdan = models.ForeignKey(
        Gongdan, on_delete=models.CASCADE, related_name='images', db_column='gongdan_pk',
    )
    # 冗余业务工单号（勿命名 gongdan_id，会与 FK 访问器冲突）
    gongdan_hao = models.CharField(max_length=32, db_index=True, verbose_name='工单号冗余')
    url = models.CharField(max_length=512, verbose_name='图片URL')
    paixu = models.IntegerField(default=0, verbose_name='排序')
    CreateTime = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'gongdan_image'
        verbose_name = '工单证据图'
        ordering = ['paixu', 'id']


class GongdanShengji(QModel):
    """升级/继续投诉补充说明（只记文字与次数；图片改主表 images）。"""
    gongdan = models.ForeignKey(
        Gongdan, on_delete=models.CASCADE, related_name='shengji_list', db_column='gongdan_pk',
    )
    gongdan_hao = models.CharField(max_length=32, db_index=True, verbose_name='工单号冗余')
    cishu = models.IntegerField(verbose_name='第几次升级')
    shuoming = models.CharField(max_length=500, verbose_name='补充说明')
    shenqingren_id = models.CharField(max_length=32, db_index=True, verbose_name='申请人')
    CreateTime = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'gongdan_shengji'
        verbose_name = '工单升级记录'
        ordering = ['cishu', 'id']
        unique_together = [['gongdan', 'cishu']]
