using System.Text.Json;
using FluentAssertions;
using SIGCM2.Api.Json;

namespace SIGCM2.Api.Tests.Json;

/// <summary>
/// UDT-011 T300.30 — Unit tests for DateOnlyJsonConverter.
/// Verifies round-trip serialization as "yyyy-MM-dd" ISO string.
/// No DB, no HTTP.
/// </summary>
public sealed class DateOnlyJsonConverterTests
{
    private readonly JsonSerializerOptions _options = new()
    {
        Converters = { new DateOnlyJsonConverter() }
    };

    /// <summary>[REQ-BE-JSON-001] DateOnly serializes as "yyyy-MM-dd" string.</summary>
    [Fact]
    public void Serialize_DateOnly_WritesYyyyMmDdString()
    {
        var date = new DateOnly(2026, 5, 1);
        var json = JsonSerializer.Serialize(date, _options);
        json.Should().Be("\"2026-05-01\"");
    }

    /// <summary>[REQ-BE-JSON-002] "yyyy-MM-dd" string deserializes back to DateOnly.</summary>
    [Fact]
    public void Deserialize_YyyyMmDdString_ReturnsDateOnly()
    {
        var json = "\"2026-05-01\"";
        var date = JsonSerializer.Deserialize<DateOnly>(json, _options);
        date.Should().Be(new DateOnly(2026, 5, 1));
    }

    /// <summary>[REQ-BE-JSON-003] Invalid date format (dd/MM/yyyy) throws on deserialize.</summary>
    [Fact]
    public void Deserialize_InvalidFormat_ThrowsFormatException()
    {
        var json = "\"01/05/2026\"";  // formato dd/MM/yyyy — inválido
        var act = () => JsonSerializer.Deserialize<DateOnly>(json, _options);
        act.Should().Throw<FormatException>();
    }

    /// <summary>JSON null is not valid for non-nullable DateOnly — must throw.</summary>
    [Fact]
    public void Deserialize_Null_ThrowsException()
    {
        var json = "null";
        var act = () => JsonSerializer.Deserialize<DateOnly>(json, _options);
        act.Should().Throw<Exception>();  // JsonException desde GetString() null path
    }
}
