using Hotline.Application.StatisticalReport.CallReport;
using Hotline.Caching.Interfaces;
using Hotline.Share.Dtos.CallCenter;
using Hotline.Share.Enums.CallCenter;
using Hotline.Share.Requests;
using Hotline.Share.Tools;
using Shouldly;
using SqlSugar.Extensions;

namespace Hotline.Tests.Application;
public class ZiGongCallReportApplicationTest
{
    private readonly ZiGongCallReportApplication _ziGongCallReportApplication;
    private readonly ISystemDicDataCacheManager _systemDicDataCacheManager;

    public ZiGongCallReportApplicationTest(ZiGongCallReportApplication ziGongCallReportApplication, ISystemDicDataCacheManager systemDicDataCacheManager)
    {
        _ziGongCallReportApplication = ziGongCallReportApplication;
        _systemDicDataCacheManager = systemDicDataCacheManager;
    }

    [Fact]
    public async Task QueryCallsDetailInTotal_Test()
    {
        var inDto = new BiQueryCallsDto
        {
            StartTime = "2024-10-30".ObjToDate(),
            EndTime = "2024-10-30 23:59:59".ObjToDate()
        };
        var (total, items) = await _ziGongCallReportApplication.QueryCallsDetailInTotalAsync(inDto, false);
        total.ShouldNotBe(0);
    }

    [Fact]
    public async Task QuerySeatCall_Test()
    {
        var inDto = new ReportRequiredPagedRequest
        {
            StartTime = "2024-06-01".ObjToDate(),
            EndTime = DateTime.Now
        };

        var result = await _ziGongCallReportApplication.QuerySeatCallAsync(inDto, CancellationToken.None);
        result.ShouldNotBeNull();
    }

    [Theory]
    [InlineData(null, null, null, null)]
    [InlineData(null, "19136073037", null, null)]
    [InlineData(null, null, "67387546", null)]
    [InlineData("20240805000001", null, null, null)]
    [InlineData(null, null, null, "From")]
    public async Task QueryCallsStatisticsDetail_Test(string? orderNo, string? fromNo, string? toNo, string? endBy)
    {
        return;
        var inDto = new QueryCallsStatisticsDetailInDto
        {
            StartTime = "2024-10-30".ObjToDate(),
            EndTime = "2024-10-30 23:59:59".ObjToDate(),
            OrderNo = orderNo,
            FromNo = fromNo,
            ToNo = toNo,
            EndBy = endBy?.ToEnum<EEndBy>()
        };
        var (total, items) = await _ziGongCallReportApplication.QueryCallsStatisticsDetailAsync(inDto, CancellationToken.None);
        total.ShouldNotBe(0);
        items.Any(m => m.OrderNo.NotNullOrEmpty()).ShouldBeTrue();
        items.Any(m => m.OrderTitle.NotNullOrEmpty()).ShouldBeTrue();
        if (fromNo != null)
            items.Any(m => m.FromNo != fromNo).ShouldBeFalse();
        if (toNo != null)
            items.Any(m => m.ToNo != toNo).ShouldBeFalse();
        if (orderNo != null)
            items.Any(m => m.OrderNo != orderNo).ShouldBeFalse();
        if (endBy != null)
            items.Any(m => m.EndBy != inDto.EndBy).ShouldBeFalse();
    }

    [Theory]
    [InlineData()]
    public async Task QueryCallsDetailStatistics_Test()
    {
        var inDto = new StartEndTimeDto 
        { 
            StartTime = "2024-07-29".ObjToDate(),
            EndTime = "2024-07-29 23:59:59".ObjToDate(),
        };
        var result = await _ziGongCallReportApplication.QueryCallsDetailStatisticsAsync(inDto, CancellationToken.None);
        result.Any(m => m.InConnectionRate.IsNullOrEmpty()).ShouldBeFalse();
        result.ShouldNotBeNull();
    }

    /// <summary>
    /// 测试通话时段分析中的数量是否和通话时段明细表中的数量是否相等
    /// </summary>
    /// <returns></returns>
    //[Fact]
    //public async Task HourCall_Test()
    //{
    //    var inDto = new BiQueryHourCallDto
    //    {
    //        StartTime = "2024-10-30 00:00:00".ObjToDate(),
    //        EndTime = "2024-10-30 23:59:59".ObjToDate()
    //    };

    //    var hourList = await _ziGongCallReportApplication.GetCallHourListAsync(inDto, CancellationToken.None);
    //    var hour = hourList.Where(m => m.Hour == 7).FirstOrDefault();
    //    var bInDto = new QueryCallListDto
    //    {
    //        Type = "effectiveCount",
    //        PageIndex = 1,
    //        PageSize = 20,
    //        StartHourTo = TimeSpan.Parse("07:00"),
    //        StartTime = "2024-10-30 00:00:00".ObjToDate(),
    //        EndTime = "2024-10-30 23:59:59".ObjToDate()
    //    };
    //    var bhour = await _ziGongCallReportApplication.GetCallListAsync(bInDto, CancellationToken.None).ToListAsync();
    //    bhour.Total.ShouldBe(hour.EffectiveCount, "effectiveCount 列表和详情的数量对不上");

    //    hour = hourList.Where(m => m.Hour == 12).FirstOrDefault();
    //    bInDto = new QueryCallListDto
    //    {
    //        Type = "connectByeCount",
    //        PageIndex = 1,
    //        PageSize = 20,
    //        StartHourTo = TimeSpan.Parse("12:00"),
    //        StartTime = "2024-10-30 00:00:00".ObjToDate(),
    //        EndTime = "2024-10-30 23:59:59".ObjToDate()
    //    };
    //    bhour = await _ziGongCallReportApplication.GetCallListAsync(bInDto, CancellationToken.None);
    //    bhour.Total.ShouldBe(hour.ConnectByeCount, "connectByeCount 列表和详情的数量对不上");

    //    hour = hourList.Where(m => m.Hour == 9).FirstOrDefault();
    //    bInDto = new QueryCallListDto
    //    {
    //        Type = "noConnectByeCount",
    //        PageIndex = 1,
    //        PageSize = 20,
    //        StartHourTo = TimeSpan.Parse("9:00"),
    //        StartTime = "2024-10-30 00:00:00".ObjToDate(),
    //        EndTime = "2024-10-30 23:59:59".ObjToDate()
    //    };
    //    bhour = await _ziGongCallReportApplication.GetCallListAsync(bInDto, CancellationToken.None);
    //    bhour.Total.ShouldBe(hour.NoConnectByeCount, "noConnectByeCount 列表和详情的数量对不上");

    //    hour = hourList.Where(m => m.Hour == 6).FirstOrDefault();
    //    bInDto = new QueryCallListDto
    //    {
    //        Type = "count",
    //        PageIndex = 1,
    //        PageSize = 20,
    //        StartHourTo = TimeSpan.Parse("06:00"),
    //        StartTime = "2024-10-30 00:00:00".ObjToDate(),
    //        EndTime = "2024-10-30 23:59:59".ObjToDate()
    //    };
    //    bhour = await _ziGongCallReportApplication.GetCallListAsync(bInDto, CancellationToken.None);
    //    bhour.Total.ShouldBe(hour.Count, "Count 列表和详情的数量对不上");

    //}

    [Fact]
    public async Task GetCallHotLineList_Test()
    {
        var dto = new BiQueryGateWayDto
        {
            StartTime = DateTime.Now.AddMonths(-1),
            EndTime = DateTime.Now
        };
        var result = await _ziGongCallReportApplication.GetCallHotLineListAsync(dto, CancellationToken.None);
        var hotlines = _systemDicDataCacheManager.CallForwardingSource.Select(m => m.DicDataValue).ToList();
        result.Any(m => !hotlines.Contains(m.GateWay)).ShouldBeFalse();
    }
}
