using economy.Helpers;

namespace economy.Models.Anniversary
{
    public class AnniversaryModel
    {
        private readonly DataGoKR _dataGoKR;

        public AnniversaryModel(DataGoKR dataGoKR)
        {
            _dataGoKR = dataGoKR;
        }

        // 기념일 조회
        public async Task<Response> GetAnniversary(Request request)
        {
            Response parseData = new();

            try
            {
                var uriBuilder = new UriBuilder(_dataGoKR.APIUrl)
                {
                    Path = "/B090041/openapi/service/SpcdeInfoService/getAnniversaryInfo",
                    Query = $"ServiceKey={_dataGoKR.APIKey}&numOfRows=100&solYear={request.Year}"
                };

                var response = await _dataGoKR.httpClient.GetAsync(uriBuilder.Uri);

                if (response.IsSuccessStatusCode)
                {
                    var xmlString = await response.Content.ReadAsStringAsync();
                    parseData = await Common.ParseXmlDataAsync<Response>(xmlString);


                    if (parseData.Body is null)
                    {
                        return new Response();
                    }
                }

                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"Request error: {e.Message}");
            }

            return parseData;
        }
    }
}
