using System.Globalization;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using PlanTempus.Application.Features.Localization.Services;

namespace PlanTempus.Application.Features.Suppliers.Components;

public class SupplierTableViewComponent : ViewComponent
{
    private readonly ILocalizationService _localization;
    private readonly IWebHostEnvironment _env;

    public SupplierTableViewComponent(ILocalizationService localization, IWebHostEnvironment env)
    {
        _localization = localization;
        _env = env;
    }

    public IViewComponentResult Invoke()
    {
        var data = LoadSupplierData();
        var model = new SupplierTableViewModel
        {
            SearchPlaceholder = _localization.Get("suppliers.searchPlaceholder"),
            ExportButtonText = _localization.Get("suppliers.export"),
            CreateButtonText = _localization.Get("suppliers.create"),
            ColumnSupplier = _localization.Get("suppliers.column.supplier"),
            ColumnContact = _localization.Get("suppliers.column.contact"),
            ColumnProducts = _localization.Get("suppliers.column.products"),
            ColumnLastOrder = _localization.Get("suppliers.column.lastOrder"),
            ColumnStatus = _localization.Get("suppliers.column.status"),
            EmptySearchText = _localization.Get("suppliers.emptySearch"),
            Suppliers = data.Suppliers
                .OrderBy(s => s.Name)
                .Select(s => new SupplierItemViewModel
                {
                    Id = s.Id,
                    Name = s.Name,
                    City = s.City,
                    ContactPerson = s.ContactPerson,
                    ProductCount = s.ProductCount,
                    LastOrderDate = FormatLastOrder(s.LastOrder),
                    IsActive = s.IsActive,
                    StatusClass = s.IsActive ? "active" : "inactive",
                    StatusText = s.IsActive
                        ? _localization.Get("suppliers.status.active")
                        : _localization.Get("suppliers.status.inactive")
                })
                .ToList()
        };

        return View(model);
    }

    private SupplierMockData LoadSupplierData()
    {
        var jsonPath = Path.Combine(_env.ContentRootPath, "Features", "Suppliers", "Data", "suppliersMock.json");
        var json = System.IO.File.ReadAllText(jsonPath);
        return JsonSerializer.Deserialize<SupplierMockData>(json, new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        }) ?? new SupplierMockData();
    }

    private static string FormatLastOrder(string dateStr)
    {
        if (DateTime.TryParse(dateStr, out var date))
        {
            return date.ToString("d. MMMM yyyy", new CultureInfo("da-DK"));
        }
        return dateStr;
    }
}

public class SupplierTableViewModel
{
    public required string SearchPlaceholder { get; init; }
    public required string ExportButtonText { get; init; }
    public required string CreateButtonText { get; init; }
    public required string ColumnSupplier { get; init; }
    public required string ColumnContact { get; init; }
    public required string ColumnProducts { get; init; }
    public required string ColumnLastOrder { get; init; }
    public required string ColumnStatus { get; init; }
    public required string EmptySearchText { get; init; }
    public required IReadOnlyList<SupplierItemViewModel> Suppliers { get; init; }
}

public class SupplierItemViewModel
{
    public required string Id { get; init; }
    public required string Name { get; init; }
    public required string City { get; init; }
    public required string ContactPerson { get; init; }
    public int ProductCount { get; init; }
    public required string LastOrderDate { get; init; }
    public bool IsActive { get; init; }
    public required string StatusClass { get; init; }
    public required string StatusText { get; init; }
}

internal class SupplierMockData
{
    public List<SupplierData> Suppliers { get; set; } = new();
}

internal class SupplierData
{
    public string Id { get; set; } = "";
    public string Name { get; set; } = "";
    public string City { get; set; } = "";
    public string ContactPerson { get; set; } = "";
    public int ProductCount { get; set; }
    public string LastOrder { get; set; } = "";
    public bool IsActive { get; set; }
}
