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

namespace PlanTempus.Application.Features.Customers.Components;

public class CustomerDetailGiftcardsViewComponent : ViewComponent
{
    private readonly ILocalizationService _localization;

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

    public IViewComponentResult Invoke(string customerId)
    {
        var customer = CustomerDetailCatalog.Get(customerId);
        var culture = new CultureInfo("da-DK");

        // Separate giftcards and punchcards
        var giftcards = customer.Giftcards.Active.Where(g => g.Type == "giftcard").ToList();
        var punchcards = customer.Giftcards.Active.Where(g => g.Type == "punchcard").ToList();

        var model = new CustomerDetailGiftcardsViewModel
        {
            GiftcardsTitle = "Aktive gavekort",
            PunchcardsTitle = "Klippekort",
            ExpiredTitle = "Udlobne / Brugte",
            NoExpiredText = "Ingen udlobne eller brugte kort",
            Giftcards = giftcards.Select(g =>
            {
                var expiresText = "Udlober aldrig";
                if (!string.IsNullOrEmpty(g.ExpiresAt) && DateTime.TryParse(g.ExpiresAt, out var expires))
                {
                    expiresText = $"Udlober: {expires.ToString("d. MMMM yyyy", culture)}";
                }
                var percentage = g.OriginalValue > 0 ? (int)((g.CurrentBalance ?? 0) / g.OriginalValue * 100) : 0;
                return new GiftcardItemViewModel
                {
                    Label = g.Label,
                    BalanceText = $"Saldo: <strong>{g.CurrentBalance:N0} kr</strong> (af {g.OriginalValue:N0} kr)".Replace(",", "."),
                    ExpiresText = expiresText,
                    Percentage = percentage
                };
            }).ToList(),
            Punchcards = punchcards.Select(p =>
            {
                var expiresText = "Udlober aldrig";
                if (!string.IsNullOrEmpty(p.ExpiresAt) && DateTime.TryParse(p.ExpiresAt, out var expires))
                {
                    expiresText = $"Udlober: {expires.ToString("d. MMMM yyyy", culture)}";
                }
                var percentage = p.TotalPunches > 0 ? (int)((p.UsedPunches ?? 0) * 100 / p.TotalPunches) : 0;
                return new GiftcardItemViewModel
                {
                    Label = p.Label,
                    BalanceText = $"Brugt: <strong>{p.UsedPunches} af {p.TotalPunches}</strong> klip",
                    ExpiresText = expiresText,
                    Percentage = percentage
                };
            }).ToList(),
            ExpiredCards = customer.Giftcards.Expired.Select(g => new GiftcardItemViewModel
            {
                Label = g.Label,
                BalanceText = "",
                ExpiresText = "",
                Percentage = 0
            }).ToList()
        };

        return View(model);
    }
}

public class CustomerDetailGiftcardsViewModel
{
    public required string GiftcardsTitle { get; init; }
    public required string PunchcardsTitle { get; init; }
    public required string ExpiredTitle { get; init; }
    public required string NoExpiredText { get; init; }
    public List<GiftcardItemViewModel> Giftcards { get; init; } = new();
    public List<GiftcardItemViewModel> Punchcards { get; init; } = new();
    public List<GiftcardItemViewModel> ExpiredCards { get; init; } = new();
}

public class GiftcardItemViewModel
{
    public required string Label { get; init; }
    public required string BalanceText { get; init; }
    public required string ExpiresText { get; init; }
    public int Percentage { get; init; }
}
