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

namespace PlanTempus.Application.Features.Employees.Components;

public class EmployeeTableViewComponent : ViewComponent
{
    private readonly ILocalizationService _localization;

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

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

public class EmployeeTableViewModel
{
    public required string Key { get; init; }
    public required int CurrentCount { get; init; }
    public required int MaxCount { get; init; }
    public required string CountLabel { get; init; }
    public required string InviteButtonText { get; init; }
    public required string ColumnUser { get; init; }
    public required string ColumnRole { get; init; }
    public required string ColumnStatus { get; init; }
    public required string ColumnLastActive { get; init; }
    public required IReadOnlyList<string> EmployeeKeys { get; init; }
    public double ProgressPercent => MaxCount > 0 ? (double)CurrentCount / MaxCount * 100 : 0;
}

internal class EmployeeTableData
{
    public required string Key { get; init; }
    public required int CurrentCount { get; init; }
    public required int MaxCount { get; init; }
    public required IReadOnlyList<string> EmployeeKeys { get; init; }
}

public static class EmployeeTableCatalog
{
    private static readonly Dictionary<string, EmployeeTableData> Tables = new()
    {
        ["all-employees"] = new EmployeeTableData
        {
            Key = "all-employees",
            CurrentCount = 5,
            MaxCount = 8,
            EmployeeKeys = ["employee-1", "employee-2", "employee-3", "employee-4", "employee-5"]
        }
    };

    public static EmployeeTableViewModel Get(string key, ILocalizationService localization)
    {
        if (!Tables.TryGetValue(key, out var table))
            throw new KeyNotFoundException($"EmployeeTable with key '{key}' not found");

        return new EmployeeTableViewModel
        {
            Key = table.Key,
            CurrentCount = table.CurrentCount,
            MaxCount = table.MaxCount,
            CountLabel = localization.Get("employees.users.count"),
            InviteButtonText = localization.Get("employees.users.inviteUser"),
            ColumnUser = localization.Get("employees.users.columns.user"),
            ColumnRole = localization.Get("employees.users.columns.role"),
            ColumnStatus = localization.Get("employees.users.columns.status"),
            ColumnLastActive = localization.Get("employees.users.columns.lastActive"),
            EmployeeKeys = table.EmployeeKeys
        };
    }
}
