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

"""
Modbus配置模型模块
包含Modbus配置相关的请求和响应数据模型
"""

from typing import Optional, List, Dict, Any, Union
from enum import Enum
from pydantic import BaseModel, Field, validator
import re

from routes.model.base import PageResult, PaginationParams

# Modbus地址类型枚举
class ModbusAddressType(str, Enum):
    """Modbus地址类型枚举"""
    COIL = "0X"  # 线圈寄存器
    DISCRETE_INPUT = "1X"  # 离散输入寄存器
    INPUT_REGISTER = "3X"  # 输入寄存器
    HOLDING_REGISTER = "4X"  # 保持寄存器

# Modbus配置添加请求模型
class ModbusConfigAddRequest(BaseModel):
    """Modbus配置添加请求模型"""
    name: str = Field(..., description="配置名称", min_length=1, max_length=100)
    ip: str = Field(..., description="设备IP地址")
    port: int = Field(..., description="通信端口号")
    slave_id: int = Field(..., description="从站ID")
    address_type: ModbusAddressType = Field(..., description="地址类型")
    address_number: int = Field(..., description="地址编号")
    task_id: Optional[str] = Field(None, description="任务ID")
    target_value: Optional[int] = Field(None, description="目标值")
    reset_after_trigger: Optional[bool] = Field(False, description="触发后是否重置")
    reset_signal_address: Optional[int] = Field(None, description="重置信号地址")
    reset_value: Optional[int] = Field(None, description="重置值")
    remark: Optional[str] = Field(None, description="备注")
    tenant_id: Optional[str] = Field(None, description="租户ID")
    
# Modbus配置更新请求模型
class ModbusConfigUpdateRequest(BaseModel):
    """Modbus配置更新请求模型"""
    name: Optional[str] = Field(None, description="配置名称", min_length=1, max_length=100)
    ip: Optional[str] = Field(None, description="设备IP地址")
    port: Optional[int] = Field(None, description="通信端口号")
    slave_id: Optional[int] = Field(None, description="从站ID")
    address_type: Optional[ModbusAddressType] = Field(None, description="地址类型")
    address_number: Optional[int] = Field(None, description="地址编号")
    task_id: Optional[str] = Field(None, description="任务ID")
    target_value: Optional[int] = Field(None, description="目标值")
    remark: Optional[str] = Field(None, description="备注")

# Modbus连接测试请求模型
class ModbusConnectionTestRequest(BaseModel):
    """Modbus连接测试请求模型"""
    ip: str = Field(..., description="设备IP地址")
    port: int = Field(..., description="通信端口号")
    slave_id: int = Field(..., description="从站ID")
    address_type: Optional[ModbusAddressType] = Field(None, description="地址类型")
    address_number: Optional[int] = Field(None, description="地址编号")
    

# Modbus配置详情返回模型
class ModbusConfigDetail(BaseModel):
    """Modbus配置详情返回模型"""
    id: str = Field(..., description="配置ID")
    name: str = Field(..., description="配置名称", min_length=1, max_length=100)
    ip: str = Field(..., description="设备IP地址")
    port: int = Field(..., description="通信端口号")
    slave_id: int = Field(..., description="从站ID")
    address_type: str = Field(..., description="地址类型")
    address_number: int = Field(..., description="地址编号")
    task_id: Optional[str] = Field(None, description="任务ID")
    target_value: Optional[int] = Field(None, description="目标值")
    reset_after_trigger: Optional[bool] = Field(False, description="触发后是否重置")
    reset_signal_address: Optional[int] = Field(None, description="重置信号地址")
    reset_value: Optional[int] = Field(None, description="重置值")
    remark: Optional[str] = Field(None, description="备注")
    status: int = Field(..., description="状态（1:启用, 0:禁用）")
    tenant_id: Optional[str] = Field(None, description="租户ID")
    create_date: Optional[str] = Field(None, description="创建时间")
    update_date: Optional[str] = Field(None, description="更新时间")

# Modbus配置列表查询参数
class ModbusConfigListParams(PaginationParams):
    """Modbus配置列表查询参数"""
    name: Optional[str] = Field(None, description="配置名称，用于筛选", min_length=1, max_length=100)
    ip: Optional[str] = Field(None, description="设备IP地址，用于筛选")

# Modbus配置列表项模型
class ModbusConfigListItem(BaseModel):
    """Modbus配置列表项模型"""
    id: str = Field(..., description="配置ID")
    name: str = Field(..., description="配置名称", min_length=1, max_length=100)
    ip: str = Field(..., description="设备IP地址")
    port: int = Field(..., description="通信端口号")
    slave_id: int = Field(..., description="从站ID")
    address_type: str = Field(..., description="地址类型")
    address_number: int = Field(..., description="地址编号")
    target_value: Optional[int] = Field(None, description="目标值")
    remark: Optional[str] = Field(None, description="备注")
    status: int = Field(..., description="状态（1:启用, 0:禁用）")
    create_date: Optional[str] = Field(None, description="创建时间")

# Modbus配置列表响应模型
class ModbusConfigListResponse(PageResult):
    """Modbus配置列表响应模型"""
    records: List[ModbusConfigListItem] = Field(..., description="配置列表")
