using System.Text.Json;
using System.Text.Json.Serialization;

namespace SolutionErp.Api.Serialization;

// [S85 anh Kiet — TZ FIX] Timestamps luu UTC (DateTime.UtcNow) nhung EF doc tu SQL Server
// datetime2 = DateTimeKind.Unspecified -> System.Text.Json mac dinh serialize KHONG co 'Z'
// (vd "2026-06-23T08:20:00") -> FE new Date(naive) hieu la LOCAL -> lech +7h (VN UTC+7,
// "moi tao ma bao 7h truoc"). Converter nay ep serialize UTC ISO-8601 CO 'Z' -> FE new
// Date("...Z") convert dung ve local. System.Text.Json tu dung converter nay cho non-null
// value cua DateTime? (Nullable) nen khong can dang ky rieng cho DateTime?.
public sealed class UtcDateTimeJsonConverter : JsonConverter<DateTime>
{
    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        => reader.GetDateTime();

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        var utc = value.Kind switch
        {
            DateTimeKind.Utc => value,
            DateTimeKind.Local => value.ToUniversalTime(),
            _ => DateTime.SpecifyKind(value, DateTimeKind.Utc), // Unspecified (tu EF) = coi la UTC
        };
        writer.WriteStringValue(utc.ToString("O")); // ISO-8601 round-trip, co 'Z'
    }
}
