namespace SIGCM2.Application.Common;

/// <summary>
/// Extension methods for <see cref="TimeProvider"/> that expose Argentina-localized
/// date helpers. Handles the UTC-3 offset cross-platform (IANA / Windows TZ IDs).
///
/// UDT-011: Cat2 fields (VigenciaDesde, etc.) must use civil Argentine date, never
/// raw UTC, to avoid date-creep during the 22:00–23:59 window.
/// </summary>
public static class TimeProviderArgentinaExtensions
{
    // IANA TZ id — Linux / macOS / .NET 8+ on Windows with ICU
    public const string ArgentinaTimeZoneId = "America/Argentina/Buenos_Aires";

    // Windows built-in TZ id — fallback for environments without ICU
    public const string ArgentinaTimeZoneIdWindows = "Argentina Standard Time";

    private static readonly TimeZoneInfo ArgentinaTz = LoadArgentinaTz();

    /// <summary>
    /// Returns today's civil date in Argentina timezone, computed from the
    /// <see cref="TimeProvider"/> UTC clock.
    /// Safe in tests via <c>FakeTimeProvider</c>; safe in production via
    /// <c>TimeProvider.System</c>.
    /// </summary>
    public static DateOnly GetArgentinaToday(this TimeProvider timeProvider)
    {
        var utcNow = timeProvider.GetUtcNow();
        var argentinaNow = TimeZoneInfo.ConvertTime(utcNow, ArgentinaTz);
        return DateOnly.FromDateTime(argentinaNow.DateTime);
    }

    private static TimeZoneInfo LoadArgentinaTz()
    {
        try
        {
            return TimeZoneInfo.FindSystemTimeZoneById(ArgentinaTimeZoneId);
        }
        catch (TimeZoneNotFoundException)
        {
            // Windows without ICU: fall back to built-in Windows TZ name
            return TimeZoneInfo.FindSystemTimeZoneById(ArgentinaTimeZoneIdWindows);
        }
    }
}
