using System.Linq.Expressions;
using e_suite.API.Common;
using e_suite.API.Common.models;
using e_suite.API.Common.repository;
using e_suite.Database.Core.Tables.Diagnostics;
using e_suite.Utilities.Pagination;

namespace e_suite.Modules.PerformanceManager;

public class PerformanceManager : IPerformanceManager
{
    private readonly IPerformanceManagerRepository _performanceManagerRepository;

    public PerformanceManager(IPerformanceManagerRepository performanceManagerRepository)
    {
        _performanceManagerRepository = performanceManagerRepository;
    }

    public async Task<PaginatedData<ReadPerformanceReportSummary>> GetPerformanceSummaryAsync(Paging paging, CancellationToken cancellationToken)
    {
        var performanceReportSummary = _performanceManagerRepository.GetPerformanceReportSummary();


        var paginatedData = await PaginatedData.Paginate(performanceReportSummary, paging,
            KeySelector, cancellationToken);

        var paginatedResult = new PaginatedData<ReadPerformanceReportSummary>
        {
            Count = paginatedData.Count,
            Page = paginatedData.Page,
            PageSize = paginatedData.PageSize,
            Data = paginatedData.Data.Select(ToReadPerformanceReportSummary)

        };

        return paginatedResult;
    }

    private ReadPerformanceReportSummary ToReadPerformanceReportSummary(PerformanceReportSummary performanceReportSummary)
    {
        return new ReadPerformanceReportSummary(performanceReportSummary)
        {
            Host = performanceReportSummary.Host,
            ControllerName = performanceReportSummary.ControllerName,
            ActionName = performanceReportSummary.ActionName
        };
    }
    
    private Expression<Func<PerformanceReportSummary, object>> KeySelector(string? sortKey)
    {
        return sortKey?.ToLowerInvariant() switch
        {
            "host" => x => x.Host,
            "controllername" => x => x.ControllerName,
            "actionname" => x => x.ActionName,
            "requestType" => x => x.RequestType,
            "count" => x => x.Count,
            "mintotaltimems" => x => x.MinTotalTimeMs,
            "maxtotaltimemS" => x => x.MaxTotalTimeMs,
            "avgtotaltimems" => x => x.AvgTotalTimeMs,
            "sumtotaltimems" => x => x.SumTotalTimeMs,
            _ => x => x.SumTotalTimeMs
        };
    }

    public async Task<PaginatedData<ReadPerformanceReport>> GetPerformanceDetailsAsync(
        string hostName,
        string controllerName,
        string actionName,
        string requestType,
        Paging paging,
        CancellationToken cancellationToken
    )
    {
        var performanceReportSummary = _performanceManagerRepository.GetPerformanceReports(hostName, controllerName, actionName, requestType);
        
        var paginatedData = await PaginatedData.Paginate(performanceReportSummary, paging,
            PerformanceReportKeySelector, cancellationToken);

        var paginatedResult = new PaginatedData<ReadPerformanceReport>
        {
            Count = paginatedData.Count,
            Page = paginatedData.Page,
            PageSize = paginatedData.PageSize,
            Data = paginatedData.Data.Select(ToReadPerformanceReport)

        };

        return paginatedResult;
    }

    private ReadPerformanceReport ToReadPerformanceReport(PerformanceReport report)
    {
        return new ReadPerformanceReport(report)
        {
            Host = report.Host,
            ControllerName = report.ControllerName,
            ActionName = report.ActionName
        };
    }

    private Expression<Func<PerformanceReport, object>> PerformanceReportKeySelector(string? sortKey)
    {
        return sortKey?.ToLowerInvariant() switch
        {
            "host" => x => x.Host,
            "controllername" => x => x.ControllerName,
            "actionname" => x => x.ActionName,
            "requestType" => x => x.RequestType,
            "id" => x => x.Id,
            "actionparameters" => x => x.ActionParameters,
            "startdateyime" => x => x.StartDateTime,
            "timings" => x => x.Timings,
            "totaltimems" => x => x.TotalTimeMS,
            _ => x => x.StartDateTime
        };
    }
}