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

namespace NPP.SmartSchedue.Api.Contracts.Services.Integration.Output
{
    /// <summary>
    /// 人员分配结果
    /// </summary>
    public class PersonnelAllocationResult
    {
        /// <summary>
        /// 分配是否成功
        /// </summary>
        public bool IsSuccess { get; set; }
        
        /// <summary>
        /// 分配成功的任务人员匹配
        /// </summary>
        public List<TaskPersonnelMatch> SuccessfulMatches { get; set; } = new();
        
        /// <summary>
        /// 分配失败的任务
        /// </summary>
        public List<FailedPersonnelAllocation> FailedAllocations { get; set; } = new();
        
        /// <summary>
        /// 人员工作负载分析
        /// </summary>
        public List<PersonnelWorkloadAnalysis> WorkloadAnalysis { get; set; } = new();
        
        /// <summary>
        /// 公平性评分（0-100）
        /// </summary>
        public int FairnessScore { get; set; }
        
        /// <summary>
        /// 分配策略执行摘要
        /// </summary>
        public string AllocationSummary { get; set; }

        /// <summary>
        /// 失败任务列表（为了兼容性）
        /// </summary>
        public List<FailedTaskInfo> FailedTasks { get; set; } = new();
        
        /// <summary>
        /// 分配策略
        /// </summary>
        public string AllocationStrategy { get; set; }
        
        /// <summary>
        /// 处理详情
        /// </summary>
        public string ProcessingDetails { get; set; }
        
        /// <summary>
        /// 验证结果 - 新增属性支持五层架构
        /// </summary>
        public object ValidationResult { get; set; }
    }
    
    /// <summary>
    /// 任务人员匹配
    /// </summary>
    public class TaskPersonnelMatch
    {
        public long TaskId { get; set; }
        public string TaskCode { get; set; }
        public long PersonnelId { get; set; }
        public string PersonnelName { get; set; }
        public int MatchScore { get; set; } // 匹配度评分 0-100
        public string MatchReason { get; set; } // 匹配原因
        public List<string> QualificationMatches { get; set; } = new(); // 匹配的资质
        public decimal EstimatedEfficiency { get; set; } // 预估执行效率
    }
    
    
    /// <summary>
    /// 失败的人员分配
    /// </summary>
    public class FailedPersonnelAllocation
    {
        public long TaskId { get; set; }
        public string TaskCode { get; set; }
        public string FailureReason { get; set; }
        public List<string> ConflictDetails { get; set; } = new();
        public List<PersonnelConflictInfo> ConflictPersonnels { get; set; } = new();
    }
    
    /// <summary>
    /// 人员冲突信息
    /// </summary>
    public class PersonnelConflictInfo
    {
        public long PersonnelId { get; set; }
        public string PersonnelName { get; set; }
        public string ConflictType { get; set; } // "时间冲突"、"资质不匹配"、"工作量超限"等
        public string ConflictDescription { get; set; }
    }
    
    /// <summary>
    /// 设备分配结果
    /// </summary>
    public class EquipmentAllocationResult
    {
        /// <summary>
        /// 分配成功的任务设备匹配
        /// </summary>
        public List<TaskEquipmentMatch> SuccessfulMatches { get; set; } = new();
        
        /// <summary>
        /// 分配失败的任务
        /// </summary>
        public List<FailedEquipmentAllocation> FailedAllocations { get; set; } = new();
        
        /// <summary>
        /// 设备利用率分析
        /// </summary>
        public List<EquipmentUtilizationAnalysis> UtilizationAnalysis { get; set; } = new();
        
        /// <summary>
        /// 总体设备利用率（百分比）
        /// </summary>
        public decimal OverallUtilizationRate { get; set; }
        
        /// <summary>
        /// 设备分配策略执行摘要
        /// </summary>
        public string AllocationSummary { get; set; }
    }
    
    /// <summary>
    /// 任务设备匹配
    /// </summary>
    public class TaskEquipmentMatch
    {
        public long TaskId { get; set; }
        public string TaskCode { get; set; }
        public long EquipmentId { get; set; }
        public string EquipmentName { get; set; }
        public string EquipmentCode { get; set; }
        public decimal UtilizationRate { get; set; }
        public string AllocationReason { get; set; }
        public decimal EstimatedPerformance { get; set; } // 预估性能指标
    }
    
    /// <summary>
    /// 设备利用率分析
    /// </summary>
    public class EquipmentUtilizationAnalysis
    {
        public long EquipmentId { get; set; }
        public string EquipmentName { get; set; }
        public int AssignedTaskCount { get; set; }
        public decimal TotalScheduledHours { get; set; }
        public decimal UtilizationRate { get; set; }
        public List<DateTime> ScheduledDates { get; set; } = new();
        public string UtilizationStatus { get; set; } // "闲置"、"正常"、"高负荷"、"满负荷"
    }
    
    /// <summary>
    /// 失败的设备分配
    /// </summary>
    public class FailedEquipmentAllocation
    {
        public long TaskId { get; set; }
        public string TaskCode { get; set; }
        public string FailureReason { get; set; }
        public List<string> ConflictDetails { get; set; } = new();
        public List<EquipmentConflictInfo> ConflictEquipments { get; set; } = new();
    }
    
    /// <summary>
    /// 设备冲突信息
    /// </summary>
    public class EquipmentConflictInfo
    {
        public long EquipmentId { get; set; }
        public string EquipmentName { get; set; }
        public string ConflictType { get; set; } // "时间冲突"、"能力不匹配"、"维护期间"等
        public string ConflictDescription { get; set; }
    }
    
    /// <summary>
    /// 失败任务信息
    /// </summary>
    public class FailedTaskInfo
    {
        public long TaskId { get; set; }
        public string TaskCode { get; set; }
        public string FailureReason { get; set; }
        public FailureType FailureType { get; set; }
        public List<string> DetailedErrors { get; set; } = new();
        public List<string> ConflictDetails { get; set; } = new();
    }
    
    /// <summary>
    /// 失败类型枚举
    /// </summary>
    public enum FailureType
    {
        /// <summary>
        /// 人员分配失败
        /// </summary>
        PersonnelAllocationFailed = 1,
        
        /// <summary>
        /// 设备分配失败
        /// </summary>
        EquipmentAllocationFailed = 2,
        
        /// <summary>
        /// 资源不足
        /// </summary>
        InsufficientResources = 3,
        
        /// <summary>
        /// 规则冲突
        /// </summary>
        RuleConflict = 4,
        
        /// <summary>
        /// 系统错误
        /// </summary>
        SystemError = 5,

        /// <summary>
        /// 设备不可用
        /// </summary>
        EquipmentUnavailable = 6,

        /// <summary>
        /// 验证失败
        /// </summary>
        ValidationFailure = 7,

        /// <summary>
        /// 时间冲突
        /// </summary>
        TimeConflict = 8,

        /// <summary>
        /// 无可用人员
        /// </summary>
        NoAvailablePersonnel = 9
    }
    
    /// <summary>
    /// 整合记录
    /// </summary>
    public class IntegrationRecord
    {
        public long Id { get; set; }
        
        /// <summary>
        /// 整合批次编码
        /// </summary>
        public string IntegrationBatchCode { get; set; }
        
        /// <summary>
        /// 整合时间
        /// </summary>
        public DateTime IntegrationTime { get; set; }
        
        /// <summary>
        /// 操作人员
        /// </summary>
        public long OperatorUserId { get; set; }
        public string OperatorName { get; set; }
        
        /// <summary>
        /// 整合的任务ID列表
        /// </summary>
        public List<long> TaskIds { get; set; } = new();
        
        /// <summary>
        /// 整合策略描述
        /// </summary>
        public string StrategyDescription { get; set; }
        
        /// <summary>
        /// 整合结果摘要
        /// </summary>
        public string ResultSummary { get; set; }
        
        /// <summary>
        /// 成功任务数量
        /// </summary>
        public int SuccessTaskCount { get; set; }
        
        /// <summary>
        /// 失败任务数量
        /// </summary>
        public int FailedTaskCount { get; set; }
        
        /// <summary>
        /// 整合耗时（毫秒）
        /// </summary>
        public long ElapsedMilliseconds { get; set; }
        
        /// <summary>
        /// 项目编号列表
        /// </summary>
        public string ProjectNumbers { get; set; }
        
        /// <summary>
        /// 任务总数
        /// </summary>
        public int TaskCount { get; set; }
        
        /// <summary>
        /// 成功率
        /// </summary>
        public decimal SuccessRate { get; set; }
        
        /// <summary>
        /// 人员公平性评分
        /// </summary>
        public int PersonnelFairnessScore { get; set; }
        
        /// <summary>
        /// 设备利用率
        /// </summary>
        public decimal EquipmentUtilizationRate { get; set; }
        
        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { get; set; }
        
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedTime { get; set; } = DateTime.Now;
    }
}