using Hotline.CallCenter.Calls;
using Hotline.Orders;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hotline.Api.Controllers.Job
{
    public class ScheduledTasksController : BaseController
    {
        private readonly IOrderVisitRepository _orderVisitRepository;
        private readonly ILogger<ScheduledTasksController> _logger;
        private readonly IMediator _mediator;
        private readonly IOrderVisitDomainService _orderVisitDomainService;

        public ScheduledTasksController(IOrderVisitRepository orderVisitRepository,
            ILogger<ScheduledTasksController> logger,
            IMediator mediator,
            IOrderVisitDomainService orderVisitDomainService)
        {
            _orderVisitRepository = orderVisitRepository;
            _logger = logger;
            _mediator = mediator;
            _orderVisitDomainService = orderVisitDomainService;
        }

        /// <summary>
        /// 每天晚上凌晨处理前一天语音回访没处理的数据
        /// </summary>
        /// <returns></returns>
        [HttpPost("voice-evaluation-visit-job")]
        [AllowAnonymous]
        public async Task VoiceEvaluationVisitJob()
        {
            var list = await _orderVisitRepository.Queryable()
                .Where(p => p.VisitState == Share.Enums.Order.EVisitState.AiVisiting)
                .ToListAsync();
            if (list != null && list.Any())
            {
                foreach (var item in list)
                {
                    item.VisitState = Share.Enums.Order.EVisitState.WaitForVisit;
                    await _orderVisitRepository.UpdateAsync(item, HttpContext.RequestAborted);
                }
            }
        }
    }
}
