using Hotline.CallCenter.Calls;
using Hotline.Orders;
using Hotline.Repository.SqlSugar.CallCenter;
using Hotline.Share.Enums.Order;
using Hotline.Share.Tools;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;

namespace Hotline.Application.CallCenter;
public class CallNativeApplication : ICallNativeApplication, IScopeDependency
{
    private readonly ICallNativeRepository _callNativeRepository;
    private readonly ICallDomainService _callDomainService;
    private readonly IOrderVisitDomainService _orderVisitDomainService;

    public CallNativeApplication(ICallNativeRepository callNativeRepository, IOrderVisitDomainService orderVisitDomainService, ICallDomainService callDomainService)
    {
        _callNativeRepository = callNativeRepository;
        _orderVisitDomainService = orderVisitDomainService;
        _callDomainService = callDomainService;
    }

    /// <summary>
    /// 根据 OrderId 返回用户电话评价枚举
    /// 默认返回 默认满意
    /// </summary>
    /// <param name="orderId"></param>
    /// <returns></returns>
    public async Task<EVoiceEvaluate> GetReplyVoiceOrDefaultByOrderIdAsync(string orderId)
    {
        var callNative = await _callDomainService.GetByOrderIdAsync(orderId);
        if (callNative is null || callNative.ReplyTxt.IsNullOrEmpty())
            return EVoiceEvaluate.DefaultSatisfied;

        try
        {
            return _orderVisitDomainService.GetVisitEvaluateByReplyTxt<EVoiceEvaluate>(callNative.ReplyTxt!.Trim());
        }
        catch (UserFriendlyException)
        {
            return EVoiceEvaluate.DefaultSatisfied;
        }
    }
}
