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

namespace NPP.SmartSchedue.Api.Contracts.Domain.Integration
{
    /// <summary>
    /// 整合记录变更详情
    /// 记录版本间的详细变更信息，支持变更分析和回滚操作
    /// </summary>
    public class IntegrationChangeDetails
    {
        /// <summary>
        /// 变更类型列表
        /// 可能的值：TaskModified, TaskAdded, TaskRemoved, PersonnelChanged, EquipmentChanged, 
        /// TaskTimeConflict, PersonnelUnavailable, EquipmentUnavailable, PriorityAdjusted
        /// </summary>
        public List<string> ChangeTypes { get; set; } = new();

        /// <summary>
        /// 发生变更的任务ID列表
        /// </summary>
        public List<long> ChangedTaskIds { get; set; } = new();

        /// <summary>
        /// 新增的任务ID列表
        /// </summary>
        public List<long> AddedTaskIds { get; set; } = new();

        /// <summary>
        /// 移除的任务ID列表
        /// </summary>
        public List<long> RemovedTaskIds { get; set; } = new();

        /// <summary>
        /// 受影响的人员ID列表
        /// </summary>
        public List<long> AffectedPersonnelIds { get; set; } = new();

        /// <summary>
        /// 受影响的设备ID列表
        /// </summary>
        public List<long> AffectedEquipmentIds { get; set; } = new();

        /// <summary>
        /// 具体的任务修改详情
        /// </summary>
        public List<TaskChangeDetail> TaskModifications { get; set; } = new();

        /// <summary>
        /// 人员分配变更详情
        /// </summary>
        public List<PersonnelAssignmentChange> PersonnelChanges { get; set; } = new();

        /// <summary>
        /// 设备分配变更详情
        /// </summary>
        public List<EquipmentAssignmentChange> EquipmentChanges { get; set; } = new();

        /// <summary>
        /// 变更触发的原始事件
        /// 记录导致变更的原始事件信息
        /// </summary>
        public ChangeTriggeredEvent TriggerEvent { get; set; } = new();

        /// <summary>
        /// 变更检测时间
        /// </summary>
        public DateTime DetectedTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 变更处理状态
        /// Detected-已检测, Processing-处理中, Completed-已完成, Failed-处理失败
        /// </summary>
        public string ProcessingStatus { get; set; } = "Detected";

        /// <summary>
        /// 变更影响分析
        /// </summary>
        public ChangeImpactAnalysis ImpactAnalysis { get; set; } = new();
    }

    /// <summary>
    /// 任务变更详情（Domain层）
    /// </summary>
    public class TaskChangeDetail
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        public long TaskId { get; set; }

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

        /// <summary>
        /// 修改类型
        /// TimeChanged-时间变更, PriorityChanged-优先级变更, PersonnelRequirementChanged-人员需求变更, 
        /// EquipmentRequirementChanged-设备需求变更, StatusChanged-状态变更
        /// </summary>
        public string ModificationType { get; set; } = string.Empty;

        /// <summary>
        /// 修改前的值（JSON格式）
        /// </summary>
        public string BeforeValue { get; set; } = "{}";

        /// <summary>
        /// 修改后的值（JSON格式）
        /// </summary>
        public string AfterValue { get; set; } = "{}";

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime ModifiedTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 修改说明
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 影响评估
        /// 评估这个修改对整体分配的影响程度
        /// </summary>
        public string ImpactAssessment { get; set; } = string.Empty;

        /// <summary>
        /// 变更类型（兼容性属性，映射到ModificationType）
        /// </summary>
        public string ChangeType => ModificationType;

        /// <summary>
        /// 影响级别（根据ImpactAssessment计算）
        /// </summary>
        public TaskChangeImpactLevel ImpactLevel 
        { 
            get 
            {
                if (ImpactAssessment.Contains("高影响") || ImpactAssessment.Contains("严重"))
                    return TaskChangeImpactLevel.High;
                if (ImpactAssessment.Contains("中等") || ImpactAssessment.Contains("一般"))
                    return TaskChangeImpactLevel.Medium;
                if (ImpactAssessment.Contains("低影响") || ImpactAssessment.Contains("轻微"))
                    return TaskChangeImpactLevel.Low;
                return TaskChangeImpactLevel.Medium; // 默认中等影响
            }
        }
    }

    /// <summary>
    /// 人员分配变更
    /// </summary>
    public class PersonnelAssignmentChange
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        public long TaskId { get; set; }

        /// <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 ChangeReason { get; set; } = string.Empty;

        /// <summary>
        /// 变更时间
        /// </summary>
        public DateTime ChangeTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 是否为自动变更
        /// </summary>
        public bool IsAutomaticChange { get; set; } = false;
    }

    /// <summary>
    /// 设备分配变更
    /// </summary>
    public class EquipmentAssignmentChange
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        public long TaskId { get; set; }

        /// <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 ChangeReason { get; set; } = string.Empty;

        /// <summary>
        /// 变更时间
        /// </summary>
        public DateTime ChangeTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 是否为自动变更
        /// </summary>
        public bool IsAutomaticChange { get; set; } = false;
    }

    /// <summary>
    /// 变更触发事件
    /// </summary>
    public class ChangeTriggeredEvent
    {
        /// <summary>
        /// 事件类型
        /// TaskModified-任务修改, UserRequest-用户请求, SystemOptimization-系统优化, 
        /// ResourceUnavailable-资源不可用, ConflictDetected-冲突检测
        /// </summary>
        public string EventType { get; set; } = string.Empty;

        /// <summary>
        /// 事件来源
        /// 标识触发变更的具体来源
        /// </summary>
        public string EventSource { get; set; } = string.Empty;

        /// <summary>
        /// 事件详情
        /// </summary>
        public string EventDetails { get; set; } = string.Empty;

        /// <summary>
        /// 事件发生时间
        /// </summary>
        public DateTime EventTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 触发用户ID
        /// </summary>
        public long? TriggeredByUserId { get; set; }

        /// <summary>
        /// 触发用户姓名
        /// </summary>
        public string TriggeredByUserName { get; set; } = string.Empty;
    }

    /// <summary>
    /// 变更影响分析
    /// </summary>
    public class ChangeImpactAnalysis
    {
        /// <summary>
        /// 影响范围
        /// Local-局部影响, Regional-区域影响, Global-全局影响
        /// </summary>
        public string ImpactScope { get; set; } = "Local";

        /// <summary>
        /// 影响严重程度
        /// Low-轻微, Medium-中等, High-严重, Critical-关键
        /// </summary>
        public string SeverityLevel { get; set; } = "Low";

        /// <summary>
        /// 预计影响的任务数量
        /// </summary>
        public int EstimatedAffectedTasks { get; set; } = 0;

        /// <summary>
        /// 预计影响的人员数量
        /// </summary>
        public int EstimatedAffectedPersonnel { get; set; } = 0;

        /// <summary>
        /// 预计影响的设备数量
        /// </summary>
        public int EstimatedAffectedEquipment { get; set; } = 0;

        /// <summary>
        /// 建议的处理策略
        /// AutoReallocation-自动重新分配, ManualReview-手动复审, FullReintegration-完全重新整合
        /// </summary>
        public string RecommendedAction { get; set; } = "AutoReallocation";

        /// <summary>
        /// 预计处理耗时（分钟）
        /// </summary>
        public int EstimatedProcessingTimeMinutes { get; set; } = 0;

        /// <summary>
        /// 风险评估
        /// </summary>
        public string RiskAssessment { get; set; } = string.Empty;

        /// <summary>
        /// 业务影响说明
        /// </summary>
        public string BusinessImpactDescription { get; set; } = string.Empty;
    }
}