using System;
using System.Collections.Generic;
using NPP.SmartSchedue.Api.Contracts.Services.Work.Output;
using NPP.SmartSchedue.Api.Contracts.Services.Integration.Models;

namespace NPP.SmartSchedue.Api.Contracts.Services.Integration.Output
{
    /// <summary>
    /// 智能重新分配结果
    /// 记录重新分配操作的详细结果和统计信息
    /// 
    /// 业务价值：
    /// 1. 结果追踪：详细记录重新分配的执行结果
    /// 2. 性能评估：提供重新分配后的效果评估
    /// 3. 问题诊断：记录失败原因和改进建议
    /// 4. 决策支持：为后续优化提供数据支撑
    /// </summary>
    public class SmartReallocationResult
    {
        /// <summary>
        /// 重新分配是否成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 成功消息
        /// </summary>
        public string SuccessMessage { get; set; } = string.Empty;

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; set; } = string.Empty;

        /// <summary>
        /// 重新分配执行时间
        /// </summary>
        public DateTime ReallocationTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 新创建的整合记录ID
        /// 如果创建了新版本
        /// </summary>
        public long? NewIntegrationRecordId { get; set; }

        /// <summary>
        /// 新版本号
        /// </summary>
        public int? NewVersionNumber { get; set; }

        /// <summary>
        /// 原整合记录ID
        /// </summary>
        public long OriginalIntegrationRecordId { get; set; }

        /// <summary>
        /// 人员重新分配结果
        /// </summary>
        public PersonnelReallocationResult PersonnelReallocation { get; set; } = new();

        /// <summary>
        /// 设备重新分配结果
        /// </summary>
        public EquipmentReallocationResult EquipmentReallocation { get; set; } = new();

        /// <summary>
        /// 任务处理结果
        /// </summary>
        public TaskReallocationResult TaskReallocation { get; set; } = new();

        /// <summary>
        /// 重新分配统计信息
        /// </summary>
        public ReallocationStatistics Statistics { get; set; } = new();

        /// <summary>
        /// 性能对比分析
        /// 与原方案的对比
        /// </summary>
        public PerformanceComparison PerformanceComparison { get; set; } = new();

        /// <summary>
        /// 质量评估
        /// </summary>
        public QualityAssessment QualityAssessment { get; set; } = new();

        /// <summary>
        /// 通知发送结果
        /// </summary>
        public List<IntegrationNotificationResult> NotificationResults { get; set; } = new();

        /// <summary>
        /// 验证结果
        /// 重新分配后的验证检查结果
        /// </summary>
        public ReallocationValidationResult ValidationResult { get; set; } = new();

        /// <summary>
        /// 警告信息
        /// </summary>
        public List<string> WarningMessages { get; set; } = new();

        /// <summary>
        /// 改进建议
        /// </summary>
        public List<string> ImprovementSuggestions { get; set; } = new();

        /// <summary>
        /// 重新分配原因
        /// </summary>
        public string ReallocationReason { get; set; } = string.Empty;

        /// <summary>
        /// 操作员信息
        /// </summary>
        public OperatorInfo Operator { get; set; } = new();

        /// <summary>
        /// 重新分配ID
        /// 标识本次重新分配操作
        /// </summary>
        public string ReallocationId { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 受影响的任务数量
        /// </summary>
        public int AffectedTaskCount { get; set; }
    }

    /// <summary>
    /// 人员重新分配结果
    /// </summary>
    public class PersonnelReallocationResult
    {
        /// <summary>
        /// 重新分配的人员任务数量
        /// </summary>
        public int ReallocatedTaskCount { get; set; }

        /// <summary>
        /// 成功的人员分配列表
        /// </summary>
        public List<PersonnelReallocationDetail> SuccessfulAllocations { get; set; } = new();

        /// <summary>
        /// 失败的人员分配列表
        /// </summary>
        public List<FailedPersonnelReallocation> FailedAllocations { get; set; } = new();

        /// <summary>
        /// 保持不变的分配列表
        /// </summary>
        public List<UnchangedPersonnelAllocation> UnchangedAllocations { get; set; } = new();

        /// <summary>
        /// 人员工作负载分析
        /// </summary>
        public List<PersonnelWorkloadAnalysis> WorkloadAnalysis { get; set; } = new();

        /// <summary>
        /// 新的公平性评分
        /// </summary>
        public int NewFairnessScore { get; set; }

        /// <summary>
        /// 公平性改善程度
        /// </summary>
        public int FairnessImprovement { get; set; }

        /// <summary>
        /// 分配摘要
        /// </summary>
        public string AllocationSummary { get; set; } = string.Empty;
    }

    /// <summary>
    /// 人员重新分配详情
    /// </summary>
    public class PersonnelReallocationDetail
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        public long TaskId { get; set; }

        /// <summary>
        /// 任务代码
        /// </summary>
        public string TaskCode { get; set; } = string.Empty;

        /// <summary>
        /// 原分配人员ID
        /// </summary>
        public long? PreviousPersonnelId { get; set; }

        /// <summary>
        /// 原分配人员姓名
        /// </summary>
        public string PreviousPersonnelName { get; set; } = string.Empty;

        /// <summary>
        /// 新分配人员ID
        /// </summary>
        public long NewPersonnelId { get; set; }

        /// <summary>
        /// 新分配人员姓名
        /// </summary>
        public string NewPersonnelName { get; set; } = string.Empty;

        /// <summary>
        /// 重新分配原因
        /// </summary>
        public string ReallocationReason { get; set; } = string.Empty;

        /// <summary>
        /// 匹配评分
        /// </summary>
        public int MatchScore { get; set; }

        /// <summary>
        /// 预期效率
        /// </summary>
        public decimal ExpectedEfficiency { get; set; }

        /// <summary>
        /// 重新分配时间
        /// </summary>
        public DateTime ReallocationTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 质量改善评估
        /// </summary>
        public string QualityImprovement { get; set; } = string.Empty;
    }

    /// <summary>
    /// 失败的人员重新分配
    /// </summary>
    public class FailedPersonnelReallocation
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        public long TaskId { get; set; }

        /// <summary>
        /// 任务代码
        /// </summary>
        public string TaskCode { get; set; } = string.Empty;

        /// <summary>
        /// 失败原因
        /// </summary>
        public string FailureReason { get; set; } = string.Empty;

        /// <summary>
        /// 详细错误信息
        /// </summary>
        public List<string> DetailedErrors { get; set; } = new();

        /// <summary>
        /// 冲突详情
        /// </summary>
        public List<string> ConflictDetails { get; set; } = new();

        /// <summary>
        /// 建议解决方案
        /// </summary>
        public List<string> SuggestedSolutions { get; set; } = new();

        /// <summary>
        /// 当前分配状态
        /// </summary>
        public string CurrentAllocationStatus { get; set; } = string.Empty;
    }

    /// <summary>
    /// 保持不变的人员分配
    /// </summary>
    public class UnchangedPersonnelAllocation
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        public long TaskId { get; set; }

        /// <summary>
        /// 任务代码
        /// </summary>
        public string TaskCode { get; set; } = string.Empty;

        /// <summary>
        /// 分配的人员ID
        /// </summary>
        public long PersonnelId { get; set; }

        /// <summary>
        /// 分配的人员姓名
        /// </summary>
        public string PersonnelName { get; set; } = string.Empty;

        /// <summary>
        /// 保持不变的原因
        /// </summary>
        public string UnchangedReason { get; set; } = string.Empty;

        /// <summary>
        /// 匹配评分
        /// </summary>
        public int MatchScore { get; set; }
    }

    /// <summary>
    /// 设备重新分配结果
    /// </summary>
    public class EquipmentReallocationResult
    {
        /// <summary>
        /// 重新分配的设备任务数量
        /// </summary>
        public int ReallocatedTaskCount { get; set; }

        /// <summary>
        /// 成功的设备分配列表
        /// </summary>
        public List<EquipmentReallocationDetail> SuccessfulAllocations { get; set; } = new();

        /// <summary>
        /// 失败的设备分配列表
        /// </summary>
        public List<FailedEquipmentReallocation> FailedAllocations { get; set; } = new();

        /// <summary>
        /// 保持不变的分配列表
        /// </summary>
        public List<UnchangedEquipmentAllocation> UnchangedAllocations { get; set; } = new();

        /// <summary>
        /// 设备利用率分析
        /// </summary>
        public List<EquipmentUtilizationAnalysis> UtilizationAnalysis { get; set; } = new();

        /// <summary>
        /// 新的整体利用率
        /// </summary>
        public decimal NewOverallUtilizationRate { get; set; }

        /// <summary>
        /// 利用率改善程度
        /// </summary>
        public decimal UtilizationImprovement { get; set; }

        /// <summary>
        /// 分配摘要
        /// </summary>
        public string AllocationSummary { get; set; } = string.Empty;
    }

    /// <summary>
    /// 设备重新分配详情
    /// </summary>
    public class EquipmentReallocationDetail
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        public long TaskId { get; set; }

        /// <summary>
        /// 任务代码
        /// </summary>
        public string TaskCode { get; set; } = string.Empty;

        /// <summary>
        /// 原分配设备ID
        /// </summary>
        public long? PreviousEquipmentId { get; set; }

        /// <summary>
        /// 原分配设备名称
        /// </summary>
        public string PreviousEquipmentName { get; set; } = string.Empty;

        /// <summary>
        /// 新分配设备ID
        /// </summary>
        public long NewEquipmentId { get; set; }

        /// <summary>
        /// 新分配设备名称
        /// </summary>
        public string NewEquipmentName { get; set; } = string.Empty;

        /// <summary>
        /// 重新分配原因
        /// </summary>
        public string ReallocationReason { get; set; } = string.Empty;

        /// <summary>
        /// 利用率
        /// </summary>
        public decimal UtilizationRate { get; set; }

        /// <summary>
        /// 预期性能
        /// </summary>
        public decimal ExpectedPerformance { get; set; }

        /// <summary>
        /// 重新分配时间
        /// </summary>
        public DateTime ReallocationTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 效率改善评估
        /// </summary>
        public string EfficiencyImprovement { get; set; } = string.Empty;
    }

    /// <summary>
    /// 失败的设备重新分配
    /// </summary>
    public class FailedEquipmentReallocation
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        public long TaskId { get; set; }

        /// <summary>
        /// 任务代码
        /// </summary>
        public string TaskCode { get; set; } = string.Empty;

        /// <summary>
        /// 失败原因
        /// </summary>
        public string FailureReason { get; set; } = string.Empty;

        /// <summary>
        /// 详细错误信息
        /// </summary>
        public List<string> DetailedErrors { get; set; } = new();

        /// <summary>
        /// 冲突详情
        /// </summary>
        public List<string> ConflictDetails { get; set; } = new();

        /// <summary>
        /// 建议解决方案
        /// </summary>
        public List<string> SuggestedSolutions { get; set; } = new();

        /// <summary>
        /// 当前分配状态
        /// </summary>
        public string CurrentAllocationStatus { get; set; } = string.Empty;
    }

    /// <summary>
    /// 保持不变的设备分配
    /// </summary>
    public class UnchangedEquipmentAllocation
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        public long TaskId { get; set; }

        /// <summary>
        /// 任务代码
        /// </summary>
        public string TaskCode { get; set; } = string.Empty;

        /// <summary>
        /// 分配的设备ID
        /// </summary>
        public long EquipmentId { get; set; }

        /// <summary>
        /// 分配的设备名称
        /// </summary>
        public string EquipmentName { get; set; } = string.Empty;

        /// <summary>
        /// 保持不变的原因
        /// </summary>
        public string UnchangedReason { get; set; } = string.Empty;

        /// <summary>
        /// 利用率
        /// </summary>
        public decimal UtilizationRate { get; set; }
    }

    /// <summary>
    /// 任务重新分配结果
    /// </summary>
    public class TaskReallocationResult
    {
        /// <summary>
        /// 总任务数
        /// </summary>
        public int TotalTaskCount { get; set; }

        /// <summary>
        /// 成功重新分配的任务数
        /// </summary>
        public int SuccessfulReallocationCount { get; set; }

        /// <summary>
        /// 失败的任务数
        /// </summary>
        public int FailedReallocationCount { get; set; }

        /// <summary>
        /// 保持不变的任务数
        /// </summary>
        public int UnchangedTaskCount { get; set; }

        /// <summary>
        /// 重新分配成功率
        /// </summary>
        public decimal ReallocationSuccessRate { get; set; }

        /// <summary>
        /// 任务状态分布
        /// </summary>
        public Dictionary<string, int> TaskStatusDistribution { get; set; } = new();

        /// <summary>
        /// 任务复杂度分布
        /// </summary>
        public Dictionary<string, int> ComplexityDistribution { get; set; } = new();

        /// <summary>
        /// 优先级分布
        /// </summary>
        public Dictionary<string, int> PriorityDistribution { get; set; } = new();
    }

    /// <summary>
    /// 重新分配统计信息
    /// </summary>
    public class ReallocationStatistics
    {
        /// <summary>
        /// 总执行时间（毫秒）
        /// </summary>
        public long TotalElapsedMilliseconds { get; set; }

        /// <summary>
        /// 分析阶段耗时（毫秒）
        /// </summary>
        public long AnalysisElapsedMilliseconds { get; set; }

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

        /// <summary>
        /// 验证阶段耗时（毫秒）
        /// </summary>
        public long ValidationElapsedMilliseconds { get; set; }

        /// <summary>
        /// 涉及的人员数量
        /// </summary>
        public int InvolvedPersonnelCount { get; set; }

        /// <summary>
        /// 涉及的设备数量
        /// </summary>
        public int InvolvedEquipmentCount { get; set; }

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

        /// <summary>
        /// 算法迭代次数
        /// </summary>
        public int AlgorithmIterations { get; set; }

        /// <summary>
        /// 约束条件数量
        /// </summary>
        public int ConstraintCount { get; set; }

        /// <summary>
        /// 满足的约束数量
        /// </summary>
        public int SatisfiedConstraintCount { get; set; }

        /// <summary>
        /// 约束满足率
        /// </summary>
        public decimal ConstraintSatisfactionRate { get; set; }

        /// <summary>
        /// 成功重新分配的任务数
        /// </summary>
        public int SuccessfulReallocationCount { get; set; }

        /// <summary>
        /// 失败的重新分配数
        /// </summary>
        public int FailedReallocationCount { get; set; }

        /// <summary>
        /// 人员分配成功率
        /// </summary>
        public decimal PersonnelAllocationSuccessRate { get; set; }

        /// <summary>
        /// 设备分配成功率
        /// </summary>
        public decimal EquipmentAllocationSuccessRate { get; set; }
    }

    /// <summary>
    /// 性能对比分析
    /// </summary>
    public class PerformanceComparison
    {
        /// <summary>
        /// 原方案性能评分
        /// </summary>
        public decimal OriginalPerformanceScore { get; set; }

        /// <summary>
        /// 新方案性能评分
        /// </summary>
        public decimal NewPerformanceScore { get; set; }

        /// <summary>
        /// 性能改善程度
        /// </summary>
        public decimal PerformanceImprovement { get; set; }

        /// <summary>
        /// 效率指标对比
        /// </summary>
        public EfficiencyMetricsComparison EfficiencyComparison { get; set; } = new();

        /// <summary>
        /// 资源利用率对比
        /// </summary>
        public ResourceUtilizationComparison ResourceUtilizationComparison { get; set; } = new();

        /// <summary>
        /// 公平性对比
        /// </summary>
        public FairnessComparison FairnessComparison { get; set; } = new();

        /// <summary>
        /// 质量指标对比
        /// </summary>
        public QualityMetricsComparison QualityComparison { get; set; } = new();

        /// <summary>
        /// 改善摘要
        /// </summary>
        public string ImprovementSummary { get; set; } = string.Empty;
    }

    /// <summary>
    /// 效率指标对比
    /// </summary>
    public class EfficiencyMetricsComparison
    {
        /// <summary>
        /// 原预期完成时间（小时）
        /// </summary>
        public decimal OriginalEstimatedCompletionTime { get; set; }

        /// <summary>
        /// 新预期完成时间（小时）
        /// </summary>
        public decimal NewEstimatedCompletionTime { get; set; }

        /// <summary>
        /// 时间节省（小时）
        /// </summary>
        public decimal TimeSaved { get; set; }

        /// <summary>
        /// 效率提升百分比
        /// </summary>
        public decimal EfficiencyImprovementPercentage { get; set; }

        /// <summary>
        /// 平均任务完成效率
        /// </summary>
        public decimal AverageTaskCompletionEfficiency { get; set; }
    }

    /// <summary>
    /// 资源利用率对比
    /// </summary>
    public class ResourceUtilizationComparison
    {
        /// <summary>
        /// 原人员利用率
        /// </summary>
        public decimal OriginalPersonnelUtilization { get; set; }

        /// <summary>
        /// 新人员利用率
        /// </summary>
        public decimal NewPersonnelUtilization { get; set; }

        /// <summary>
        /// 人员利用率改善
        /// </summary>
        public decimal PersonnelUtilizationImprovement { get; set; }

        /// <summary>
        /// 原设备利用率
        /// </summary>
        public decimal OriginalEquipmentUtilization { get; set; }

        /// <summary>
        /// 新设备利用率
        /// </summary>
        public decimal NewEquipmentUtilization { get; set; }

        /// <summary>
        /// 设备利用率改善
        /// </summary>
        public decimal EquipmentUtilizationImprovement { get; set; }

        /// <summary>
        /// 整体资源利用率改善
        /// </summary>
        public decimal OverallUtilizationImprovement { get; set; }
    }

    /// <summary>
    /// 公平性对比
    /// </summary>
    public class FairnessComparison
    {
        /// <summary>
        /// 原公平性评分
        /// </summary>
        public int OriginalFairnessScore { get; set; }

        /// <summary>
        /// 新公平性评分
        /// </summary>
        public int NewFairnessScore { get; set; }

        /// <summary>
        /// 公平性改善程度
        /// </summary>
        public int FairnessImprovement { get; set; }

        /// <summary>
        /// 工作负载方差改善
        /// </summary>
        public decimal WorkloadVarianceImprovement { get; set; }

        /// <summary>
        /// 最大工作负载差异改善
        /// </summary>
        public decimal MaxWorkloadDifferenceImprovement { get; set; }
    }

    /// <summary>
    /// 质量指标对比
    /// </summary>
    public class QualityMetricsComparison
    {
        /// <summary>
        /// 原技能匹配度
        /// </summary>
        public decimal OriginalSkillMatchRate { get; set; }

        /// <summary>
        /// 新技能匹配度
        /// </summary>
        public decimal NewSkillMatchRate { get; set; }

        /// <summary>
        /// 技能匹配度改善
        /// </summary>
        public decimal SkillMatchImprovement { get; set; }

        /// <summary>
        /// 原质量风险评分
        /// </summary>
        public int OriginalQualityRiskScore { get; set; }

        /// <summary>
        /// 新质量风险评分
        /// </summary>
        public int NewQualityRiskScore { get; set; }

        /// <summary>
        /// 质量风险改善程度
        /// </summary>
        public int QualityRiskImprovement { get; set; }
    }

    /// <summary>
    /// 质量评估
    /// </summary>
    public class QualityAssessment
    {
        /// <summary>
        /// 总体质量评分（0-100）
        /// </summary>
        public int OverallQualityScore { get; set; }

        /// <summary>
        /// 分配合理性评分
        /// </summary>
        public int AllocationReasonabilityScore { get; set; }

        /// <summary>
        /// 资源匹配度评分
        /// </summary>
        public int ResourceMatchScore { get; set; }

        /// <summary>
        /// 约束满足度评分
        /// </summary>
        public int ConstraintSatisfactionScore { get; set; }

        /// <summary>
        /// 优化效果评分
        /// </summary>
        public int OptimizationEffectivenessScore { get; set; }

        /// <summary>
        /// 风险控制评分
        /// </summary>
        public int RiskControlScore { get; set; }

        /// <summary>
        /// 质量改善点
        /// </summary>
        public List<string> QualityImprovements { get; set; } = new();

        /// <summary>
        /// 潜在问题点
        /// </summary>
        public List<string> PotentialIssues { get; set; } = new();

        /// <summary>
        /// 质量评估摘要
        /// </summary>
        public string QualityAssessmentSummary { get; set; } = string.Empty;
    }

    /// <summary>
    /// 重新分配验证结果
    /// </summary>
    public class ReallocationValidationResult
    {
        /// <summary>
        /// 验证是否通过
        /// </summary>
        public bool IsValid { get; set; } = true;

        /// <summary>
        /// 验证执行时间
        /// </summary>
        public DateTime ValidationTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 验证错误列表
        /// </summary>
        public List<IntegrationValidationError> ValidationErrors { get; set; } = new();

        /// <summary>
        /// 验证警告列表
        /// </summary>
        public List<ValidationWarning> ValidationWarnings { get; set; } = new();

        /// <summary>
        /// 冲突检测结果
        /// </summary>
        public List<ConflictDetail> ConflictDetails { get; set; } = new();

        /// <summary>
        /// 业务规则验证结果
        /// </summary>
        public List<BusinessRuleValidation> BusinessRuleValidations { get; set; } = new();

        /// <summary>
        /// 验证摘要
        /// </summary>
        public string ValidationSummary { get; set; } = string.Empty;

        /// <summary>
        /// 验证耗时（毫秒）
        /// </summary>
        public long ValidationElapsedMilliseconds { get; set; }
    }

    /// <summary>
    /// 业务规则验证
    /// </summary>
    public class BusinessRuleValidation
    {
        /// <summary>
        /// 规则名称
        /// </summary>
        public string RuleName { get; set; } = string.Empty;

        /// <summary>
        /// 验证是否通过
        /// </summary>
        public bool IsPassed { get; set; }

        /// <summary>
        /// 验证消息
        /// </summary>
        public string ValidationMessage { get; set; } = string.Empty;

        /// <summary>
        /// 严重程度
        /// </summary>
        public string SeverityLevel { get; set; } = "Medium";

        /// <summary>
        /// 受影响的任务ID列表
        /// </summary>
        public List<long> AffectedTaskIds { get; set; } = new();
    }

    /// <summary>
    /// 操作员信息
    /// </summary>
    public class OperatorInfo
    {
        /// <summary>
        /// 操作员用户ID
        /// </summary>
        public long UserId { get; set; }

        /// <summary>
        /// 操作员用户名
        /// </summary>
        public string UserName { get; set; } = string.Empty;

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

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

        /// <summary>
        /// 操作类型
        /// </summary>
        public string OperationType { get; set; } = string.Empty;
    }
}