using System;
using System.Collections.Generic;
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 CancelIntegrationRecordResult
{
    /// <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 CancelledTime { get; set; }

    /// <summary>
    /// 撤销的批次编码
    /// </summary>
    public string IntegrationBatchCode { get; set; } = string.Empty;

    /// <summary>
    /// 成功撤销的任务详情
    /// </summary>
    public List<CancelledTaskDetail> CancelledTasks { get; set; } = new();

    /// <summary>
    /// 撤销失败的任务详情
    /// </summary>
    public List<FailedCancelTaskDetail> FailedCancelTasks { get; set; } = new();

    /// <summary>
    /// 任务状态回滚结果
    /// </summary>
    public TaskStatusRollbackResult TaskStatusRollbackResult { get; set; } = new();

    /// <summary>
    /// 撤销前验证结果
    /// </summary>
    public CancelValidationResult ValidationResult { get; set; } = new();

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

    /// <summary>
    /// 撤销统计信息
    /// </summary>
    public CancelStatistics Statistics { get; set; } = new();

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

/// <summary>
/// 已撤销任务详情
/// </summary>
public class CancelledTaskDetail
{
    /// <summary>
    /// 任务ID
    /// </summary>
    public long TaskId { get; set; }

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

    /// <summary>
    /// 撤销前分配的人员ID
    /// </summary>
    public long? PreviousAssignedPersonnelId { get; set; }

    /// <summary>
    /// 撤销前分配的人员姓名
    /// </summary>
    public string PreviousAssignedPersonnelName { get; set; } = string.Empty;

    /// <summary>
    /// 撤销前分配的设备ID
    /// </summary>
    public long? PreviousAssignedEquipmentId { get; set; }

    /// <summary>
    /// 撤销前分配的设备名称
    /// </summary>
    public string PreviousAssignedEquipmentName { get; set; } = string.Empty;

    /// <summary>
    /// 撤销前状态
    /// </summary>
    public string PreviousStatus { get; set; } = string.Empty;

    /// <summary>
    /// 撤销后状态
    /// </summary>
    public string CurrentStatus { get; set; } = string.Empty;

    /// <summary>
    /// 任务撤销时间
    /// </summary>
    public DateTime CancelTime { get; set; }
}

/// <summary>
/// 撤销失败任务详情
/// </summary>
public class FailedCancelTaskDetail
{
    /// <summary>
    /// 任务ID
    /// </summary>
    public long TaskId { get; set; }

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

    /// <summary>
    /// 失败原因
    /// </summary>
    public string FailureReason { get; set; } = string.Empty;

    /// <summary>
    /// 详细错误信息
    /// </summary>
    public List<string> DetailedErrors { get; set; } = new();

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

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

/// <summary>
/// 任务状态回滚结果
/// </summary>
public class TaskStatusRollbackResult
{
    /// <summary>
    /// 回滚是否完全成功
    /// </summary>
    public bool IsFullySuccessful { get; set; }

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

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

    /// <summary>
    /// 回滚详细结果
    /// </summary>
    public List<TaskRollbackDetail> RollbackDetails { get; set; } = new();

    /// <summary>
    /// 回滚耗时（毫秒）
    /// </summary>
    public long RollbackElapsedMilliseconds { get; set; }
}

/// <summary>
/// 任务回滚详情
/// </summary>
public class TaskRollbackDetail
{
    /// <summary>
    /// 任务ID
    /// </summary>
    public long TaskId { get; set; }

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

    /// <summary>
    /// 回滚是否成功
    /// </summary>
    public bool IsRollbackSuccessful { get; set; }

    /// <summary>
    /// 原始状态
    /// </summary>
    public string OriginalStatus { get; set; } = string.Empty;

    /// <summary>
    /// 目标状态
    /// </summary>
    public string TargetStatus { get; set; } = string.Empty;

    /// <summary>
    /// 实际状态
    /// </summary>
    public string ActualStatus { get; set; } = string.Empty;

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

    /// <summary>
    /// 回滚时间
    /// </summary>
    public DateTime RollbackTime { get; set; }
}

/// <summary>
/// 撤销验证结果
/// </summary>
public class CancelValidationResult
{
    /// <summary>
    /// 验证是否通过
    /// </summary>
    public bool IsValid { get; set; }

    /// <summary>
    /// 验证执行时间
    /// </summary>
    public DateTime ValidationTime { get; set; }

    /// <summary>
    /// 阻塞性错误（会阻止撤销）
    /// </summary>
    public List<IntegrationValidationError> BlockingErrors { get; set; } = new();

    /// <summary>
    /// 警告性问题（不阻止撤销但需要注意）
    /// </summary>
    public List<ValidationWarning> Warnings { get; set; } = new();

    /// <summary>
    /// 验证详情
    /// </summary>
    public string ValidationDetails { get; set; } = string.Empty;

    /// <summary>
    /// 发现的进行中任务数量
    /// </summary>
    public int InProgressTaskCount { get; set; }

    /// <summary>
    /// 需要强制撤销的任务列表
    /// </summary>
    public List<long> TasksRequiringForceCancel { get; set; } = new();
}

/// <summary>
/// 撤销统计信息
/// </summary>
public class CancelStatistics
{
    /// <summary>
    /// 总任务数
    /// </summary>
    public int TotalTaskCount { get; set; }

    /// <summary>
    /// 成功撤销任务数
    /// </summary>
    public int SuccessfulCancelCount { get; set; }

    /// <summary>
    /// 撤销失败任务数
    /// </summary>
    public int FailedCancelCount { get; set; }

    /// <summary>
    /// 涉及人员数量
    /// </summary>
    public int InvolvedPersonnelCount { get; set; }

    /// <summary>
    /// 涉及设备数量
    /// </summary>
    public int InvolvedEquipmentCount { get; set; }

    /// <summary>
    /// 撤销成功率
    /// </summary>
    public decimal CancelSuccessRate { get; set; }

    /// <summary>
    /// 撤销总耗时（毫秒）
    /// </summary>
    public long TotalElapsedMilliseconds { get; set; }
}