using CityInfo.WEB.Models;
using System.Net.Http.Json;

namespace CityInfo.WEB.Services
{
    public class CityDataService : ICityDataService
    {
        private readonly HttpClient _httpClient;
        private readonly ILogger<CityDataService> _logger;

        public CityDataService(HttpClient httpClient, ILogger<CityDataService> logger)
        {
            _httpClient = httpClient;
            _logger = logger;
        }

        public async Task<IEnumerable<City>> GetCities()
        {
            try
            {
                return await _httpClient.GetFromJsonAsync<IEnumerable<City>>("/api/cities")
                       ?? Enumerable.Empty<City>();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Erreur lors de la récupération des villes");
                return Enumerable.Empty<City>();
            }
        }

        public async Task<City?> GetCity(int cityId)
        {
            try
            {
                var response = await _httpClient.GetAsync($"/api/cities/{cityId}");

                if (response.IsSuccessStatusCode)
                {
                    return await response.Content.ReadFromJsonAsync<City>();
                }

                if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    return null;
                }

                _logger.LogWarning("Erreur HTTP {StatusCode} lors du chargement de la ville {CityId}",
                    response.StatusCode, cityId);

                return null;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Erreur lors de la récupération de la ville {CityId}", cityId);
                return null;
            }
        }

        public async Task<City?> CreateCity(City city)
        {
            try
            {
                var response = await _httpClient.PostAsJsonAsync("/api/cities", city);

                if (!response.IsSuccessStatusCode)
                {
                    _logger.LogWarning("Erreur HTTP {StatusCode} lors de la création d'une ville", response.StatusCode);
                    return null;
                }

                return await response.Content.ReadFromJsonAsync<City>();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Erreur lors de la création d'une ville");
                return null;
            }
        }

        public async Task<bool> UpdateCity(City city)
        {
            try
            {
                var response = await _httpClient.PutAsJsonAsync($"/api/cities/{city.Id}", city);
                return response.IsSuccessStatusCode;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Erreur lors de la mise à jour de la ville {CityId}", city.Id);
                return false;
            }
        }

        public async Task<bool> DeleteCity(int cityId)
        {
            try
            {
                var response = await _httpClient.DeleteAsync($"/api/cities/{cityId}");
                return response.IsSuccessStatusCode;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Erreur lors de la suppression de la ville {CityId}", cityId);
                return false;
            }
        }
    }
}