using CMSMicroservice.Application.Common.Interfaces;

namespace CMSMicroservice.Application.CommissionCQ.Queries.GetWorkerStatus;

public class GetWorkerStatusQueryHandler : IRequestHandler<GetWorkerStatusQuery, GetWorkerStatusResponseDto>
{
    private readonly IApplicationDbContext _context;
    private readonly IWeekDefinitionRepository _weekDefinitionRepository;

    public GetWorkerStatusQueryHandler(
        IApplicationDbContext context,
        IWeekDefinitionRepository weekDefinitionRepository)
    {
        _context = context;
        _weekDefinitionRepository = weekDefinitionRepository;
    }

    public async Task<GetWorkerStatusResponseDto> Handle(
        GetWorkerStatusQuery request,
        CancellationToken cancellationToken)
    {
        // TODO: این باید از یک service یا cache واقعی worker status را بگیرد
        // فعلاً mock data برمی‌گرداند
        
        await Task.CompletedTask;
        
        var currentWeek = _weekDefinitionRepository.GetCurrentWeek();
        var currentWeekNumber = currentWeek != null 
            ? $"{currentWeek.GregorianYear}-W{currentWeek.GregorianWeekNumber:D2}" 
            : null;

        return new GetWorkerStatusResponseDto
        {
            IsRunning = false,
            IsEnabled = true,
            CurrentExecutionId = null,
            CurrentWeekNumber = currentWeekNumber,
            CurrentWeekDisplayName = currentWeek?.DisplayName,
            CurrentStep = "Idle",
            LastRunAt = DateTime.Now.AddHours(-24),
            NextScheduledRun = DateTime.Now.AddDays(7),
            TotalExecutions = 48,
            SuccessfulExecutions = 47,
            FailedExecutions = 1
        };
    }
}
