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

namespace NPP.SmartSchedue.Api.Contracts.Services.Integration.Output
{
    /// <summary>
    /// 版本对比结果
    /// 详细记录两个版本之间的差异分析
    /// </summary>
    public class VersionComparisonResult
    {
        /// <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 ComparisonTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 源版本信息
        /// </summary>
        public IntegrationVersionInfo SourceVersion { get; set; } = new();

        /// <summary>
        /// 目标版本信息
        /// </summary>
        public IntegrationVersionInfo TargetVersion { get; set; } = new();

        /// <summary>
        /// 版本差异摘要
        /// </summary>
        public VersionDifferenceSummary DifferenceSummary { get; set; } = new();

        /// <summary>
        /// 详细差异列表
        /// </summary>
        public List<DetailedDifference> DetailedDifferences { get; set; } = new();

        /// <summary>
        /// 性能对比分析
        /// </summary>
        public PerformanceComparison PerformanceComparison { get; set; } = new();

        /// <summary>
        /// 改进建议
        /// </summary>
        public List<string> ImprovementRecommendations { get; set; } = new();

        /// <summary>
        /// 回归风险评估
        /// </summary>
        public RegressionRiskAssessment RegressionRisk { get; set; } = new();
    }

    /// <summary>
    /// 版本差异摘要
    /// </summary>
    public class VersionDifferenceSummary
    {
        /// <summary>
        /// 总变更项数量
        /// </summary>
        public int TotalChangeCount { get; set; }

        /// <summary>
        /// 任务分配变更数量
        /// </summary>
        public int TaskAllocationChangeCount { get; set; }

        /// <summary>
        /// 人员分配变更数量
        /// </summary>
        public int PersonnelAllocationChangeCount { get; set; }

        /// <summary>
        /// 设备分配变更数量
        /// </summary>
        public int EquipmentAllocationChangeCount { get; set; }

        /// <summary>
        /// 新增任务数量
        /// </summary>
        public int AddedTaskCount { get; set; }

        /// <summary>
        /// 移除任务数量
        /// </summary>
        public int RemovedTaskCount { get; set; }

        /// <summary>
        /// 配置变更数量
        /// </summary>
        public int ConfigurationChangeCount { get; set; }

        /// <summary>
        /// 主要变更类型
        /// </summary>
        public List<string> MajorChangeTypes { get; set; } = new();

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

        /// <summary>
        /// 整体变更趋势
        /// Improved, Unchanged, Declined
        /// </summary>
        public string OverallTrend { get; set; } = "Unchanged";
    }

    /// <summary>
    /// 详细差异
    /// </summary>
    public class DetailedDifference
    {
        /// <summary>
        /// 差异类型
        /// TaskAssignment, PersonnelAllocation, EquipmentAllocation, Configuration, Statistics
        /// </summary>
        public string DifferenceType { get; set; } = string.Empty;

        /// <summary>
        /// 差异项目名称
        /// </summary>
        public string ItemName { get; set; } = string.Empty;

        /// <summary>
        /// 差异项目ID
        /// </summary>
        public long? ItemId { get; set; }

        /// <summary>
        /// 变更操作类型
        /// Added, Removed, Modified, Unchanged
        /// </summary>
        public string ChangeOperation { get; set; } = string.Empty;

        /// <summary>
        /// 源版本值
        /// </summary>
        public string SourceValue { get; set; } = string.Empty;

        /// <summary>
        /// 目标版本值
        /// </summary>
        public string TargetValue { get; set; } = string.Empty;

        /// <summary>
        /// 差异描述
        /// </summary>
        public string DifferenceDescription { get; set; } = string.Empty;

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

        /// <summary>
        /// 改进指标
        /// 正值表示改进，负值表示退化
        /// </summary>
        public decimal ImprovementMetric { get; set; }

        /// <summary>
        /// 相关联的其他变更
        /// </summary>
        public List<string> RelatedChanges { get; set; } = new();
    }

    /// <summary>
    /// 回归风险评估
    /// </summary>
    public class RegressionRiskAssessment
    {
        /// <summary>
        /// 整体回归风险等级
        /// </summary>
        public string OverallRiskLevel { get; set; } = "Medium";

        /// <summary>
        /// 质量回归风险评分
        /// </summary>
        public int QualityRegressionRisk { get; set; }

        /// <summary>
        /// 性能回归风险评分
        /// </summary>
        public int PerformanceRegressionRisk { get; set; }

        /// <summary>
        /// 业务连续性风险评分
        /// </summary>
        public int BusinessContinuityRisk { get; set; }

        /// <summary>
        /// 风险缓解建议
        /// </summary>
        public List<string> RiskMitigationSuggestions { get; set; } = new();
    }

    /// <summary>
    /// 版本回滚输入
    /// </summary>
    public class VersionRollbackInput
    {
        /// <summary>
        /// 当前版本ID
        /// </summary>
        public long CurrentVersionId { get; set; }

        /// <summary>
        /// 目标回滚版本ID
        /// </summary>
        public long TargetVersionId { get; set; }

        /// <summary>
        /// 回滚原因
        /// </summary>
        public string RollbackReason { get; set; } = string.Empty;

        /// <summary>
        /// 操作员用户ID
        /// </summary>
        public long OperatorUserId { get; set; }

        /// <summary>
        /// 操作员用户名
        /// </summary>
        public string OperatorUserName { get; set; } = string.Empty;

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

        /// <summary>
        /// 是否强制回滚
        /// 即使存在冲突也强制执行
        /// </summary>
        public bool ForceRollback { get; set; } = false;

        /// <summary>
        /// 回滚范围
        /// Full-完全回滚, Partial-部分回滚, ConfigOnly-仅配置回滚
        /// </summary>
        public string RollbackScope { get; set; } = "Full";

        /// <summary>
        /// 回滚后是否发布
        /// </summary>
        public bool PublishAfterRollback { get; set; } = false;

        /// <summary>
        /// 通知相关人员
        /// </summary>
        public bool NotifyAffectedPersonnel { get; set; } = true;

        /// <summary>
        /// 业务备注
        /// </summary>
        public string Remarks { get; set; } = string.Empty;
    }

    /// <summary>
    /// 版本回滚结果
    /// </summary>
    public class VersionRollbackResult
    {
        /// <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 RollbackTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 原版本ID
        /// </summary>
        public long OriginalVersionId { get; set; }

        /// <summary>
        /// 目标版本ID
        /// </summary>
        public long TargetVersionId { get; set; }

        /// <summary>
        /// 新创建的版本ID（回滚后的当前版本）
        /// </summary>
        public long? NewVersionId { get; set; }

        /// <summary>
        /// 回滚前验证结果
        /// </summary>
        public RollbackValidationResult ValidationResult { get; set; } = new();

        /// <summary>
        /// 回滚执行详情
        /// </summary>
        public List<RollbackExecutionDetail> ExecutionDetails { get; set; } = new();

        /// <summary>
        /// 回滚统计信息
        /// </summary>
        public RollbackStatistics 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> RecommendedNextActions { get; set; } = new();
    }

    /// <summary>
    /// 回滚验证结果
    /// </summary>
    public class RollbackValidationResult
    {
        /// <summary>
        /// 验证是否通过
        /// </summary>
        public bool IsValid { get; set; }

        /// <summary>
        /// 验证时间
        /// </summary>
        public DateTime ValidationTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 阻塞性错误
        /// </summary>
        public List<IntegrationValidationError> BlockingErrors { get; set; } = new();

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

        /// <summary>
        /// 冲突检测结果
        /// </summary>
        public ConflictDetectionResult ConflictDetection { get; set; } = new();

        /// <summary>
        /// 数据完整性检查结果
        /// </summary>
        public DataIntegrityCheckResult DataIntegrityCheck { get; set; } = new();
    }

    /// <summary>
    /// 冲突检测结果
    /// </summary>
    public class ConflictDetectionResult
    {
        /// <summary>
        /// 是否存在冲突
        /// </summary>
        public bool HasConflicts { get; set; }

        /// <summary>
        /// 冲突数量
        /// </summary>
        public int ConflictCount { get; set; }

        /// <summary>
        /// 具体冲突列表
        /// </summary>
        public List<ConflictDetail> Conflicts { get; set; } = new();
    }

    /// <summary>
    /// 冲突详情
    /// </summary>
    public partial class ConflictDetail
    {
        /// <summary>
        /// 冲突描述
        /// </summary>
        public string ConflictDescription { get; set; } = string.Empty;

        /// <summary>
        /// 涉及的资源ID
        /// </summary>
        public List<long> InvolvedResourceIds { get; set; } = new();

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

        /// <summary>
        /// 解决建议
        /// </summary>
        public List<string> ResolutionSuggestions { get; set; } = new();
    }

    /// <summary>
    /// 数据完整性检查结果
    /// </summary>
    public class DataIntegrityCheckResult
    {
        /// <summary>
        /// 数据完整性是否通过
        /// </summary>
        public bool IsDataIntegrityValid { get; set; }

        /// <summary>
        /// 缺失数据项
        /// </summary>
        public List<string> MissingDataItems { get; set; } = new();

        /// <summary>
        /// 无效数据项
        /// </summary>
        public List<string> InvalidDataItems { get; set; } = new();

        /// <summary>
        /// 数据一致性问题
        /// </summary>
        public List<string> ConsistencyIssues { get; set; } = new();
    }

    /// <summary>
    /// 回滚执行详情
    /// </summary>
    public class RollbackExecutionDetail
    {
        /// <summary>
        /// 执行步骤
        /// </summary>
        public string ExecutionStep { get; set; } = string.Empty;

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

        /// <summary>
        /// 执行状态
        /// </summary>
        public string ExecutionStatus { get; set; } = string.Empty;

        /// <summary>
        /// 执行结果描述
        /// </summary>
        public string ResultDescription { get; set; } = string.Empty;

        /// <summary>
        /// 执行耗时（毫秒）
        /// </summary>
        public long ElapsedMilliseconds { get; set; }

        /// <summary>
        /// 错误信息（如果有）
        /// </summary>
        public string ErrorMessage { get; set; } = string.Empty;
    }

    /// <summary>
    /// 回滚统计信息
    /// </summary>
    public class RollbackStatistics
    {
        /// <summary>
        /// 总执行耗时（毫秒）
        /// </summary>
        public long TotalElapsedMilliseconds { get; set; }

        /// <summary>
        /// 回滚的任务数量
        /// </summary>
        public int RolledBackTaskCount { get; set; }

        /// <summary>
        /// 回滚的人员分配数量
        /// </summary>
        public int RolledBackPersonnelAllocationCount { get; set; }

        /// <summary>
        /// 回滚的设备分配数量
        /// </summary>
        public int RolledBackEquipmentAllocationCount { get; set; }

        /// <summary>
        /// 恢复的配置项数量
        /// </summary>
        public int RestoredConfigurationCount { get; set; }

        /// <summary>
        /// 数据库操作次数
        /// </summary>
        public int DatabaseOperationCount { get; set; }

        /// <summary>
        /// 回滚成功率
        /// </summary>
        public decimal RollbackSuccessRate { get; set; }
    }

    /// <summary>
    /// 任务变更监听结果
    /// </summary>
    public class TaskChangeMonitoringResult
    {
        /// <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>
        /// 整合记录ID
        /// </summary>
        public long IntegrationRecordId { get; set; }

        /// <summary>
        /// 监听状态
        /// </summary>
        public bool MonitoringEnabled { get; set; }

        /// <summary>
        /// 监听的任务数量
        /// </summary>
        public int MonitoredTaskCount { get; set; }

        /// <summary>
        /// 监听配置
        /// </summary>
        public TaskChangeMonitoringConfig MonitoringConfig { get; set; } = new();

        /// <summary>
        /// 设置时间
        /// </summary>
        public DateTime ConfigurationTime { get; set; } = DateTime.Now;
    }

    /// <summary>
    /// 任务变更监听配置
    /// </summary>
    public class TaskChangeMonitoringConfig
    {
        /// <summary>
        /// 监听的事件类型
        /// </summary>
        public List<string> MonitoredEventTypes { get; set; } = new();

        /// <summary>
        /// 检查频率（分钟）
        /// </summary>
        public int CheckIntervalMinutes { get; set; } = 5;

        /// <summary>
        /// 自动处理阈值
        /// 影响评分低于此阈值时自动处理
        /// </summary>
        public int AutoProcessThreshold { get; set; } = 30;

        /// <summary>
        /// 是否启用实时通知
        /// </summary>
        public bool EnableRealTimeNotification { get; set; } = true;

        /// <summary>
        /// 批量处理大小
        /// </summary>
        public int BatchProcessingSize { get; set; } = 10;
    }

    /// <summary>
    /// 任务变更事件查询输入
    /// </summary>
    public class TaskChangeEventQueryInput
    {
        /// <summary>
        /// 整合记录ID
        /// </summary>
        public long? IntegrationRecordId { get; set; }

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

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

        /// <summary>
        /// 事件类型过滤
        /// </summary>
        public List<string> EventTypeFilter { get; set; } = new();

        /// <summary>
        /// 处理状态过滤
        /// </summary>
        public List<string> ProcessingStatusFilter { get; set; } = new();

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 触发用户过滤
        /// </summary>
        public List<long> TriggeredByUserFilter { get; set; } = new();

        /// <summary>
        /// 事件优先级过滤
        /// </summary>
        public int? MinPriority { get; set; }

        /// <summary>
        /// 最小影响评分
        /// </summary>
        public int? MinImpactScore { get; set; }

        /// <summary>
        /// 是否包含测试事件
        /// </summary>
        public bool IncludeTestEvents { get; set; } = false;

        /// <summary>
        /// 页码
        /// </summary>
        public int PageIndex { get; set; } = 1;

        /// <summary>
        /// 页大小
        /// </summary>
        public int PageSize { get; set; } = 20;

        /// <summary>
        /// 排序字段
        /// </summary>
        public string SortBy { get; set; } = "EventTime";

        /// <summary>
        /// 排序方向
        /// </summary>
        public string SortDirection { get; set; } = "Desc";
    }

    /// <summary>
    /// 任务变更事件信息
    /// </summary>
    public class TaskChangeEventInfo
    {
        /// <summary>
        /// 事件ID
        /// </summary>
        public long EventId { get; set; }

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

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

        /// <summary>
        /// 事件类型
        /// </summary>
        public string EventType { get; set; } = string.Empty;

        /// <summary>
        /// 变更字段
        /// </summary>
        public string ChangedFields { get; set; } = string.Empty;

        /// <summary>
        /// 事件时间
        /// </summary>
        public DateTime EventTime { get; set; }

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

        /// <summary>
        /// 事件优先级
        /// </summary>
        public int EventPriority { get; set; }

        /// <summary>
        /// 影响评估评分
        /// </summary>
        public int ImpactAssessmentScore { get; set; }

        /// <summary>
        /// 处理状态
        /// </summary>
        public string ProcessingStatus { get; set; } = string.Empty;

        /// <summary>
        /// 处理结果摘要
        /// </summary>
        public string ProcessingResultSummary { get; set; } = string.Empty;

        /// <summary>
        /// 是否触发了重新分配
        /// </summary>
        public bool TriggeredReallocation { get; set; }

        /// <summary>
        /// 创建的新版本ID
        /// </summary>
        public long? CreatedNewVersionId { get; set; }

        /// <summary>
        /// 通知状态
        /// </summary>
        public string NotificationStatus { get; set; } = string.Empty;

        /// <summary>
        /// 事件标签
        /// </summary>
        public string EventTags { get; set; } = string.Empty;

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

    /// <summary>
    /// 快照创建结果
    /// </summary>
    public class SnapshotCreationResult
    {
        /// <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>
        /// 整合记录ID
        /// </summary>
        public long IntegrationRecordId { get; set; }

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

        /// <summary>
        /// 快照版本号
        /// </summary>
        public int SnapshotVersion { get; set; }

        /// <summary>
        /// 快照数据大小（字节）
        /// </summary>
        public long SnapshotDataSizeBytes { get; set; }

        /// <summary>
        /// 包含的数据类型
        /// </summary>
        public List<string> IncludedDataTypes { get; set; } = new();

        /// <summary>
        /// 快照统计信息
        /// </summary>
        public SnapshotStatistics Statistics { get; set; } = new();

        /// <summary>
        /// 创建耗时（毫秒）
        /// </summary>
        public long CreationElapsedMilliseconds { get; set; }

        /// <summary>
        /// 快照ID
        /// 快照的唯一标识符
        /// </summary>
        public string SnapshotId { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 创建时间
        /// 快照创建的具体时间
        /// </summary>
        public DateTime CreationTime { get; set; } = DateTime.Now;
    }

    /// <summary>
    /// 快照统计信息
    /// </summary>
    public class SnapshotStatistics
    {
        /// <summary>
        /// 任务分配条数
        /// </summary>
        public int TaskAssignmentCount { get; set; }

        /// <summary>
        /// 人员工作负载条数
        /// </summary>
        public int PersonnelWorkloadCount { get; set; }

        /// <summary>
        /// 设备利用率条数
        /// </summary>
        public int EquipmentUtilizationCount { get; set; }

        /// <summary>
        /// 配置项数量
        /// </summary>
        public int ConfigurationItemCount { get; set; }

        /// <summary>
        /// 业务指标数量
        /// </summary>
        public int BusinessMetricCount { get; set; }

        /// <summary>
        /// 数据压缩率
        /// </summary>
        public decimal CompressionRatio { get; set; }
    }
}