using System.Text.Json;
using PaydayFrontend.Models;

namespace PaydayFrontend.Services;

public interface IUniversityService
{
    public Task<List<University>> GetAllUniversity(); 
    public Task<List<Direction>> GetDirectionsByUniversityId(long universityId);
    public Task<University> GetUniversityById(long id);
}

public class UniversityService : IUniversityService
{
    private readonly HttpClient _httpClient;

    public UniversityService(HttpClient httpClient)
    {
        _httpClient = httpClient;
        _httpClient.BaseAddress = new Uri("https://payday.zetcraft.ru");
    }

    public async Task<List<University>> GetAllUniversity()
    {
        var response = await _httpClient.GetAsync("v1/public/university");
        var result = await response.Content.ReadAsStringAsync();
        var university = JsonSerializer.Deserialize<List<University>>(result, new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        });
        return university;
    }
    
    public async Task<List<Direction>> GetDirectionsByUniversityId(long universityId)
    {
        var response = await _httpClient.GetAsync($"v1/public/university/{universityId}/directions");
        var result = await response.Content.ReadAsStringAsync();
        var directions = JsonSerializer.Deserialize<List<Direction>>(result, new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        });
        return directions;
    }
    
    public async Task<University> GetUniversityById(long id)
    {
        var response = await _httpClient.GetAsync("v1/public/university/" + id);
        var result = await response.Content.ReadAsStringAsync();
        var university = JsonSerializer.Deserialize<University>(result, new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        });
        return university;
    }
}