using System;
using System.Collections.Generic;

namespace NPP.SmartSchedue.Api.Contracts.Services.Integration.Output
{
    /// <summary>
    /// 全局优化分配结果
    /// </summary>
    public class GlobalAllocationResult
    {
        /// <summary>
        /// 是否分配成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 成功分配的任务-人员匹配
        /// </summary>
        public List<GlobalTaskPersonnelMatch> SuccessfulMatches { get; set; } = new();

        /// <summary>
        /// 失败的分配项
        /// </summary>
        public List<GlobalFailedAllocation> FailedAllocations { get; set; } = new();

        /// <summary>
        /// 全局优化性能指标
        /// </summary>
        public GlobalOptimizationMetrics OptimizationMetrics { get; set; } = new();

        /// <summary>
        /// 智能协商执行结果
        /// </summary>
        public List<GlobalNegotiationAction> NegotiationActions { get; set; } = new();

        /// <summary>
        /// 工作负载公平性分析
        /// </summary>
        public GlobalWorkloadFairnessAnalysis FairnessAnalysis { get; set; } = new();

        /// <summary>
        /// 约束冲突检测结果
        /// </summary>
        public List<GlobalConflictDetectionInfo> ConflictDetections { get; set; } = new();

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

        /// <summary>
        /// 处理详情
        /// </summary>
        public string ProcessingDetails { get; set; } = string.Empty;
    }

    /// <summary>
    /// 全局任务人员匹配
    /// </summary>
    public class GlobalTaskPersonnelMatch
    {
        /// <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 int MatchScore { get; set; }

        /// <summary>
        /// 匹配原因
        /// </summary>
        public string MatchReason { get; set; } = string.Empty;

        /// <summary>
        /// 资质匹配情况
        /// </summary>
        public List<string> QualificationMatches { get; set; } = new();

        /// <summary>
        /// 预估效率
        /// </summary>
        public decimal EstimatedEfficiency { get; set; }
    }

    /// <summary>
    /// 全局分配失败项
    /// </summary>
    public class GlobalFailedAllocation
    {
        /// <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> ConflictDetails { get; set; } = new();
    }

    /// <summary>
    /// 全局优化性能指标
    /// </summary>
    public class GlobalOptimizationMetrics
    {
        /// <summary>
        /// 算法执行时间（毫秒）
        /// </summary>
        public long ExecutionTimeMs { get; set; }

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

        /// <summary>
        /// 种群大小
        /// </summary>
        public int PopulationSize { get; set; }

        /// <summary>
        /// 最终收敛度
        /// </summary>
        public double ConvergenceLevel { get; set; }

        /// <summary>
        /// 最佳适应度分数
        /// </summary>
        public double BestFitnessScore { get; set; }

        /// <summary>
        /// 平均适应度分数
        /// </summary>
        public double AverageFitnessScore { get; set; }

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

    /// <summary>
    /// 全局工作负载公平性分析
    /// </summary>
    public class GlobalWorkloadFairnessAnalysis
    {
        /// <summary>
        /// 基尼系数
        /// </summary>
        public double GiniCoefficient { get; set; }

        /// <summary>
        /// 公平性等级
        /// </summary>
        public GlobalFairnessLevel FairnessLevel { get; set; }

        /// <summary>
        /// 人员工作负载分布
        /// </summary>
        public Dictionary<long, GlobalPersonnelWorkloadInfo> PersonnelWorkloads { get; set; } = new();

        /// <summary>
        /// 负载标准差
        /// </summary>
        public double WorkloadStandardDeviation { get; set; }

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

    /// <summary>
    /// 全局人员工作负载信息
    /// </summary>
    public class GlobalPersonnelWorkloadInfo
    {
        /// <summary>
        /// 人员ID
        /// </summary>
        public long PersonnelId { get; set; }

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

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

        /// <summary>
        /// 预估总工时
        /// </summary>
        public decimal EstimatedTotalHours { get; set; }

        /// <summary>
        /// 工作负载百分比
        /// </summary>
        public double WorkloadPercentage { get; set; }

        /// <summary>
        /// 分配的任务列表
        /// </summary>
        public List<long> AssignedTaskIds { get; set; } = new();
    }

    /// <summary>
    /// 全局智能协商操作
    /// </summary>
    public class GlobalNegotiationAction
    {
        /// <summary>
        /// 操作类型
        /// </summary>
        public GlobalNegotiationActionType ActionType { get; set; }

        /// <summary>
        /// 任务ID
        /// </summary>
        public long TaskId { get; set; }

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

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

        /// <summary>
        /// 操作原因
        /// </summary>
        public string Reason { get; set; } = string.Empty;

        /// <summary>
        /// 协商成功
        /// </summary>
        public bool IsSuccessful { get; set; }
    }

    /// <summary>
    /// 全局冲突检测信息
    /// </summary>
    public class GlobalConflictDetectionInfo
    {
        /// <summary>
        /// 冲突类型
        /// </summary>
        public GlobalConflictType ConflictType { get; set; }

        /// <summary>
        /// 涉及的任务ID
        /// </summary>
        public long TaskId { get; set; }

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

        /// <summary>
        /// 冲突严重程度
        /// </summary>
        public GlobalConflictSeverity Severity { get; set; }

        /// <summary>
        /// 冲突描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 是否已解决
        /// </summary>
        public bool IsResolved { get; set; }

        /// <summary>
        /// 解决方案
        /// </summary>
        public string Resolution { get; set; } = string.Empty;
    }

    /// <summary>
    /// 全局公平性等级枚举
    /// </summary>
    public enum GlobalFairnessLevel
    {
        /// <summary>
        /// 非常公平（基尼系数 < 0.2）
        /// </summary>
        VeryFair = 1,

        /// <summary>
        /// 相对公平（基尼系数 0.2-0.3）
        /// </summary>
        Fair = 2,

        /// <summary>
        /// 一般公平（基尼系数 0.3-0.4）
        /// </summary>
        Moderate = 3,

        /// <summary>
        /// 不够公平（基尼系数 0.4-0.5）
        /// </summary>
        Unfair = 4,

        /// <summary>
        /// 很不公平（基尼系数 > 0.5）
        /// </summary>
        VeryUnfair = 5
    }

    /// <summary>
    /// 全局协商操作类型枚举
    /// </summary>
    public enum GlobalNegotiationActionType
    {
        /// <summary>
        /// 人员替换
        /// </summary>
        PersonnelReplacement = 1,

        /// <summary>
        /// 任务重分配
        /// </summary>
        TaskReallocation = 2,

        /// <summary>
        /// 人工介入
        /// </summary>
        ManualIntervention = 3
    }

    /// <summary>
    /// 全局冲突类型枚举
    /// </summary>
    public enum GlobalConflictType
    {
        /// <summary>
        /// 次日休息规则冲突
        /// </summary>
        NextDayRestViolation = 1,

        /// <summary>
        /// 同天班次连续性冲突
        /// </summary>
        SameDayShiftContinuity = 2,

        /// <summary>
        /// 周任务限制冲突
        /// </summary>
        WeeklyTaskLimit = 3,

        /// <summary>
        /// 跨周末连续性冲突
        /// </summary>
        CrossWeekendContinuity = 4,

        /// <summary>
        /// 资质不匹配冲突
        /// </summary>
        QualificationMismatch = 5,

        /// <summary>
        /// 时间不可用冲突
        /// </summary>
        TimeUnavailable = 6,

        /// <summary>
        /// 工作限制超出
        /// </summary>
        WorkLimitExceeded = 7,

        /// <summary>
        /// 负载不均衡
        /// </summary>
        LoadImbalance = 8
    }

    /// <summary>
    /// 全局冲突严重程度枚举
    /// </summary>
    public enum GlobalConflictSeverity
    {
        /// <summary>
        /// 严重
        /// </summary>
        Critical = 1,

        /// <summary>
        /// 高
        /// </summary>
        High = 2,

        /// <summary>
        /// 中等
        /// </summary>
        Medium = 3,

        /// <summary>
        /// 低
        /// </summary>
        Low = 4,

        /// <summary>
        /// 警告
        /// </summary>
        Warning = 5
    }
}