using ConstructorAppUI.Dtos.ContactUsDtos;
using ConstructorAppUI.Dtos.ProjectDtos;
using ConstructorAppUI.Dtos.ReferenceDtos;
using ConstructorAppUI.Dtos.TestimonialDtos;
using ConstructorAppUI.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace ConstructorAppUI.Controllers
{
    
    public class DashboardController : Controller
    {
        private readonly string _signalRHubBaseUrl;
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly string _apiBaseUrl;

        public DashboardController(IConfiguration configuration, IHttpClientFactory httpClientFactory, IWebHostEnvironment webHostEnvironment)
        {
            _signalRHubBaseUrl = configuration["SignalRHubSettings:BaseUrl"];
            _httpClientFactory = httpClientFactory;
            _webHostEnvironment = webHostEnvironment;
            _apiBaseUrl = configuration["ApiSettings:BaseUrl"];
        }

        //[Authorize(Roles = "Admin")]
        public async Task<IActionResult> IndexAsync()
        {
            ViewBag.SignalRHubBaseUrl = _signalRHubBaseUrl;
            ViewBag.ApiBaseUrl = _apiBaseUrl;

            var client = _httpClientFactory.CreateClient();

            // İletişim Talepleri için Listeyi Çek
            var contactsResponse = await client.GetAsync($"{_apiBaseUrl}/api/ContactUs/CountAll");
            var contacts = new List<ResultContactUsDto>();
            var contactCount = 0;
            if (contactsResponse.IsSuccessStatusCode)
            {
                var contactJsonData = await contactsResponse.Content.ReadAsStringAsync();
                contactCount = JsonConvert.DeserializeObject<int>(contactJsonData);
            }

            // Projeler için Listeyi Çek
            var projectResponse = await client.GetAsync($"{_apiBaseUrl}/api/Project/CountAll");
            var projects = new List<ResultProjectDto>();
            var projectCount = 0;
            if (projectResponse.IsSuccessStatusCode)
            {
                var projectJsonData = await contactsResponse.Content.ReadAsStringAsync();
                projectCount = JsonConvert.DeserializeObject<int>(projectJsonData);
            }

            // Reference için Listeyi Çek
            var referenceResponse = await client.GetAsync($"{_apiBaseUrl}/api/Reference/CountAll");
            var references = new List<ResultReferenceDto>();
            var referenceCount = 0;
            if (referenceResponse.IsSuccessStatusCode)
            {
                var referenceJsonData = await referenceResponse.Content.ReadAsStringAsync();
                referenceCount = JsonConvert.DeserializeObject<int>(referenceJsonData);
            }

            // Testimonial için Listeyi Çek
            var testimonialResponse = await client.GetAsync($"{_apiBaseUrl}/api/Testimonial/CountAll");
            var testimonials = new List<ResultTestimonialDto>();
            var testimonialCount = 0;
            if (testimonialResponse.IsSuccessStatusCode)
            {
                var testimonialJsonData = await referenceResponse.Content.ReadAsStringAsync();
                testimonialCount = JsonConvert.DeserializeObject<int>(testimonialJsonData);
            }

            // ViewModel'i Doldur
            var viewModel = new DashboardIndexViewModel
            {
                ContactUsCount = contactCount,
                ProjectCount = projectCount,
                ReferenceCount = referenceCount,
                TestimonialCount = testimonialCount,
            };

            return View(viewModel);
        }
    }
}
