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

namespace PlanTempus.Application.Features.Customers.Components;

public class CustomerDetailAppointmentsViewComponent : ViewComponent
{
    private readonly ILocalizationService _localization;

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

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

        var model = new CustomerDetailAppointmentsViewModel
        {
            UpcomingTitle = "Kommende aftaler",
            HistoryTitle = "Tidligere aftaler",
            MoveButtonText = "Flyt",
            CancelButtonText = "Aflys",
            SeeAllText = "Se alle aftaler ->",
            DateHeader = "Dato",
            ServiceHeader = "Service",
            HairdresserHeader = "Frisor",
            DurationHeader = "Varighed",
            PriceHeader = "Pris",
            Upcoming = customer.Appointments.Upcoming.Select(a =>
            {
                var dateText = a.Date;
                if (DateTime.TryParse(a.Date, out var date))
                {
                    dateText = date.ToString("dddd d. MMMM yyyy", culture);
                    dateText = char.ToUpper(dateText[0]) + dateText[1..];
                }
                return new UpcomingAppointmentViewModel
                {
                    FormattedDate = $"{dateText} kl. {a.Time}",
                    Details = $"{a.Service} - {a.Hairdresser} - {a.Duration}"
                };
            }).ToList(),
            History = customer.Appointments.History.Select(a =>
            {
                var dateText = a.Date;
                if (DateTime.TryParse(a.Date, out var date))
                {
                    dateText = date.ToString("d. MMM yyyy", culture);
                }
                return new HistoryAppointmentViewModel
                {
                    FormattedDate = dateText,
                    Service = a.Service,
                    Hairdresser = a.Hairdresser,
                    Duration = a.Duration,
                    FormattedPrice = $"{a.Price:N0} kr".Replace(",", ".")
                };
            }).ToList()
        };

        return View(model);
    }
}

public class CustomerDetailAppointmentsViewModel
{
    public required string UpcomingTitle { get; init; }
    public required string HistoryTitle { get; init; }
    public required string MoveButtonText { get; init; }
    public required string CancelButtonText { get; init; }
    public required string SeeAllText { get; init; }
    public required string DateHeader { get; init; }
    public required string ServiceHeader { get; init; }
    public required string HairdresserHeader { get; init; }
    public required string DurationHeader { get; init; }
    public required string PriceHeader { get; init; }
    public List<UpcomingAppointmentViewModel> Upcoming { get; init; } = new();
    public List<HistoryAppointmentViewModel> History { get; init; } = new();
}

public class UpcomingAppointmentViewModel
{
    public required string FormattedDate { get; init; }
    public required string Details { get; init; }
}

public class HistoryAppointmentViewModel
{
    public required string FormattedDate { get; init; }
    public required string Service { get; init; }
    public required string Hairdresser { get; init; }
    public required string Duration { get; init; }
    public required string FormattedPrice { get; init; }
}
