using JSMR.Application.Circles.Queries.Search;
using JSMR.Application.Creators.Commands.UpdateCreatorStatus;
using JSMR.Application.Creators.Queries.Search;
using JSMR.Application.Tags.Commands.SetEnglishName;
using JSMR.Application.Tags.Commands.UpdateTagStatus;
using JSMR.Application.Tags.Queries.Search;
using JSMR.Application.VoiceWorks.Commands.Delete;
using JSMR.Application.VoiceWorks.Commands.SetFavorite;
using JSMR.Application.VoiceWorks.Queries.Search;
using JSMR.UI.Blazor.Exceptions;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JSMR.UI.Blazor.Services;

public class VoiceWorksClient(HttpClient http)
{
    private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
    {
        Converters =
        {
            new JsonStringEnumConverter()
        }
    };

    private static async Task<T?> ReadJsonOrThrowAsync<T>(HttpResponseMessage response, CancellationToken cancellationToken)
    {
        if (!response.IsSuccessStatusCode)
        {
            string body = await response.Content.ReadAsStringAsync(cancellationToken);

            throw new ApiException(response.StatusCode, $"Request failed: {(int)response.StatusCode}", body);
        }

        return await response.Content.ReadFromJsonAsync<T>(JsonOptions, cancellationToken: cancellationToken);
    }

    public async Task<SearchVoiceWorksResponse?> SearchAsync(SearchVoiceWorksRequest request, CancellationToken ct = default)
    {
        using var resp = await http.PostAsJsonAsync("/api/voiceworks/search", request, ct);
        return await resp.Content.ReadFromJsonAsync<SearchVoiceWorksResponse>(JsonOptions, cancellationToken: ct);
    }

    public async Task<SearchCirclesResponse?> SearchAsync(SearchCirclesRequest request, CancellationToken ct = default)
    {
        using var resp = await http.PostAsJsonAsync("/api/circles/search", request, ct);
        return await resp.Content.ReadFromJsonAsync<SearchCirclesResponse>(JsonOptions, cancellationToken: ct);
    }

    public async Task<SearchCreatorsResponse?> SearchAsync(SearchCreatorsRequest request, CancellationToken ct = default)
    {
        using var resp = await http.PostAsJsonAsync("/api/creators/search", request, ct);
        return await resp.Content.ReadFromJsonAsync<SearchCreatorsResponse>(JsonOptions, cancellationToken: ct);
    }

    public async Task<SearchTagsResponse?> SearchAsync(SearchTagsRequest request, CancellationToken ct = default)
    {
        using var resp = await http.PostAsJsonAsync("/api/tags/search", request, ct);
        return await resp.Content.ReadFromJsonAsync<SearchTagsResponse>(JsonOptions, cancellationToken: ct);
    }

    public async Task<SetVoiceWorkFavoriteResponse?> SetVoiceWorkFavoriteeAsync(SetVoiceWorkFavoriteRequest request, CancellationToken ct = default)
    {
        using var resp = await http.PostAsJsonAsync("/api/voicework/set-favorite", request, ct);
        return await resp.Content.ReadFromJsonAsync<SetVoiceWorkFavoriteResponse>(JsonOptions, cancellationToken: ct);
    }

    public async Task<DeleteVoiceWorkResponse?> DeleteVoiceWorkAsync(DeleteVoiceWorkRequest request, CancellationToken ct = default)
    {
        using var resp = await http.PostAsJsonAsync("/api/voicework/delete", request, ct);
        //return await resp.Content.ReadFromJsonAsync<DeleteVoiceWorkResponse>(JsonOptions, cancellationToken: ct);
        return await ReadJsonOrThrowAsync<DeleteVoiceWorkResponse>(resp, ct);
    }

    public async Task<UpdateTagStatusResponse?> UpdateTagStatusAsync(UpdateTagStatusRequest request, CancellationToken ct = default)
    {
        using var resp = await http.PostAsJsonAsync("/api/tags/update-status", request, ct);
        return await resp.Content.ReadFromJsonAsync<UpdateTagStatusResponse>(JsonOptions, cancellationToken: ct);
    }

    public async Task<SetTagEnglishNameResponse?> SetTagEnglishNameAsync(SetTagEnglishNameRequest request, CancellationToken ct = default)
    {
        using var resp = await http.PostAsJsonAsync("/api/tags/set-english-name", request, ct);
        return await resp.Content.ReadFromJsonAsync<SetTagEnglishNameResponse>(JsonOptions, cancellationToken: ct);
    }

    public async Task<UpdateCreatorStatusResponse?> UpdateCreatorStatusAsync(UpdateCreatorStatusRequest request, CancellationToken ct = default)
    {
        using var resp = await http.PostAsJsonAsync("/api/creators/update-status", request, ct);
        return await resp.Content.ReadFromJsonAsync<UpdateCreatorStatusResponse>(JsonOptions, cancellationToken: ct);
    }
}