using Microsoft.AspNetCore.Mvc;
using Microsoft.ApplicationInsights;

namespace PlanTempus.Application.Components
{   
    public class OrganizationViewComponent : ApiViewComponentBase
    {
        private readonly TelemetryClient _telemetry;

        public OrganizationViewComponent(
            IHttpClientFactory httpClientFactory,
            TelemetryClient telemetry)
            : base(httpClientFactory, telemetry)
        {
            _telemetry = telemetry;
        }

        public async Task<IViewComponentResult> InvokeAsync(int organizationId, bool showDetailedView = true)
        {
            _telemetry.TrackEvent($"{GetType().Name}Invoked", new Dictionary<string, string>
            {
                ["OrganizationId"] = organizationId.ToString(),
                ["ShowDetailedView"] = showDetailedView.ToString()
            });

            var organization = await GetJObjectFromApiAsync($"/api/organization/get/{organizationId}");

            if (organization == null)
                return HandleError("Organization not found");

            ViewBag.ShowDetailedView = showDetailedView;

            
            _telemetry.TrackEvent("Viewed", new Dictionary<string, string>
            {
                ["OrganizationId"] = organizationId.ToString(),
                ["Name"] = organization["name"]?.ToString()
            });

            return View(organization);
        }
    }
}