#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
呼叫器设备模型
对应vwed_calldevice表和vwed_calldevice_button表
"""

import datetime
import json
from sqlalchemy import Column, String, Integer, Boolean, DateTime, Text, Index, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.mysql import LONGTEXT
from data.models.base import BaseModel

class VWEDCallDevice(BaseModel):
    """
    呼叫器设备模型
    对应vwed_calldevice表
    功能：定义呼叫器设备配置
    """
    __tablename__ = 'vwed_calldevice'

    __table_args__ = (
        Index('idx_vwed_calldevice_protocol', 'protocol'),
        Index('idx_vwed_calldevice_brand', 'brand'),
        Index('idx_vwed_calldevice_ip', 'ip'),
        Index('idx_vwed_calldevice_device_name', 'device_name'),
        Index('idx_vwed_calldevice_status', 'status'),
        {
            'mysql_engine': 'InnoDB',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_general_ci',
            'info': {'order_by': 'created_at DESC'}
        }
    )

    id = Column(String(255), primary_key=True, nullable=False, comment='呼叫器设备唯一标识')
    protocol = Column(String(50), nullable=False, comment='协议类型')
    brand = Column(String(100), nullable=False, comment='品牌')
    ip = Column(String(50), nullable=False, comment='IP地址')
    port = Column(Integer, nullable=False, comment='端口号')
    device_name = Column(String(100), nullable=False, comment='设备名称')
    is_init = Column(Boolean, default=False, comment='是否初始化')
    status = Column(Integer, default=0, comment='状态(0:禁用,1:启用,2:初始化中)')
    slave_id = Column(String(255), comment='从机ID')

    
    # 定义关联关系
    buttons = relationship("VWEDCallDeviceButton", back_populates="call_device", cascade="all, delete-orphan")
    
    def __repr__(self):
        return f"<VWEDCallDevice(id='{self.id}', protocol='{self.protocol}', brand='{self.brand}', ip='{self.ip}', port='{self.port}', device_name='{self.device_name}', status='{self.status}')>"
    
    def to_dict(self):
        result = super().to_dict()
        result['buttons'] = [button.to_dict() for button in self.buttons]
        return result

class VWEDCallDeviceButton(BaseModel):
    """
    呼叫器设备按钮模型
    对应vwed_calldevice_button表
    功能：定义呼叫器设备的按钮配置
    """
    __tablename__ = 'vwed_calldevice_button'

    __table_args__ = (
        Index('idx_vwed_calldevice_button_device_id', 'device_id'),
        Index('idx_vwed_calldevice_button_register_address', 'register_address'),
        {
            'mysql_engine': 'InnoDB',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_general_ci',
            'info': {'order_by': 'created_at DESC'}
        }
    )

    id = Column(String(255), primary_key=True, nullable=False, comment='按钮唯一标识')
    signal_name = Column(String(100), nullable=False, comment='信号名称')
    signal_type = Column(Integer, nullable=False, comment='信号类型 1:按钮 2:灯')
    signal_length = Column(String(100), nullable=False, comment='信号长度')
    register_address = Column(String(100), nullable=False, comment='寄存器地址')
    function_code = Column(String(255), comment='功能码')
    device_id = Column(String(255), ForeignKey('vwed_calldevice.id'), nullable=False, comment='所属呼叫器设备ID')
    remark = Column(String(255), comment='备注')
    vwed_task_id = Column(String(255), comment='按下灯亮绑定的VWED任务ID')
    long_vwed_task_id = Column(String(255), comment='长按取消后触发VWED任务ID')
    
    # 定义关联关系
    call_device = relationship("VWEDCallDevice", back_populates="buttons")
    
    def __repr__(self):
        return f"<VWEDCallDeviceButton(id='{self.id}', device_id='{self.device_id}', signal_name='{self.signal_name}', register_address='{self.register_address}', remark='{self.remark}')>" 