"""投诉工单模型：主单 + 证据图 + 升级补充说明 + 俱乐部开关。"""
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='投诉类型')
    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(兼容)')

    # 通用被投诉对象（存库；接口对普通端脱敏不展示 UID）
    target_role = models.CharField(max_length=16, blank=True, default='', db_index=True, verbose_name='被投诉角色')
    target_user_id = models.CharField(max_length=32, blank=True, default='', db_index=True, verbose_name='被投诉人UID')

    shuoming = models.CharField(max_length=500, verbose_name='投诉说明')
    # 投诉人自填联系方式（选填，便于客服回访）
    contact_qq = models.CharField(max_length=32, blank=True, default='', verbose_name='联系QQ')
    contact_wechat = models.CharField(max_length=64, blank=True, default='', verbose_name='联系微信')
    contact_phone = models.CharField(max_length=20, blank=True, default='', 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='处理时间')

    # 平台介入（对外只展示「平台已介入」，不露客服 UID）
    platform_intervened = models.BooleanField(default=False, db_index=True, verbose_name='平台已介入')
    intervene_at = models.DateTimeField(null=True, blank=True, verbose_name='介入时间')
    intervene_by = models.CharField(max_length=32, blank=True, default='', verbose_name='介入操作人UID(仅存库)')
    intervene_reason = models.CharField(max_length=32, blank=True, default='', 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_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):
    """升级/继续投诉补充说明。"""
    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']]


class ClubGongdanConfig(QModel):
    """俱乐部工单相关开关（默认全关，仅种子打开的俱乐部生效）。"""
    club_id = models.CharField(max_length=16, unique=True, db_index=True, verbose_name='俱乐部')
    # 未结清被投诉/牵连时禁止提现
    complaint_withdraw_block = models.BooleanField(default=False, verbose_name='投诉拦截提现')
    # 投诉页人工客服（分俱乐部配置；有则前端展示）
    human_kefu_wechat = models.CharField(max_length=64, blank=True, default='', verbose_name='人工客服微信号')
    human_kefu_link = models.CharField(max_length=512, blank=True, default='', verbose_name='人工客服链接')
    human_kefu_tip = models.CharField(max_length=120, blank=True, default='', verbose_name='人工客服提示文案')
    CreateTime = models.DateTimeField(auto_now_add=True)
    UpdateTime = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'club_gongdan_config'
        verbose_name = '俱乐部工单配置'
