@page "/summary"
@page "/summary/{YearMonth}"
@attribute [Authorize]
@rendermode InteractiveServer

@using System.Globalization
@inject global::WorkTracker.Services.WorkDays.IWorkDayService WorkDayService
@inject IJSRuntime JS
@inject NavigationManager Navigation

<PageTitle>Monthly Summary</PageTitle>

<h1>Monthly Summary</h1>

<div class="d-flex align-items-center gap-2 mb-3">
    <button class="btn btn-outline-secondary btn-sm" @onclick="PreviousMonth">&laquo; Prev</button>
    <h2 class="h5 mb-0">@currentMonth.ToString("MMMM yyyy")</h2>
    <button class="btn btn-outline-secondary btn-sm" @onclick="NextMonth">Next &raquo;</button>
    <a class="btn btn-success btn-sm ms-auto" href="@GetExcelDownloadUrl()">Download Excel</a>
    <a class="btn btn-outline-primary btn-sm ms-auto" href="yearly-summary/@currentMonth.Year">Yearly Summary</a>
</div>

<div class="form-check mb-3">
    <input id="include-preview" type="checkbox" class="form-check-input" checked="@includePreview" @onchange="OnIncludePreviewChanged" />
    <label class="form-check-label" for="include-preview">Include preview work units in totals</label>
</div>

<div class="btn-group mb-3" role="group" aria-label="Summary view selector">
    <button type="button" class="btn @(viewMode == SummaryViewMode.Cards ? "btn-primary" : "btn-outline-primary")" @onclick="() => SetViewMode(SummaryViewMode.Cards)">Cards</button>
    <button type="button" class="btn @(viewMode == SummaryViewMode.Timesheet ? "btn-primary" : "btn-outline-primary")" @onclick="() => SetViewMode(SummaryViewMode.Timesheet)">Timesheet</button>
</div>

@if (loading)
{
    <p><em>Loading...</em></p>
}
else if (viewMode == SummaryViewMode.Cards && summary is not null)
{
    <div class="row g-3">
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100">
                <div class="card-body">
                    <div class="text-muted small">Working Days</div>
                    <div class="fs-3 fw-bold">@summary.TotalWorkingDays</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100">
                <div class="card-body">
                    <div class="text-muted small">Counted Work Units</div>
                    <div class="fs-3 fw-bold">@summary.CountedWorkUnits</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100">
                <div class="card-body">
                    <div class="text-muted small">Total Worked Hours</div>
                    <div class="fs-3 fw-bold">@FormatHours(summary.TotalWorkedHours)</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100">
                <div class="card-body">
                    <div class="text-muted small">Preview Hours</div>
                    <div class="fs-3 fw-bold">@FormatHours(summary.TotalPreviewWorkedHours)</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100">
                <div class="card-body">
                    <div class="text-muted small">Preview Units</div>
                    <div class="fs-3 fw-bold">@summary.PreviewWorkUnits</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100">
                <div class="card-body">
                    <div class="text-muted small">Hours Off</div>
                    <div class="fs-3 fw-bold">@FormatHours(summary.TotalHoursOff)</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100 border-success">
                <div class="card-body">
                    <div class="text-muted small">Gross Income</div>
                    <div class="fs-3 fw-bold text-success">€@summary.TotalGrossIncome.ToString("N2")</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100 border-primary">
                <div class="card-body">
                    <div class="text-muted small">Net Income</div>
                    <div class="fs-3 fw-bold text-primary">€@summary.TotalNetIncome.ToString("N2")</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100">
                <div class="card-body">
                    <div class="text-muted small">Office Days</div>
                    <div class="fs-3 fw-bold">@summary.OfficeDays</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100">
                <div class="card-body">
                    <div class="text-muted small">Home Days</div>
                    <div class="fs-3 fw-bold">@summary.HomeDays</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100">
                <div class="card-body">
                    <div class="text-muted small">Holidays</div>
                    <div class="fs-3 fw-bold">@summary.HolidayDays</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100">
                <div class="card-body">
                    <div class="text-muted small">Closure Days</div>
                    <div class="fs-3 fw-bold">@summary.ClosureDays</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100">
                <div class="card-body">
                    <div class="text-muted small">Days Off</div>
                    <div class="fs-3 fw-bold">@summary.DaysOff</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-4 col-xl-3">
            <div class="card text-center h-100">
                <div class="card-body">
                    <div class="text-muted small">Sick Days</div>
                    <div class="fs-3 fw-bold">@summary.SickDays</div>
                </div>
            </div>
        </div>
    </div>
}
else if (viewMode == SummaryViewMode.Timesheet && timesheet is not null)
{
    <div class="timesheet-summary-card card border-0 shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-bordered table-sm align-middle mb-0 timesheet-summary-table">
                    <thead>
                        <tr>
                            <th class="timesheet-summary-sticky-column">Categoria</th>
                            @for (var i = 0; i < timesheet.Days.Count; i++)
                            {
                                var day = timesheet.Days[i];
                                <th class="text-center timesheet-summary-day-header @GetDayColumnClass(day) @GetDayPopupClass(i, timesheet.Days.Count)">
                                    <div>@day.Date.Day</div>
                                    <div class="small text-muted">@GetDayHeader(day.Date)</div>
                                    <div class="timesheet-summary-day-popup">
                                        <div class="fw-semibold mb-2">@day.Date.ToString("dddd d MMMM", ItalianCulture)</div>
                                        @if (day.WorkUnitSummaries.Count == 0 && day.EventSummaries.Count == 0)
                                        {
                                            <div class="small text-muted">Nessun elemento registrato.</div>
                                        }
                                        else
                                        {
                                            @foreach (var workUnit in day.WorkUnitSummaries)
                                            {
                                                <div class="timesheet-summary-day-popup-item">@workUnit</div>
                                            }

                                            @foreach (var calendarEvent in day.EventSummaries)
                                            {
                                                <div class="timesheet-summary-day-popup-item timesheet-summary-day-popup-item-event">@calendarEvent</div>
                                            }
                                        }
                                    </div>
                                </th>
                            }
                            <th class="text-center timesheet-summary-total-column">Totale</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var row in timesheet.Rows)
                        {
                            <tr>
                                <th scope="row" class="timesheet-summary-sticky-column">@row.Label</th>
                                @for (var i = 0; i < row.DailyValues.Count; i++)
                                {
                                    <td class="text-center @GetDayColumnClass(timesheet.Days[i])">@FormatTimesheetValue(row.DailyValues[i], row.ValueFormat)</td>
                                }
                                <td class="text-center fw-semibold timesheet-summary-total-column">@FormatTimesheetValue(row.Total, row.ValueFormat)</td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>
}

@code {
    [Parameter] public string? YearMonth { get; set; }

    private static readonly CultureInfo ItalianCulture = CultureInfo.GetCultureInfo("it-IT");
    private const string IncludePreviewPreferenceKey = "worktracker.includePreviewWorkUnits";

    private DateOnly currentMonth;
    private bool loading = true;
    private bool includePreview;
    private global::WorkTracker.Domain.MonthlySummaryModel? summary;
    private global::WorkTracker.Domain.MonthlyTimesheetModel? timesheet;
    private SummaryViewMode viewMode = SummaryViewMode.Timesheet;

    protected override async Task OnParametersSetAsync()
    {
        currentMonth = ParseCurrentMonth();
        await LoadSummary();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (!firstRender)
        {
            return;
        }

        var savedIncludePreview = await JS.InvokeAsync<bool?>("workTrackerPreferences.getBool", IncludePreviewPreferenceKey);
        if (savedIncludePreview.HasValue && savedIncludePreview.Value != includePreview)
        {
            includePreview = savedIncludePreview.Value;
            await LoadSummary();
            await InvokeAsync(StateHasChanged);
        }
    }

    private async Task OnIncludePreviewChanged(ChangeEventArgs e)
    {
        includePreview = e.Value is bool value && value;
        await JS.InvokeVoidAsync("workTrackerPreferences.setBool", IncludePreviewPreferenceKey, includePreview);
        await LoadSummary();
    }

    private async Task LoadSummary()
    {
        loading = true;
        summary = await WorkDayService.GetMonthlySummaryAsync(currentMonth.Year, currentMonth.Month, includePreview);
        timesheet = await WorkDayService.GetMonthlyTimesheetAsync(currentMonth.Year, currentMonth.Month, includePreview);
        loading = false;
    }

    private Task PreviousMonth()
    {
        currentMonth = currentMonth.AddMonths(-1);
        Navigation.NavigateTo($"/summary/{currentMonth:yyyy-MM}");
        return Task.CompletedTask;
    }

    private Task NextMonth()
    {
        currentMonth = currentMonth.AddMonths(1);
        Navigation.NavigateTo($"/summary/{currentMonth:yyyy-MM}");
        return Task.CompletedTask;
    }

    private void SetViewMode(SummaryViewMode mode)
    {
        viewMode = mode;
    }

    private string GetExcelDownloadUrl()
    {
        return $"/api/monthly-timesheet/{currentMonth.Year}/{currentMonth.Month}/excel?includePreview={includePreview.ToString().ToLowerInvariant()}";
    }

    private static string GetDayHeader(DateOnly date)
    {
        return ItalianCulture.TextInfo.ToTitleCase(date.ToString("ddd", ItalianCulture));
    }

    private DateOnly ParseCurrentMonth()
    {
        if (!string.IsNullOrEmpty(YearMonth) && DateTime.TryParseExact(YearMonth, "yyyy-MM", null, DateTimeStyles.None, out var parsed))
        {
            return new DateOnly(parsed.Year, parsed.Month, 1);
        }

        return new DateOnly(DateTime.Today.Year, DateTime.Today.Month, 1);
    }

    private static string GetDayColumnClass(global::WorkTracker.Domain.MonthlyTimesheetDayModel day)
    {
        var classes = new List<string>();

        if (day.Date == DateOnly.FromDateTime(DateTime.Today))
        {
            classes.Add("timesheet-summary-day-today");
        }

        if (day.IsWeekend || day.IsHoliday)
        {
            classes.Add("timesheet-summary-day-danger");
        }

        if (day.IsClosure)
        {
            classes.Add("timesheet-summary-day-closure");
        }

        return string.Join(" ", classes);
    }

    private static string GetDayPopupClass(int index, int totalDays)
    {
        if (index == 0)
        {
            return "timesheet-summary-day-popup-left";
        }

        return index >= totalDays - 2 ? "timesheet-summary-day-popup-right" : string.Empty;
    }

    private static string FormatTimesheetValue(decimal? value, global::WorkTracker.Domain.MonthlyTimesheetValueFormat valueFormat)
    {
        if (!value.HasValue || value.Value <= 0m)
        {
            return string.Empty;
        }

        return valueFormat == global::WorkTracker.Domain.MonthlyTimesheetValueFormat.Days
            ? value.Value.ToString("0.##", ItalianCulture)
            : FormatDecimalHours(value.Value);
    }

    private static string FormatDecimalHours(decimal value)
    {
        return DurationFormatter.FormatHours(value, blankWhenZero: true);
    }

    private static string FormatHours(decimal value)
    {
        return DurationFormatter.FormatHours(value);
    }

    private enum SummaryViewMode
    {
        Cards,
        Timesheet
    }
}
