using ConstructorAppUI.Dtos.FooterDtos;
using ConstructorAppUI.Dtos.HomeBannerDtos;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace ConstructorAppUI.ViewComponents.HomeLayoutComponents
{
    public class _HomeLayoutNavbarPartialComponent : ViewComponent
    {
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly string? _apiBaseUrl;

        public _HomeLayoutNavbarPartialComponent(IHttpClientFactory httpClientFactory, IConfiguration configuration)
        {
            _httpClientFactory = httpClientFactory;
            _apiBaseUrl = configuration["ApiSettings:BaseUrl"];
        }

        public async Task<IViewComponentResult> InvokeAsync()
        {
            var client = _httpClientFactory.CreateClient();

            // HomeBanner verisi çekiliyor
            var responseMessage = await client.GetAsync($"{_apiBaseUrl}/api/HomeBanner");
            var values = new List<ResultHomeBannerDto>();

            if (responseMessage.IsSuccessStatusCode)
            {
                var jsonData = await responseMessage.Content.ReadAsStringAsync();
                values = JsonConvert.DeserializeObject<List<ResultHomeBannerDto>>(jsonData);
            }

            // Footer verisi çekiliyor (telefon numarası için)
            var footerResponse = await client.GetAsync($"{_apiBaseUrl}/api/Footer");
            if (footerResponse.IsSuccessStatusCode)
            {
                var jsonFooter = await footerResponse.Content.ReadAsStringAsync();
                var footerData = JsonConvert.DeserializeObject<List<ResultFooterDto>>(jsonFooter);
                var phone = footerData.FirstOrDefault()?.Phone;

                ViewBag.FooterPhone = phone;
            }

            return View(values);
        }
    }
    }

