using System.Globalization;
using Microsoft.Data.Sqlite;

namespace ZB.MOM.WW.Auth.ApiKeys.Sqlite;

/// <summary>
/// Shared helpers for reading round-trippable timestamps out of the SQLite stores.
/// All timestamps are persisted with the round-trip ("O") format, so parsing is centralized
/// here to keep the three stores DRY and consistent.
/// </summary>
internal static class SqliteValueParsing
{
    /// <summary>Parses a round-trip ("O") formatted timestamp written by the stores.</summary>
    internal static DateTimeOffset ParseUtc(string value) =>
        DateTimeOffset.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);

    /// <summary>Reads a nullable round-trip timestamp at <paramref name="ordinal"/>.</summary>
    internal static DateTimeOffset? ReadNullableUtc(SqliteDataReader reader, int ordinal) =>
        reader.IsDBNull(ordinal) ? null : ParseUtc(reader.GetString(ordinal));
}
