using System;
using System.Globalization;
using Theriapolis.Core.Persistence;

namespace Theriapolis.GodotHost.Platform;

/// <summary>
/// Slot-picker label formatting. Pulls the in-game time from
/// <see cref="SaveHeader.SlotLabel"/> (e.g. "Howlwind — Y0 Spring D5
/// (Tier 1)") and appends the wall-clock saved-at time parsed from
/// <see cref="SaveHeader.SavedAtUtc"/>, rendered in the player's local
/// timezone with a relative label when recent.
///
/// Shared between <see cref="Scenes.SaveLoadScreen"/> (load picker
/// from Title) and <see cref="Scenes.PauseMenuScreen"/>'s save picker
/// so both surfaces present the same row format.
/// </summary>
public static class SaveSlotFormat
{
    /// <summary>Composed row label: "{slot} — {in-game} · saved {when}".</summary>
    public static string FormatRow(string slotPrefix, SaveHeader header)
        => $"{slotPrefix} — {header.SlotLabel()}  ·  saved {FormatSavedAt(header.SavedAtUtc)}";

    /// <summary>Parses the SaveHeader's UTC saved-at timestamp and
    /// renders it relative to now, in local time. Returns "&lt;unknown&gt;"
    /// for empty / unparseable inputs so the row still shows something.</summary>
    public static string FormatSavedAt(string savedAtUtc)
    {
        if (string.IsNullOrWhiteSpace(savedAtUtc)) return "<unknown>";
        if (!DateTime.TryParse(
                savedAtUtc, CultureInfo.InvariantCulture,
                DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
                out var utc))
            return savedAtUtc;

        DateTime local = utc.ToLocalTime();
        DateTime now = DateTime.Now;
        if (local.Date == now.Date)
            return $"today, {local:HH:mm}";
        if (local.Date == now.Date.AddDays(-1))
            return $"yesterday, {local:HH:mm}";
        if (local.Year == now.Year)
            return local.ToString("MMM d, HH:mm", CultureInfo.InvariantCulture);
        return local.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
    }
}
