using System.Net.Http.Json;
using ASTRAIN.Shared.Dtos;
using ASTRAIN.Shared.Requests;
using ASTRAIN.Shared.Responses;

namespace ASTRAIN.Client.Services;

public class ApiClient
{
    private readonly HttpClient _http;

    public ApiClient(HttpClient http)
    {
        _http = http;
    }

    public async Task<string> EnsureUserAsync(string? userId)
    {
        var response = await _http.GetFromJsonAsync<EnsureUserResponse>($"/api/users/ensure?userId={userId}");
        return response?.UserId ?? string.Empty;
    }

    public async Task<List<ExerciseDto>> GetExercisesAsync(string userId)
    {
        return await _http.GetFromJsonAsync<List<ExerciseDto>>($"/api/users/{userId}/exercises") ?? new List<ExerciseDto>();
    }

    public async Task<ExerciseDto?> CreateExerciseAsync(string userId, ExerciseUpsertRequest request)
    {
        var response = await _http.PostAsJsonAsync($"/api/users/{userId}/exercises", request);
        return await response.Content.ReadFromJsonAsync<ExerciseDto>();
    }

    public async Task<ExerciseDto?> UpdateExerciseAsync(string userId, int exerciseId, ExerciseUpsertRequest request)
    {
        var response = await _http.PutAsJsonAsync($"/api/users/{userId}/exercises/{exerciseId}", request);
        return await response.Content.ReadFromJsonAsync<ExerciseDto>();
    }

    public async Task DeleteExerciseAsync(string userId, int exerciseId)
    {
        await _http.DeleteAsync($"/api/users/{userId}/exercises/{exerciseId}");
    }

    public async Task<List<RoutineDto>> GetRoutinesAsync(string userId)
    {
        return await _http.GetFromJsonAsync<List<RoutineDto>>($"/api/users/{userId}/routines") ?? new List<RoutineDto>();
    }

    public async Task<RoutineDto?> GetRoutineAsync(string userId, int routineId)
    {
        return await _http.GetFromJsonAsync<RoutineDto>($"/api/users/{userId}/routines/{routineId}");
    }

    public async Task<RoutineDto?> CreateRoutineAsync(string userId, RoutineUpsertRequest request)
    {
        var response = await _http.PostAsJsonAsync($"/api/users/{userId}/routines", request);
        return await response.Content.ReadFromJsonAsync<RoutineDto>();
    }

    public async Task<RoutineDto?> UpdateRoutineAsync(string userId, int routineId, RoutineUpsertRequest request)
    {
        var response = await _http.PutAsJsonAsync($"/api/users/{userId}/routines/{routineId}", request);
        return await response.Content.ReadFromJsonAsync<RoutineDto>();
    }

    public async Task DeleteRoutineAsync(string userId, int routineId)
    {
        await _http.DeleteAsync($"/api/users/{userId}/routines/{routineId}");
    }

    public async Task<RoutineRunSummaryDto> GetLastRunAsync(string userId, int routineId)
    {
        return await _http.GetFromJsonAsync<RoutineRunSummaryDto>($"/api/users/{userId}/routines/{routineId}/last-run")
               ?? new RoutineRunSummaryDto(DateTime.MinValue, new List<RoutineRunEntryDto>());
    }

    public async Task SaveRunAsync(string userId, int routineId, RoutineRunRequest request)
    {
        await _http.PostAsJsonAsync($"/api/users/{userId}/routines/{routineId}/runs", request);
    }
}
