using System;
using System.Collections.Generic;

namespace NPP.SmartSchedue.Api.Contracts.Services.Integration.Output
{
    /// <summary>
    /// 智能整合结果
    /// 记录智能任务整合的完整执行结果和统计信息
    /// 
    /// 业务价值：
    /// 1. 结果追踪：详细记录整合执行的完整结果
    /// 2. 性能评估：提供整合后的效果评估和性能指标
    /// 3. 问题诊断：记录失败原因和改进建议
    /// 4. 决策支持：为后续优化提供数据支撑
    /// 5. 审计追踪：支持操作审计和合规性检查
    /// </summary>
    public class SmartIntegrationResult
    {
        #region 核心执行状态

        /// <summary>
        /// 整合是否成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 整合批次编码
        /// 格式：INT + yyyyMMddHHmmss + XXX，总长度23字符
        /// </summary>
        public string IntegrationBatchCode { get; set; } = string.Empty;

        /// <summary>
        /// 整合记录ID
        /// 关联到数据库中的整合记录表
        /// </summary>
        public long IntegrationRecordId { get; set; }

        /// <summary>
        /// 完成时间
        /// </summary>
        public DateTime CompletionTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 执行耗时（毫秒）
        /// </summary>
        public long ElapsedMilliseconds { get; set; }

        #endregion

        #region 分配结果

        /// <summary>
        /// 人员分配结果
        /// 包含成功分配、失败分配、公平性评分等详细信息
        /// </summary>
        public PersonnelAllocationResult PersonnelAllocation { get; set; } = new();

        /// <summary>
        /// 设备分配结果
        /// 包含成功分配、失败分配、利用率分析等详细信息
        /// </summary>
        public EquipmentAllocationResult EquipmentAllocation { get; set; } = new();

        #endregion

        #region 统计信息

        /// <summary>
        /// 成功分配的任务数量
        /// </summary>
        public int SuccessfulTaskCount { get; set; }

        /// <summary>
        /// 总体成功率（0-1）
        /// 计算公式：成功任务数量 / 总任务数量
        /// </summary>
        public double OverallSuccessRate { get; set; }

        /// <summary>
        /// 失败任务列表
        /// 包含失败原因、类型和冲突详情
        /// </summary>
        public List<FailedTaskInfo> FailedTasks { get; set; } = new();

        #endregion

        #region 详细信息和报告

        /// <summary>
        /// 整合详情
        /// 包含整合过程的详细描述和结果摘要
        /// </summary>
        public string IntegrationDetails { get; set; } = string.Empty;

        /// <summary>
        /// 改进建议列表
        /// 基于整合结果生成的优化建议
        /// </summary>
        public List<string> ImprovementSuggestions { get; set; } = new();

        /// <summary>
        /// 警告信息列表
        /// 整合过程中产生的警告信息
        /// </summary>
        public List<string> WarningMessages { get; set; } = new();

        #endregion

        #region 性能和质量指标

        /// <summary>
        /// 人员分配公平性评分（0-100）
        /// 评估人员分配是否公平合理
        /// </summary>
        public double PersonnelFairnessScore => PersonnelAllocation?.FairnessScore ?? 0;

        /// <summary>
        /// 设备利用率（百分比）
        /// 设备分配后的整体利用率
        /// </summary>
        public decimal EquipmentUtilizationRate => EquipmentAllocation?.OverallUtilizationRate ?? 0;

        /// <summary>
        /// 整合质量评分（0-100）
        /// 综合考虑成功率、公平性、利用率等因素
        /// </summary>
        public double IntegrationQualityScore => CalculateIntegrationQualityScore();

        #endregion

        #region 操作信息

        /// <summary>
        /// 操作员用户ID
        /// </summary>
        public long OperatorUserId { get; set; }

        /// <summary>
        /// 操作员姓名
        /// </summary>
        public string OperatorName { get; set; } = string.Empty;

        /// <summary>
        /// 操作时间
        /// </summary>
        public DateTime OperationTime { get; set; } = DateTime.Now;

        #endregion

        #region 业务统计

        /// <summary>
        /// 涉及的项目数量
        /// </summary>
        public int ProjectCount { get; set; }

        /// <summary>
        /// 涉及的项目编号列表
        /// </summary>
        public List<string> ProjectNumbers { get; set; } = new();

        /// <summary>
        /// 任务优先级分布
        /// 记录不同优先级任务的数量分布
        /// </summary>
        public Dictionary<string, int> TaskPriorityDistribution { get; set; } = new();

        /// <summary>
        /// 任务复杂度分布
        /// 记录不同复杂度任务的数量分布
        /// </summary>
        public Dictionary<string, int> TaskComplexityDistribution { get; set; } = new();

        #endregion

        #region 系统信息

        /// <summary>
        /// 系统版本信息
        /// </summary>
        public string SystemVersion { get; set; } = string.Empty;

        /// <summary>
        /// 执行环境信息
        /// </summary>
        public string ExecutionEnvironment { get; set; } = string.Empty;

        /// <summary>
        /// 数据库连接信息（脱敏）
        /// </summary>
        public string DatabaseInfo { get; set; } = string.Empty;

        #endregion

        #region 私有方法

        /// <summary>
        /// 计算整合质量评分
        /// 综合考虑多个维度的质量指标
        /// </summary>
        private double CalculateIntegrationQualityScore()
        {
            var successRateWeight = 0.4;
            var fairnessWeight = 0.3;
            var utilizationWeight = 0.2;
            var efficiencyWeight = 0.1;

            var successRateScore = OverallSuccessRate * 100;
            var fairnessScore = PersonnelFairnessScore;
            var utilizationScore = (double)EquipmentUtilizationRate;
            var efficiencyScore = ElapsedMilliseconds > 0 ? Math.Min(100, 10000.0 / ElapsedMilliseconds * 100) : 0;

            return successRateScore * successRateWeight +
                   fairnessScore * fairnessWeight +
                   utilizationScore * utilizationWeight +
                   efficiencyScore * efficiencyWeight;
        }

        #endregion
    }
}
