from django.db import models
from apps.am.models import Area
import uuid
from apps.hrm.models import Employee
from apps.utils.models import BaseModel

# Create your models here.


class TDevice(BaseModel):
    """TN: 第三方设备补充信息
    """
    DEVICE_BLG = 10
    DEVICE_IBEACON = 20
    DEVICE_BLT = 30
    DEVICE_AOA = 40
    DEVICE_SPEAKER = 50
    DEVICE_VCHANNEL = 60
    DEVICE_DCHANNEL = 70
    DEVICE_PANEL = 80

    DEVICE_CHOICE = (
        (DEVICE_BLG, '定位基站'),
        (DEVICE_IBEACON, '定位信标'),
        (DEVICE_BLT, '定位标签'),
        (DEVICE_AOA, 'aoa引擎'),
        (DEVICE_SPEAKER, '喇叭'),
        (DEVICE_VCHANNEL, '视频通道'),
        (DEVICE_DCHANNEL, '闸机通道'),
        (DEVICE_PANEL, '面板机')
    )
    type = models.PositiveSmallIntegerField('设备类型', choices=DEVICE_CHOICE)
    code = models.CharField('设备唯一标识', max_length=50,
                            db_index=True, unique=True)
    name = models.CharField('名称', max_length=50, null=True, blank=True)
    location = models.JSONField('位置信息', default=dict,
                                null=False, blank=True)
    area = models.ForeignKey(Area, on_delete=models.SET_NULL,
                             verbose_name='所在区', null=True, blank=True)
    areas = models.ManyToManyField(Area, verbose_name='覆盖区',
                                   related_name='tareas', blank=True)
    obj_cate = models.CharField(
        '绑定对象', max_length=20, help_text='people/...', null=True, blank=True)
    employee = models.OneToOneField(Employee, verbose_name='当前绑定人员', on_delete=models.SET_NULL,
                                    null=True, blank=True, related_name='blt')
    is_clock = models.BooleanField('是否打卡设备', default=False)
    access_list = models.JSONField('自动下发人员类型', default=list,
                                   null=False, blank=True, help_text='employee/remployee/visitor/driver')
    mtask_uid = models.CharField(
        '监控任务ID', max_length=100, null=True, blank=True)  # 废弃字段
    # algos = models.ManyToManyField('ecm.eventcate', through='ecm.algochannel', blank=True)
    third_info = models.JSONField('三方信息', default=dict,
                                  null=False, blank=True)


class BltBind(BaseModel):
    """TN: 定位标签绑定/解绑记录
    """
    BLT_BIND = 10
    BLT_UNBIND = 20
    BIND_TYPE_CHOICES = (
        (10, '绑定'),
        (20, '解绑')
    )
    type = models.PositiveSmallIntegerField('绑定类型', default=10, help_text='10(绑定)/20(解绑)',
                                            choices=BIND_TYPE_CHOICES)
    obj_cate = models.CharField(
        '绑定对象', max_length=20, help_text='people/...', null=True, blank=True)
    blt = models.ForeignKey(TDevice, verbose_name='关联标签',
                            on_delete=models.CASCADE)
    employee = models.ForeignKey(
        Employee, verbose_name='关联人员', on_delete=models.CASCADE, null=True, blank=True)


class Tlog(BaseModel):
    """TN: 第三方请求与处理日志
    """
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    target = models.CharField('请求目标', max_length=20)
    result = models.CharField('请求结果', max_length=20)
    path = models.CharField(max_length=400, help_text="请求地址")
    requested_at = models.DateTimeField()
    response_ms = models.PositiveIntegerField(default=0)
    headers = models.JSONField(null=True, blank=True)
    response = models.JSONField(null=True, blank=True)
    method = models.CharField(max_length=10)
    url = models.TextField(null=True, blank=True)
    errors = models.TextField(null=True, blank=True)
    params = models.JSONField(null=True, blank=True)
    body = models.JSONField(null=True, blank=True)


class DoorAuth(BaseModel):
    """TN: 闸机通道授权记录
    """
    employee = models.ForeignKey(
        Employee, verbose_name='关联用户', on_delete=models.CASCADE)
    dchannel = models.ForeignKey(
        TDevice, verbose_name='关联门通道', on_delete=models.CASCADE)
