using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NPP.SmartSchedue.Api.Contracts.Core.Enums;
using NPP.SmartSchedue.Api.Contracts.Domain.Work;
using NPP.SmartSchedue.Api.Contracts.Services.Integration.Models;
using NPP.SmartSchedue.Api.Repositories.Work;

namespace NPP.SmartSchedue.Api.Services.Integration
{
    /// <summary>
    /// 任务状态验证器
    /// 专门负责步骤3：状态验证的详细分析
    /// </summary>
    public class WorkOrderStatusValidator
    {
        private readonly WorkOrderRepository _workOrderRepository;
        private readonly ILogger<WorkOrderStatusValidator> _logger;

        public WorkOrderStatusValidator(
            WorkOrderRepository workOrderRepository,
            ILogger<WorkOrderStatusValidator> logger)
        {
            _workOrderRepository = workOrderRepository;
            _logger = logger;
        }

        /// <summary>
        /// 修改操作状态验证（基于已查询的任务对象）
        /// </summary>
        public WorkOrderValidationResult ValidateModifyStatus(WorkOrderEntity task)
        {
            if (task == null)
            {
                return WorkOrderValidationResult.Fail("任务不存在");
            }

            try
            {
                // 详细状态分析
                switch ((WorkOrderStatusEnum)task.Status)
                {
                    case WorkOrderStatusEnum.PendingSubmit:
                    case WorkOrderStatusEnum.PendingReview:
                    case WorkOrderStatusEnum.PendingAssignment:
                    case WorkOrderStatusEnum.PendingIntegration:
                        // 这些状态允许修改
                        return WorkOrderValidationResult.Success();

                    case WorkOrderStatusEnum.Assigned:
                        // 已分配状态：需要检查是否已开始执行
                        if (task.ActualStartTime.HasValue)
                        {
                            return WorkOrderValidationResult.Fail("任务已开始执行，不允许修改");
                        }
                        return WorkOrderValidationResult.Success();

                    case WorkOrderStatusEnum.InProgress:
                        return WorkOrderValidationResult.Fail("任务进行中，不允许修改");

                    case WorkOrderStatusEnum.Completed:
                        return WorkOrderValidationResult.Fail("任务已完成，不允许修改");

                    case WorkOrderStatusEnum.Cancelled:
                        return WorkOrderValidationResult.Fail("任务已取消，不允许修改");

                    default:
                        return WorkOrderValidationResult.Fail("未知的任务状态");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "验证任务修改状态时发生错误，TaskId: {TaskId}", task.Id);
                return WorkOrderValidationResult.Fail($"验证失败：{ex.Message}");
            }
        }

        /// <summary>
        /// 删除操作状态验证（基于已查询的任务对象）
        /// </summary>
        public WorkOrderValidationResult ValidateDeleteStatus(WorkOrderEntity task)
        {
            if (task == null)
            {
                return WorkOrderValidationResult.Fail("任务不存在");
            }

            try
            {
                // 详细状态分析
                switch ((WorkOrderStatusEnum)task.Status)
                {
                    case WorkOrderStatusEnum.PendingSubmit:
                    case WorkOrderStatusEnum.PendingReview:
                    case WorkOrderStatusEnum.PendingAssignment:
                    case WorkOrderStatusEnum.PendingIntegration:
                        // 这些状态允许删除
                        return WorkOrderValidationResult.Success();

                    case WorkOrderStatusEnum.Assigned:
                        // 已分配但未开始可以删除
                        if (!task.ActualStartTime.HasValue)
                        {
                            return WorkOrderValidationResult.Success();
                        }
                        return WorkOrderValidationResult.Fail("任务已开始执行，不允许删除，请使用取消操作");

                    case WorkOrderStatusEnum.InProgress:
                        return WorkOrderValidationResult.Fail("任务进行中，不允许删除，请使用取消操作");

                    case WorkOrderStatusEnum.Completed:
                        return WorkOrderValidationResult.Fail("任务已完成，不允许删除");

                    case WorkOrderStatusEnum.Cancelled:
                        // 已取消的任务可以物理删除
                        return WorkOrderValidationResult.Success();

                    default:
                        return WorkOrderValidationResult.Fail("未知的任务状态");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "验证任务删除状态时发生错误，TaskId: {TaskId}", task.Id);
                return WorkOrderValidationResult.Fail($"验证失败：{ex.Message}");
            }
        }

        /// <summary>
        /// 取消操作状态验证（基于已查询的任务对象）
        /// </summary>
        public WorkOrderValidationResult ValidateCancelStatus(WorkOrderEntity task)
        {
            if (task == null)
            {
                return WorkOrderValidationResult.Fail("任务不存在");
            }

            try
            {
                // 详细状态分析
                switch ((WorkOrderStatusEnum)task.Status)
                {
                    case WorkOrderStatusEnum.PendingSubmit:
                    case WorkOrderStatusEnum.PendingReview:
                    case WorkOrderStatusEnum.PendingAssignment:
                    case WorkOrderStatusEnum.PendingIntegration:
                    case WorkOrderStatusEnum.Assigned:
                    case WorkOrderStatusEnum.InProgress:
                        // 这些状态允许取消
                        return WorkOrderValidationResult.Success();

                    case WorkOrderStatusEnum.Completed:
                        return WorkOrderValidationResult.Fail("任务已完成，不能取消");

                    case WorkOrderStatusEnum.Cancelled:
                        return WorkOrderValidationResult.Fail("任务已经是取消状态");

                    default:
                        return WorkOrderValidationResult.Fail("未知的任务状态");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "验证任务取消状态时发生错误，TaskId: {TaskId}", task.Id);
                return WorkOrderValidationResult.Fail($"验证失败：{ex.Message}");
            }
        }
    }
}