using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using NPP.SmartSchedue.Api.Contracts.Services.Integration.Input;
using System.Linq;
using NPP.SmartSchedue.Api.Contracts.Services.Integration.Models; // Added for .Any() and .Intersect()

namespace NPP.SmartSchedue.Api.Contracts.Services.Integration.Input
{
    /// <summary>
    /// 人员分配推荐输入参数
    /// 用于智能推荐系统中获取最优人员分配方案的输入数据
    /// 支持多种推荐策略、批量任务处理和灵活配置选项
    /// </summary>
    public class PersonnelAllocationRecommendationInput
    {
        #region 核心任务数据

        /// <summary>
        /// 工作任务ID列表
        /// 需要推荐人员分配的具体工作任务
        /// </summary>
        [Required(ErrorMessage = "工作任务ID列表不能为空")]
        [MinLength(1, ErrorMessage = "至少需要一个工作任务")]
        public List<long> WorkOrderIds { get; set; } = new();

        /// <summary>
        /// 推荐策略
        /// 控制推荐算法的优先级和侧重点
        /// </summary>
        public PersonnelAllocationStrategy Strategy { get; set; } = PersonnelAllocationStrategy.FairDistribution;

        #endregion

        #region 推荐配置选项

        /// <summary>
        /// 每个任务的推荐人数
        /// 为每个任务推荐的前N个候选人员数量
        /// </summary>
        [Range(1, 10, ErrorMessage = "推荐人数应在1-10之间")]
        public int RecommendationCount { get; set; } = 3;

        /// <summary>
        /// 是否包含详细评分
        /// 在推荐结果中包含各维度的详细评分信息
        /// </summary>
        public bool IncludeDetailedScores { get; set; } = true;

        /// <summary>
        /// 是否包含风险评估
        /// 在推荐结果中包含潜在风险和冲突检测
        /// </summary>
        public bool IncludeRiskAssessment { get; set; } = true;

        /// <summary>
        /// 是否包含替代方案
        /// 当最优方案不可行时，提供替代分配方案
        /// </summary>
        public bool IncludeAlternativeSolutions { get; set; } = false;

        #endregion

        #region 筛选和约束条件

        /// <summary>
        /// 排除的人员ID列表
        /// 明确指定不参与推荐的人员
        /// </summary>
        public List<long> ExcludedPersonnelIds { get; set; } = new();

        /// <summary>
        /// 优先考虑的人员ID列表
        /// 在同等条件下优先推荐的人员
        /// </summary>
        public List<long> PreferredPersonnelIds { get; set; } = new();

        /// <summary>
        /// 最小资质匹配度要求 (0-100)
        /// 推荐人员必须达到的最低资质匹配分数
        /// </summary>
        [Range(0, 100, ErrorMessage = "最小资质匹配度应在0-100之间")]
        public double MinimumQualificationScore { get; set; } = 60.0;

        /// <summary>
        /// 最小约束符合度要求 (0-100)
        /// 推荐人员必须达到的最低约束符合分数
        /// </summary>
        [Range(0, 100, ErrorMessage = "最小约束符合度应在0-100之间")]
        public double MinimumConstraintScore { get; set; } = 60.0;

        #endregion

        #region 时间范围控制

        /// <summary>
        /// 推荐基准日期
        /// 作为推荐计算的基准时间点
        /// </summary>
        public DateTime RecommendationBaseDate { get; set; } = DateTime.Now;

        /// <summary>
        /// 推荐时间范围
        /// 限制推荐计算的时间窗口
        /// </summary>
        public DateRange? TimeRange { get; set; }

        #endregion

        #region 业务上下文信息

        /// <summary>
        /// 推荐请求来源
        /// 标识推荐触发的业务场景
        /// </summary>
        public RecommendationSource RequestSource { get; set; } = RecommendationSource.Manual;

        /// <summary>
        /// 操作员信息
        /// 记录执行推荐的用户
        /// </summary>
        public OperatorInfo Operator { get; set; } = new();

        /// <summary>
        /// 推荐批次标识
        /// 用于关联和追踪推荐过程
        /// </summary>
        public string RecommendationBatchId { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 业务场景标识
        /// 标识具体的业务使用场景
        /// </summary>
        public string BusinessScenario { get; set; } = string.Empty;

        /// <summary>
        /// 优先级权重配置
        /// 自定义各评分维度的权重分配
        /// </summary>
        public Dictionary<string, double> CustomWeights { get; set; } = new();

        #endregion

        #region 性能和质量控制

        /// <summary>
        /// 推荐超时时间（秒）
        /// 防止推荐过程过长影响用户体验
        /// </summary>
        [Range(5, 300, ErrorMessage = "推荐超时时间应在5-300秒之间")]
        public int RecommendationTimeoutSeconds { get; set; } = 60;

        /// <summary>
        /// 最小推荐置信度 (0-100)
        /// 只返回置信度达到此阈值的推荐结果
        /// </summary>
        [Range(0, 100, ErrorMessage = "最小推荐置信度应在0-100之间")]
        public double MinimumConfidenceLevel { get; set; } = 70.0;

        /// <summary>
        /// 是否启用缓存
        /// 使用缓存提高推荐性能
        /// </summary>
        public bool EnableCaching { get; set; } = true;

        #endregion

        #region 高级配置选项

        /// <summary>
        /// 负载均衡策略
        /// 控制推荐时的人员工作负载分配策略
        /// </summary>
        public LoadBalanceStrategy LoadBalanceStrategy { get; set; } = LoadBalanceStrategy.Moderate;

        /// <summary>
        /// 技能匹配策略
        /// 控制技能匹配的严格程度
        /// </summary>
        public SkillMatchStrategy SkillMatchStrategy { get; set; } = SkillMatchStrategy.Balanced;

        /// <summary>
        /// 历史表现权重
        /// 历史工作表现对推荐的影响权重
        /// </summary>
        [Range(0, 1, ErrorMessage = "历史表现权重应在0-1之间")]
        public double HistoricalPerformanceWeight { get; set; } = 0.3;

        #endregion

        #region 验证和约束

        /// <summary>
        /// 是否启用严格验证
        /// 启用更严格的推荐结果验证
        /// </summary>
        public bool EnableStrictValidation { get; set; } = false;

        /// <summary>
        /// 是否包含合规性检查
        /// 在推荐中包含法规和制度合规性检查
        /// </summary>
        public bool IncludeComplianceCheck { get; set; } = true;

        /// <summary>
        /// 是否包含成本效益分析
        /// 在推荐中包含成本效益评估
        /// </summary>
        public bool IncludeCostBenefitAnalysis { get; set; } = false;

        #endregion

        #region 输出格式控制

        /// <summary>
        /// 输出语言
        /// 推荐结果的显示语言
        /// </summary>
        public string OutputLanguage { get; set; } = "zh-CN";

        /// <summary>
        /// 是否包含可视化数据
        /// 在结果中包含图表和可视化所需的数据
        /// </summary>
        public bool IncludeVisualizationData { get; set; } = false;

        /// <summary>
        /// 是否包含执行建议
        /// 在结果中包含具体的执行建议和注意事项
        /// </summary>
        public bool IncludeExecutionAdvice { get; set; } = true;

        #endregion

        #region 构造函数

        public PersonnelAllocationRecommendationInput()
        {
            // 设置默认值
            WorkOrderIds = new List<long>();
            ExcludedPersonnelIds = new List<long>();
            PreferredPersonnelIds = new List<long>();
            CustomWeights = new Dictionary<string, double>();
            TimeRange = null;
        }

        public PersonnelAllocationRecommendationInput(List<long> workOrderIds, PersonnelAllocationStrategy strategy = PersonnelAllocationStrategy.FairDistribution)
        {
            WorkOrderIds = workOrderIds ?? new List<long>();
            Strategy = strategy;
            ExcludedPersonnelIds = new List<long>();
            PreferredPersonnelIds = new List<long>();
            CustomWeights = new Dictionary<string, double>();
            TimeRange = null;
        }

        #endregion
    }

    #region 相关枚举定义

    /// <summary>
    /// 推荐请求来源
    /// </summary>
    public enum RecommendationSource
    {
        /// <summary>
        /// 手动触发
        /// </summary>
        Manual = 1,

        /// <summary>
        /// 系统自动
        /// </summary>
        System = 2,

        /// <summary>
        /// 定时任务
        /// </summary>
        Scheduled = 3,

        /// <summary>
        /// 外部接口
        /// </summary>
        External = 4,

        /// <summary>
        /// 工作流触发
        /// </summary>
        Workflow = 5
    }

    /// <summary>
    /// 负载均衡策略
    /// </summary>
    public enum LoadBalanceStrategy
    {
        /// <summary>
        /// 保守策略
        /// 优先考虑人员休息和负载均衡
        /// </summary>
        Conservative = 1,

        /// <summary>
        /// 平衡策略
        /// 平衡效率和负载均衡
        /// </summary>
        Moderate = 2,

        /// <summary>
        /// 激进策略
        /// 优先考虑任务完成效率
        /// </summary>
        Aggressive = 3
    }

    /// <summary>
    /// 技能匹配策略
    /// </summary>
    public enum SkillMatchStrategy
    {
        /// <summary>
        /// 严格匹配
        /// 要求技能完全匹配
        /// </summary>
        Strict = 1,

        /// <summary>
        /// 平衡匹配
        /// 允许部分技能匹配，考虑学习能力
        /// </summary>
        Balanced = 2,

        /// <summary>
        /// 宽松匹配
        /// 允许技能相近，优先考虑人员发展
        /// </summary>
        Flexible = 3
    }

    #endregion

    #region 辅助类

    /// <summary>
    /// 日期范围
    /// </summary>
    public class DateRange
    {
        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime StartDate { get; set; }

        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime EndDate { get; set; }

        /// <summary>
        /// 时间范围跨度
        /// </summary>
        public TimeSpan Duration => EndDate - StartDate;
    }
    #endregion
}
