using Microsoft.AspNetCore.Mvc;
using PlanTempus.Application.Features.Localization.Services;

namespace PlanTempus.Application.Features.Dashboard.Components;

/// <summary>
/// ViewComponent for rendering a quick stat item in the sidebar.
/// </summary>
public class QuickStatViewComponent : ViewComponent
{
    private readonly ILocalizationService _localization;

    public QuickStatViewComponent(ILocalizationService localization)
    {
        _localization = localization;
    }

    public IViewComponentResult Invoke(string key)
    {
        var model = QuickStatCatalog.Get(key, _localization);
        return View(model);
    }
}

/// <summary>
/// ViewModel for the QuickStat component.
/// </summary>
public class QuickStatViewModel
{
    public required string Key { get; init; }
    public required string Value { get; init; }
    public required string Label { get; init; }
}

internal class QuickStatData
{
    public required string Key { get; init; }
    public required string Value { get; init; }
    public required string LabelKey { get; init; }
}

/// <summary>
/// Catalog of available quick stats with their data.
/// </summary>
public static class QuickStatCatalog
{
    private static readonly Dictionary<string, QuickStatData> Stats = new()
    {
        ["bookings-week"] = new QuickStatData
        {
            Key = "bookings-week",
            Value = "47",
            LabelKey = "dashboard.quickStats.bookings"
        },
        ["revenue-week"] = new QuickStatData
        {
            Key = "revenue-week",
            Value = "38.200 kr",
            LabelKey = "dashboard.quickStats.revenue"
        },
        ["new-customers"] = new QuickStatData
        {
            Key = "new-customers",
            Value = "8",
            LabelKey = "dashboard.quickStats.newCustomers"
        },
        ["avg-occupancy"] = new QuickStatData
        {
            Key = "avg-occupancy",
            Value = "72%",
            LabelKey = "dashboard.quickStats.avgOccupancy"
        }
    };

    public static QuickStatViewModel Get(string key, ILocalizationService localization)
    {
        if (!Stats.TryGetValue(key, out var stat))
            throw new KeyNotFoundException($"QuickStat with key '{key}' not found");

        return new QuickStatViewModel
        {
            Key = stat.Key,
            Value = stat.Value,
            Label = localization.Get(stat.LabelKey)
        };
    }

    public static IEnumerable<string> AllKeys => Stats.Keys;
}
