using Hotline.CallCenter.BlackLists;
using Hotline.CallCenter.Calls;
using Hotline.CallCenter.Tels;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.CallCenter;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Dtos.TrCallCenter;
using Hotline.Share.Enums.CallCenter;
using Hotline.Share.Enums.Order;
using SqlSugar;

namespace Hotline.Application.CallCenter
{
    public interface ICallApplication
    {
        /// <summary>
        /// 记录分机结束动作时间
        /// </summary>
        /// <param name="inDto"></param>
        /// <returns></returns>
        Task EndActionAsync(EndActionInDto inDto);

        /// <summary>
        /// 查询分机组和分机
        /// </summary>
        /// <returns></returns>
        Task<IReadOnlyList<TelGroupDto>> QueryGroupTel(CancellationToken cancellationToken);

        /// <summary>
        /// 查询分机
        /// </summary>
        Task<(int, IList<TelDto>)> QueryTelsAsync(QueryTelsInDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 查询分机组
        /// </summary>
        Task<IReadOnlyList<TelGroupDto>> QueryTelGroupsAsync(CancellationToken cancellationToken);

        #region 黑名单

        /// <summary>
        /// 新增黑名单
        /// </summary>
        Task<string> AddBlackListAsync(AddBlacklistDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 删除黑名单
        /// </summary>
        Task RemoveBlackListAsync(string id, CancellationToken cancellationToken);

        /// <summary>
        /// 查询黑名单
        /// </summary>
        Task<List<Blacklist>> QueryBlackListsAsync(CancellationToken cancellationToken);

        #endregion

        /// <summary>
        /// 签入
        /// </summary>
        Task<TrOnDutyResponseDto> SignInAsync(SignInDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 签出
        /// </summary>
        Task SingOutAsync(CancellationToken cancellationToken);

        /// <summary>
        /// 签出
        /// </summary>
        Task SingOutAsync(string telNo, CancellationToken cancellationToken);

        /// <summary>
        /// 查询当前用户的分机状态
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<TrOnDutyResponseDto> GetTelStateAsync(CancellationToken cancellationToken);

        /// <summary>
        /// 定量查询通话记录
        /// </summary>
        ISugarQueryable<CallNativeDto> QueryCallsFixedAsync(QueryCallsFixedDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 查询分机操作记录（定量）
        /// </summary>
        ISugarQueryable<TelOperation> QueryTelOperationsAsync(QueryTelOperationsFixedDto dto);

        /// <summary>
        /// 依据通话记录编号获取映射后的callId
        /// </summary>
        Task<string?> GetOrSetCallIdAsync(string? callNo, CancellationToken cancellationToken);

        /// <summary>
        /// 发送延迟消息让回访去关联通话记录
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<string> PublishVisitRelevanceCallIdAsync(OrderRelevanceCallIdDto dto, CancellationToken cancellationToken);

        Task<CallidRelation> GetRelationAsync(string callNo, CancellationToken cancellation);

        Task AddRelationAsync(CallidRelation relation, CancellationToken cancellation);

        /// <summary>
        /// 乐观并发更新映射关系
        /// </summary>
        Task<int> UpdateRelationOptLockAsync(CallidRelation relation, CancellationToken cancellationToken);

        /// <summary>
        /// 查询通话记录列表
        /// </summary>
        Task<List<CallNative>> GetCallListAsync(string callId, CancellationToken cancellationToken);

        /// <summary>
        /// 查询通话记录
        /// </summary>
        /// <param name="callId"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<CallNative?> GetCallAsync(string callId, CancellationToken cancellationToken);

        /// <summary>
        /// 查询通话记录
        /// 因为自贡的系统中有回访和通话记录未关联的异常数据, 使用此方法查询通话记录
        /// </summary>
        Task<CallNative> GetCallByTimeAndToNoAsync(string toNo, DateTime time, CancellationToken cancellationToken);

        /// <summary>
        /// 查询通话记录
        /// 因为自贡的系统中有回访和通话记录未关联的异常数据, 使用此方法查询通话记录
        /// </summary>
        Task<CallNative> GetCallByCallNoAsync(string callNo, CancellationToken cancellationToken);

        /// <summary>
        /// 查询通话记录
        /// </summary>
        Task<List<CallNative>> QueryCallsAsync(
            string? phone = null,
            ECallDirection? direction = null,
            DateTime? callStartTimeStart = null,
            DateTime? callStartTimeEnd = null,
            CancellationToken cancellationToken = default);

        #region tianrun

        Task<TrCallRecord?> GetTianrunCallAsync(string callId, CancellationToken cancellationToken);

		/// <summary>
		///  根据转写ID获取通话信息
		/// </summary>
		/// <returns></returns>
		Task<TrCallRecord?> GetTianrunCallTransliterationAsync(string transliterationId, CancellationToken cancellationToken);
		/// <summary>
		/// 修改转写状态
		/// </summary>
		/// <returns></returns>
		Task EditTransliterationStateAsync(string callId, ECallTransliterationState state,string transliterationId, CancellationToken cancellationToken);

		/// <summary>
		/// 关联通话记录与order（添润）
		/// </summary>
		Task RelateTianrunCallWithOrderAsync(string callId, string orderId, CancellationToken cancellationToken);

        /// <summary>
        /// 处理工单和通话记录关联问题
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<string> OrderRelateCallHandlerAsync(string orderId, CancellationToken cancellationToken);

        /// <summary>
        /// 查询通话记录
        /// </summary>
        Task<List<TrCallRecord>> QueryTianrunCallsAsync(
            string? phone = null,
            ECallDirection? direction = null,
            DateTime? callStartTimeStart = null,
            DateTime? callStartTimeEnd = null,
            CancellationToken cancellationToken = default);

        #endregion

        /// <summary>
        /// 查询分机操作选项
        /// </summary>
        /// <returns></returns>
        List<Kv> GetTelOperationOptions();

        /// <summary>
        /// 查询语音评价内容
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        Task<EVoiceEvaluate> GetReplyVoiceOrDefaultByOrderIdAsync(string orderId);

        /// <summary>
        /// 保存回访详情时发送延迟消息同步通话记录
        /// 如果回访通话记录有多条, 需要关联通话时长最长的那条
        /// </summary>
        Task OrderVisitRelevanceCallIdAsync(VisitDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 根据 OrderId 返回用户电话评价枚举
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        Task<ESeatEvaluate> GetSeatDefaultByOrderIdAsync(string orderId);
        Task ChangeTelModel(bool isCallOut, CancellationToken requestAborted);
    }
}