using Netina.Common.Models.Exception;
using Netina.Core.Abstracts;
using Netina.Domain.Models.Districts;
using Netina.Infrastructure.Models;
using Newtonsoft.Json;

namespace Netina.Infrastructure.Services;


public class DistrictService : IDistrictService
{
    public List<City> GetCitiesAsync(int? provinceId = null)
    {
        var cityList = File.ReadAllText(Path.Combine(DirectoryAddress.BaseDire, "cities.json"));
        var cities = JsonConvert.DeserializeObject<List<City>>(cityList);
        if (cities == null)
            throw new AppException("City json is null");
        if (provinceId != null)
            return cities.Where(c => c.province_id == provinceId.Value).ToList();
        return cities;

    }

    public List<Province> GetProvincesAsync()
    {
        var provinceList = File.ReadAllText(Path.Combine(DirectoryAddress.BaseDire, "provinces.json"));
        var provinces = JsonConvert.DeserializeObject<List<Province>>(provinceList);
        if (provinces == null)
            throw new AppException("Province json is null");
        return provinces;
    }
}