using Indotalent.ConfigBackEnd.Interfaces;
using Indotalent.ConfigFrontEnd.Common;
using Indotalent.Features.Serilogs.Database;
using Indotalent.Infrastructure.Authentication.Identity;
using Indotalent.Infrastructure.OData;
using Indotalent.Shared.Models;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using RestSharp;

namespace Indotalent.Features.Serilogs;

public class SerilogsService : BaseService
{
    public SerilogsService(
            IHttpClientFactory clientFactory,
            NavigationManager nav,
            ISnackbar snackbar,
            ICurrentUserService currentUserService,
            TokenProvider tokenProvider)
            : base(clientFactory, nav, snackbar, currentUserService, tokenProvider)
    {
    }

    public async Task<ApiResponse<PagedList<GetSerilogLogsPagedListResponse>>?> GetSerilogLogsPagedListAsync(ApiRequest apiRequest)
    {
        var client = new RestClient(CreateClient());
        var request = new RestRequest("api/serilog-logs", Method.Get);

        request.AddObject(apiRequest);

        return await ExecuteWithResponseAsync<PagedList<GetSerilogLogsPagedListResponse>>(client, request);
    }

    public async Task<bool> DeleteSerilogLogsByIdAsync(int id)
    {
        var client = new RestClient(CreateClient());

        var request = new RestRequest($"api/serilog-logs/delete/{id}", Method.Post);

        var response = await ExecuteWithResponseAsync<object>(client, request);

        return response?.IsSuccess ?? false;
    }

    public async Task<bool> DeleteSerilogLogsAllAsync()
    {
        var client = new RestClient(CreateClient());

        var request = new RestRequest("api/serilog-logs/clear-all", Method.Post);

        var response = await ExecuteWithResponseAsync<object>(client, request);

        return response?.IsSuccess ?? false;
    }

    public async Task<GetSerilogLogsByIdResponse?> GetSerilogLogsByIdAsync(int id)
    {
        var client = new RestClient(CreateClient());
        var request = new RestRequest($"api/serilog-logs/{id}", Method.Get);

        var response = await ExecuteWithResponseAsync<GetSerilogLogsByIdResponse>(client, request);

        return response?.Value;
    }

    public async Task<PagedList<GetSerilogLogsPagedListResponse>?> GetSerilogLogsODataAsync(int skip, int top, string? searchTerm)
    {
        var client = new RestClient(CreateClient());
        var request = new RestRequest("odata/SerilogLogs", Method.Get);

        request.AddHeader("Accept", "application/json");

        request.AddQueryParameter("$skip", skip.ToString());
        request.AddQueryParameter("$top", top.ToString());
        request.AddQueryParameter("$count", "true");
        request.AddQueryParameter("$orderby", "TimeStamp desc");

        if (!string.IsNullOrWhiteSpace(searchTerm))
        {
            string filter = $"contains(Message,'{searchTerm}') or contains(Level,'{searchTerm}')";
            request.AddQueryParameter("$filter", filter);
        }

        var response = await client.ExecuteAsync(request);

        if (response.IsSuccessful && !string.IsNullOrWhiteSpace(response.Content))
        {
            var data = System.Text.Json.JsonSerializer.Deserialize<ODataResponse<GetSerilogLogsPagedListResponse>>(response.Content, new System.Text.Json.JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true
            });

            if (data != null)
            {
                return new PagedList<GetSerilogLogsPagedListResponse>(data.Value, data.Count, skip, top);
            }
        }

        return null;
    }


}

