using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;

namespace SHH.Contracts
{
    // ==============================================================================
    // 1. 物理与运行配置 DTO (对应 CRUD 操作)
    // 用于设备新增/全量配置查询，包含基础身份、连接信息、运行参数等全量字段
    // ==============================================================================
    public class CameraConfigDto
    {
        // --- 基础身份 (Identity) ---

        /// <summary>设备唯一标识</summary>
        [Required(ErrorMessage = "设备ID不能为空")]
        [Range(1, long.MaxValue, ErrorMessage = "设备ID必须为正整数")]
        public long Id { get; set; }

        /// <summary>设备友好名称</summary>
        [MaxLength(64, ErrorMessage = "设备名称长度不能超过64个字符")]
        public string Name { get; set; } = string.Empty;

        /// <summary>摄像头品牌类型 (0:HikVision, 1:Dahua, 2:RTSP...)</summary>
        [Range(0, 10, ErrorMessage = "品牌类型值必须在0-10范围内")]
        public int Brand { get; set; }

        /// <summary>设备安装位置描述</summary>
        [MaxLength(128, ErrorMessage = "安装位置长度不能超过128个字符")]
        public string Location { get; set; } = string.Empty;

        // --- 主板关联信息 (Metadata) ---

        /// <summary>关联主板IP地址</summary>
        [RegularExpression(@"^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)?$",
            ErrorMessage = "请输入合法的IPv4地址")]
        public string MainboardIp { get; set; } = string.Empty;

        /// <summary>关联主板端口</summary>
        [Range(0, 65535, ErrorMessage = "主板端口号必须在1-65535范围内")]
        public int MainboardPort { get; set; } = 0;

        // --- 核心连接 (Connectivity) - 修改此类参数触发冷重启 ---

        /// <summary>摄像头IP地址</summary>
        [Required(ErrorMessage = "IP地址不能为空")]
        [RegularExpression(@"^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$",
            ErrorMessage = "请输入合法的IPv4地址")]
        public string IpAddress { get; set; } = string.Empty;

        /// <summary>登录用户名</summary>
        [MaxLength(32, ErrorMessage = "用户名长度不能超过32个字符")]
        public string Username { get; set; } = string.Empty;

        /// <summary>登录密码</summary>
        [MaxLength(64, ErrorMessage = "密码长度不能超过64个字符")]
        public string Password { get; set; } = string.Empty;

        /// <summary>SDK端口 (如海康默认8000)</summary>
        [Range(1, 65535, ErrorMessage = "端口号必须在1-65535范围内")]
        public ushort Port { get; set; }

        /// <summary>通道号 (通常为1)</summary>
        [Range(0, 256, ErrorMessage = "通道号必须在0-256范围内")]
        public int ChannelIndex { get; set; }

        /// <summary>码流类型 (0:主码流, 1:子码流)</summary>
        [Range(0, 1, ErrorMessage = "码流类型只能是0(主码流)或1(子码流)")]
        public int StreamType { get; set; }

        /// <summary>渲染句柄 (通常下发时为0，由本地窗口绑定时再指定，或者此处仅作占位)</summary>
        public long RenderHandle { get; set; }

        /// <summary>RTSP流路径 (备用或非SDK模式使用)</summary>
        [MaxLength(256, ErrorMessage = "RTSP地址长度不能超过256个字符")]
        public string RtspPath { get; set; } = string.Empty;

        // --- 运行时参数 (Runtime Options) - 支持热更新 ---

        /// <summary>是否使用灰度图 (用于AI分析场景加速)</summary>
        public bool UseGrayscale { get; set; } = false;

        /// <summary>是否启用图像增强 (去噪/锐化等)</summary>
        public bool EnhanceImage { get; set; } = true;

        // --- 画面变换 (Transform) - 支持热更新 ---

        /// <summary>是否允许图像压缩 (降低带宽占用)</summary>
        public bool AllowCompress { get; set; } = true;

        /// <summary>是否允许图像放大 (提升渲染质量)</summary>
        public bool AllowExpand { get; set; } = false;

        /// <summary>目标分辨率 (格式如 1920x1080，空则保持原图)</summary>
        [RegularExpression(@"^\d+x\d+$", ErrorMessage = "分辨率格式必须为 宽度x高度 (如 1920x1080)")]
        public string TargetResolution { get; set; } = string.Empty;

        /// <summary>随配置一并下发的自动订阅请求</summary>
        public List<CameraConfigSubscribeDto> AutoSubscriptions { get; set; }
            = new List<CameraConfigSubscribeDto>();

        /// <summary>是否立即执行</summary>
        [JsonProperty("ImmediateExecution")] // 确保 JSON 里的这个 key 能精准对应到这个属性
        public bool ImmediateExecution { get; set; }
    }
}