using Hotline.Ai.Quality;
using Hotline.Application.Handlers.FlowEngine;
using Hotline.FlowEngine.Notifications;
using Hotline.FlowEngine.WorkflowModules;
using MediatR;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hotline.DI;
using Hotline.EventBus;
using Hotline.Quality.Notifications;

namespace Hotline.Application.Quality
{
    [Injection(AppScopes = EAppScope.YiBin)]
	public class AiOrderQualityHandler : INotificationHandler<AiOrderQualityNotify>
	{
		private readonly IAiQualityService _aiQualityService;
		private readonly ILogger<AiOrderQualityHandler> _logger;
		public AiOrderQualityHandler(IAiQualityService aiQualityService, ILogger<AiOrderQualityHandler> logger) { _aiQualityService = aiQualityService; _logger = logger; }

		/// <summary>Handles a notification</summary>
		/// <param name="notification">The notification</param>
		/// <param name="cancellationToken">Cancellation token</param>
		public async Task Handle(AiOrderQualityNotify notification, CancellationToken cancellationToken)
		{
			try
			{
				await _aiQualityService.CreateAiOrderQualityTask(
					notification.QualityHandler.Id,
					notification.QualityHandler.AudioFile,
					notification.QualityHandler.FromNo,
					notification.QualityHandler.CallStartTime, notification.QualityHandler.ViteRecordPrefix, notification.QualityHandler.Source, cancellationToken);
			}
			catch (Exception e)
			{
				_logger.LogError($"推送质检信息错误! QualityId: {notification.QualityHandler.Id}，\r\n{e.Message}");
				throw;
			}
		}
	}
}
