using AutoMapper;
using Microsoft.EntityFrameworkCore;
using PARR.Core.Repositories.Interfaces.TaskRepositories;
using PARR.Domain.DTOs.TaskDTO;
using PARR.Domain.Enums;

namespace PARR.Core.Services.TaskServices.Helpers
{
    /// <summary>
    /// Хелпеы для TaskManagement
    /// </summary>
    internal static class TaskManagementHelpers
    {
        /// <summary>
        /// Получить список активных задач
        /// </summary>
        /// <param name="taskRepository"></param>
        /// <param name="mapper"></param>
        /// <param name="typeCode"></param>
        /// <returns></returns>
        public static async Task<List<ActiveTask>> GetActiveTasksAsync(ITaskRepository taskRepository, IMapper mapper, TaskTypeEnum? typeCode = null)
        {
            var query = taskRepository.Get()
                            .AsNoTracking()
                            .Include(t => t.TaskStatus)
                            .Include(t => t.TaskType)
                            .Where(t => t.StatusCode == TaskItemStatusEnum.Pending || t.StatusCode == TaskItemStatusEnum.Processing);

            if (typeCode.HasValue)
                query = query.Where(t => t.TypeCode == typeCode.Value);

            var tasks = await query.OrderByDescending(t => t.DateCreated).ToListAsync();

            return mapper.Map<List<ActiveTask>>(tasks);
        }

    }
}
