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

namespace NPP.SmartSchedue.Api.Contracts.Services.Integration.Output
{
    /// <summary>
    /// 处理任务变更结果
    /// 记录变更处理的详细结果和状态信息
    /// </summary>
    public class ProcessTaskChangesResult
    {
        /// <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 ProcessedTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 处理耗时（毫秒）
        /// </summary>
        public long ProcessingTime { get; set; }

        /// <summary>
        /// 处理消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 变更分析结果
        /// </summary>
        public List<TaskChangeAnalysisResult> ChangeAnalysis { get; set; } = new();

        /// <summary>
        /// 影响评估结果
        /// </summary>
        public ChangeImpactAnalysisResult ImpactAssessment { get; set; } = new();

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

        /// <summary>
        /// 变更检测结果
        /// </summary>
        public ChangeDetectionResult ChangeDetection { get; set; } = new();

        /// <summary>
        /// 影响分析结果
        /// </summary>
        public ChangeImpactAnalysisResult ImpactAnalysis { get; set; } = new();

        /// <summary>
        /// 重新分配结果
        /// 如果触发了重新分配
        /// </summary>
        public SmartReallocationResult? ReallocationResult { get; set; }

        /// <summary>
        /// 新创建的版本信息
        /// 如果创建了新版本
        /// </summary>
        public IntegrationVersionInfo? NewVersionInfo { get; set; }

        /// <summary>
        /// 处理统计信息
        /// </summary>
        public ChangeProcessingStatistics Statistics { get; set; } = new();

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

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

        /// <summary>
        /// 建议操作
        /// </summary>
        public List<string> RecommendedActions { get; set; } = new();
    }

    /// <summary>
    /// 变更检测结果
    /// </summary>
    public class ChangeDetectionResult
    {
        /// <summary>
        /// 检测是否成功
        /// </summary>
        public bool IsSuccess { get; set; } = true;

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

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

        /// <summary>
        /// 检测到的变更总数
        /// </summary>
        public int DetectedChangeCount { get; set; }

        /// <summary>
        /// 检测到的变更总数
        /// </summary>
        public int TotalChangesDetected { get; set; }

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

        /// <summary>
        /// 按类型分类的变更统计
        /// </summary>
        public Dictionary<string, int> ChangesByType { get; set; } = new();

        /// <summary>
        /// 详细的变更列表
        /// </summary>
        public List<DetailedTaskChange> DetailedChanges { get; set; } = new();

        /// <summary>
        /// 检测执行时间
        /// </summary>
        public DateTime DetectionTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 检测耗时（毫秒）
        /// </summary>
        public long DetectionElapsedMilliseconds { get; set; }

        /// <summary>
        /// 检测状态
        /// Completed-完成, PartiallyCompleted-部分完成, Failed-失败
        /// </summary>
        public string DetectionStatus { get; set; } = "Completed";

        /// <summary>
        /// 检测覆盖的任务数量
        /// </summary>
        public int TasksCovered { get; set; }

        /// <summary>
        /// 检测失败的任务
        /// </summary>
        public List<long> FailedTaskIds { get; set; } = new();
    }

    /// <summary>
    /// 详细任务变更信息
    /// </summary>
    public class DetailedTaskChange
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        public long TaskId { get; set; }

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

        /// <summary>
        /// 变更类型
        /// </summary>
        public string ChangeType { get; set; } = string.Empty;

        /// <summary>
        /// 变更描述
        /// </summary>
        public string ChangeDescription { get; set; } = string.Empty;

        /// <summary>
        /// 变更前的状态快照
        /// </summary>
        public TaskStateSnapshot BeforeState { get; set; } = new();

        /// <summary>
        /// 变更后的状态快照
        /// </summary>
        public TaskStateSnapshot AfterState { get; set; } = new();

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

        /// <summary>
        /// 影响评估
        /// </summary>
        public string ImpactAssessment { get; set; } = string.Empty;

        /// <summary>
        /// 是否需要重新分配
        /// </summary>
        public bool RequiresReallocation { get; set; }

        /// <summary>
        /// 建议的处理动作
        /// </summary>
        public List<string> SuggestedActions { get; set; } = new();
    }

    /// <summary>
    /// 任务状态快照
    /// </summary>
    public class TaskStateSnapshot
    {
        /// <summary>
        /// 工作日期
        /// </summary>
        public DateTime WorkOrderDate { get; set; }

        /// <summary>
        /// 班次ID
        /// </summary>
        public long ShiftId { get; set; }

        /// <summary>
        /// 优先级
        /// </summary>
        public int Priority { get; set; }

        /// <summary>
        /// 紧急程度
        /// </summary>
        public int Urgency { get; set; }

        /// <summary>
        /// 复杂度
        /// </summary>
        public int Complexity { get; set; }

        /// <summary>
        /// 预估工时
        /// </summary>
        public decimal? EstimatedHours { get; set; }

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

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

        /// <summary>
        /// 任务状态
        /// </summary>
        public string TaskStatus { get; set; } = string.Empty;

        /// <summary>
        /// 其他关键属性
        /// </summary>
        public Dictionary<string, object> AdditionalProperties { get; set; } = new();
    }

    /// <summary>
    /// 变更影响分析结果
    /// </summary>
    public class ChangeImpactAnalysisResult
    {
        /// <summary>
        /// 总体影响评分（0-100）
        /// </summary>
        public int OverallImpactScore { get; set; }

        /// <summary>
        /// 影响范围评估
        /// </summary>
        public ImpactScopeAssessment ScopeAssessment { get; set; } = new();

        /// <summary>
        /// 受影响的资源统计
        /// </summary>
        public AffectedResourcesStatistics AffectedResources { get; set; } = new();

        /// <summary>
        /// 风险评估
        /// </summary>
        public RiskAssessment RiskAssessment { get; set; } = new();

        /// <summary>
        /// 建议的处理策略
        /// </summary>
        public string RecommendedStrategy { get; set; } = string.Empty;

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

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

        /// <summary>
        /// 技术影响描述
        /// </summary>
        public string TechnicalImpactDescription { get; set; } = string.Empty;

        /// <summary>
        /// 分析执行时间
        /// </summary>
        public DateTime AnalysisTime { get; set; } = DateTime.Now;

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

    /// <summary>
    /// 影响范围评估
    /// </summary>
    public class ImpactScopeAssessment
    {
        /// <summary>
        /// 影响范围类别
        /// Local, Regional, Global
        /// </summary>
        public string ScopeCategory { get; set; } = "Local";

        /// <summary>
        /// 直接影响的任务数量
        /// </summary>
        public int DirectlyAffectedTaskCount { get; set; }

        /// <summary>
        /// 间接影响的任务数量
        /// </summary>
        public int IndirectlyAffectedTaskCount { get; set; }

        /// <summary>
        /// 影响的批次数量
        /// </summary>
        public int AffectedBatchCount { get; set; }

        /// <summary>
        /// 影响的项目数量
        /// </summary>
        public int AffectedProjectCount { get; set; }

        /// <summary>
        /// 跨班次影响
        /// </summary>
        public bool CrossShiftImpact { get; set; }

        /// <summary>
        /// 跨部门影响
        /// </summary>
        public bool CrossDepartmentImpact { get; set; }
    }

    /// <summary>
    /// 受影响的资源统计
    /// </summary>
    public class AffectedResourcesStatistics
    {
        /// <summary>
        /// 受影响的人员数量
        /// </summary>
        public int AffectedPersonnelCount { get; set; }

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

        /// <summary>
        /// 受影响的设备数量
        /// </summary>
        public int AffectedEquipmentCount { get; set; }

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

        /// <summary>
        /// 需要重新协调的资源数量
        /// </summary>
        public int ResourcesRequiringRecoordination { get; set; }

        /// <summary>
        /// 资源冲突数量
        /// </summary>
        public int ResourceConflictCount { get; set; }
    }

    /// <summary>
    /// 受影响的人员信息
    /// </summary>
    public class AffectedPersonnelInfo
    {
        /// <summary>
        /// 人员ID
        /// </summary>
        public long PersonnelId { get; set; }

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

        /// <summary>
        /// 影响类型
        /// WorkloadChanged, TaskReassigned, ScheduleConflict
        /// </summary>
        public string ImpactType { get; set; } = string.Empty;

        /// <summary>
        /// 影响描述
        /// </summary>
        public string ImpactDescription { get; set; } = string.Empty;

        /// <summary>
        /// 影响严重程度
        /// Low, Medium, High, Critical
        /// </summary>
        public string SeverityLevel { get; set; } = "Medium";

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

    /// <summary>
    /// 受影响的设备信息
    /// </summary>
    public class AffectedEquipmentInfo
    {
        /// <summary>
        /// 设备ID
        /// </summary>
        public long EquipmentId { get; set; }

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

        /// <summary>
        /// 影响类型
        /// UtilizationChanged, TaskReassigned, MaintenanceConflict
        /// </summary>
        public string ImpactType { get; set; } = string.Empty;

        /// <summary>
        /// 影响描述
        /// </summary>
        public string ImpactDescription { get; set; } = string.Empty;

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

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


    /// <summary>
    /// 风险项目
    /// </summary>
    public class RiskItem
    {
        /// <summary>
        /// 风险类型
        /// </summary>
        public string RiskType { get; set; } = string.Empty;

        /// <summary>
        /// 风险描述
        /// </summary>
        public string RiskDescription { get; set; } = string.Empty;

        /// <summary>
        /// 风险等级
        /// </summary>
        public string RiskLevel { get; set; } = string.Empty;

        /// <summary>
        /// 风险概率
        /// </summary>
        public decimal Probability { get; set; }

        /// <summary>
        /// 影响程度
        /// </summary>
        public decimal Impact { get; set; }

        /// <summary>
        /// 风险值（概率 x 影响）
        /// </summary>
        public decimal RiskValue { get; set; }

        /// <summary>
        /// 缓解措施
        /// </summary>
        public List<string> MitigationActions { get; set; } = new();
    }

    /// <summary>
    /// 变更处理统计信息
    /// </summary>
    public class ChangeProcessingStatistics
    {
        /// <summary>
        /// 总处理耗时（毫秒）
        /// </summary>
        public long TotalElapsedMilliseconds { get; set; }

        /// <summary>
        /// 检测阶段耗时（毫秒）
        /// </summary>
        public long DetectionElapsedMilliseconds { get; set; }

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

        /// <summary>
        /// 处理阶段耗时（毫秒）
        /// </summary>
        public long ProcessingElapsedMilliseconds { get; set; }

        /// <summary>
        /// 处理的变更数量
        /// </summary>
        public int ProcessedChangeCount { get; set; }

        /// <summary>
        /// 成功处理的变更数量
        /// </summary>
        public int SuccessfulChangeCount { get; set; }

        /// <summary>
        /// 失败处理的变更数量
        /// </summary>
        public int FailedChangeCount { get; set; }

        /// <summary>
        /// 创建的新版本数量
        /// </summary>
        public int CreatedVersionCount { get; set; }

        /// <summary>
        /// 触发的重新分配数量
        /// </summary>
        public int ReallocationTriggeredCount { get; set; }

        /// <summary>
        /// 发送的通知数量
        /// </summary>
        public int NotificationsSentCount { get; set; }

        /// <summary>
        /// 处理成功率
        /// </summary>
        public decimal ProcessingSuccessRate { get; set; }
    }

    /// <summary>
    /// 整合版本信息
    /// </summary>
    public class IntegrationVersionInfo
    {
        /// <summary>
        /// 版本ID
        /// </summary>
        public long VersionId { get; set; }

        /// <summary>
        /// 版本号
        /// </summary>
        public int VersionNumber { get; set; }

        /// <summary>
        /// 根版本ID
        /// </summary>
        public long RootVersionId { get; set; }

        /// <summary>
        /// 父版本ID
        /// </summary>
        public long? ParentVersionId { get; set; }

        /// <summary>
        /// 版本创建时间
        /// </summary>
        public DateTime CreatedTime { get; set; }

        /// <summary>
        /// 版本创建原因
        /// </summary>
        public string CreationReason { get; set; } = string.Empty;

        /// <summary>
        /// 变更摘要
        /// </summary>
        public string ChangeSummary { get; set; } = string.Empty;

        /// <summary>
        /// 是否为活跃版本
        /// </summary>
        public bool IsActiveVersion { get; set; }

        /// <summary>
        /// 变更影响评分
        /// </summary>
        public int ChangeImpactScore { get; set; }

        /// <summary>
        /// 创建者信息
        /// </summary>
        public string CreatedBy { get; set; } = string.Empty;

        /// <summary>
        /// 发布时间
        /// </summary>
        public DateTime? PublishedTime { get; set; }

        /// <summary>
        /// 版本创建原因
        /// </summary>
        public string VersionReason { get; set; } = string.Empty;

        /// <summary>
        /// 创建者用户名
        /// </summary>
        public string CreatedByUserName { get; set; } = string.Empty;

        /// <summary>
        /// 发布者用户名
        /// </summary>
        public string PublishedByUserName { get; set; } = string.Empty;

        /// <summary>
        /// 任务总数
        /// </summary>
        public int TaskCount { get; set; }

        /// <summary>
        /// 成功任务数量
        /// </summary>
        public int SuccessTaskCount { get; set; }

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

        /// <summary>
        /// 性能指标
        /// </summary>
        public IntegrationPerformanceMetrics PerformanceMetrics { get; set; } = new();

        /// <summary>
        /// 发布状态
        /// </summary>
        public string PublishStatus { get; set; } = "Draft";

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreationTime { get; set; } = DateTime.Now;
    }

    /// <summary>
    /// 整合版本历史查询结果
    /// </summary>
    public class IntegrationVersionHistoryResult
    {
        /// <summary>
        /// 整合记录ID
        /// </summary>
        public long IntegrationRecordId { get; set; }

        /// <summary>
        /// 查询时间
        /// </summary>
        public DateTime QueryTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess { get; set; } = true;

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

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

        /// <summary>
        /// 版本记录列表
        /// </summary>
        public List<IntegrationRecordEntity> VersionRecords { get; set; } = new();

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 版本统计信息
        /// </summary>
        public VersionStatistics Statistics { get; set; } = new();

        /// <summary>
        /// 变更历史时间线
        /// </summary>
        public List<ChangeHistoryItem> ChangeHistory { get; set; } = new();

        /// <summary>
        /// 版本历史信息列表
        /// </summary>
        public List<IntegrationVersionInfo> VersionHistory { get; set; } = new();

        /// <summary>
        /// 版本对比结果
        /// </summary>
        public VersionComparisonResult? VersionComparison { get; set; }

        /// <summary>
        /// 版本统计信息
        /// </summary>
        public VersionStatistics VersionStatistics { get; set; } = new();

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

        /// <summary>
        /// 版本信息列表
        /// </summary>
        public List<IntegrationVersionInfo> VersionInfos { get; set; } = new();
    }

    /// <summary>
    /// 版本统计信息
    /// </summary>
    public class VersionStatistics
    {
        /// <summary>
        /// 总版本数
        /// </summary>
        public int TotalVersions { get; set; }

        /// <summary>
        /// 活跃版本数
        /// </summary>
        public int ActiveVersions { get; set; }

        /// <summary>
        /// 平均版本间隔时间（分钟）
        /// </summary>
        public double AverageVersionInterval { get; set; }
    }

    /// <summary>
    /// 变更历史项目
    /// </summary>
    public class ChangeHistoryItem
    {
        /// <summary>
        /// 版本ID
        /// </summary>
        public long VersionId { get; set; }

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

        /// <summary>
        /// 变更类型
        /// </summary>
        public string ChangeType { get; set; } = string.Empty;

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

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

}