using System.Net;
using System.Net.Http.Json;
using System.Text.Json;

namespace GerbilManager.Tests;

/// <summary>
/// WEB-PUSH: VAPID-Public-Key abrufen + Abo speichern/entfernen.
/// (Das tatsächliche Versenden wird nicht getestet — es geht an externe Push-Dienste.)
/// </summary>
public class PushEndpointTests : IClassFixture<ApiFactory>
{
    private readonly ApiFactory _factory;
    public PushEndpointTests(ApiFactory factory) => _factory = factory;

    [Fact]
    public async Task Vapid_public_key_is_exposed()
    {
        var client = _factory.CreateClient();
        var doc = JsonDocument.Parse(await client.GetStringAsync("/push/vapid-public-key")).RootElement;
        Assert.True(doc.GetProperty("enabled").GetBoolean());
        Assert.False(string.IsNullOrWhiteSpace(doc.GetProperty("publicKey").GetString()));
    }

    [Fact]
    public async Task Subscribe_is_idempotent_and_unsubscribe_works()
    {
        var client = _factory.CreateClient();
        var endpoint = $"https://push.example.com/{Guid.NewGuid()}";
        var body = new { endpoint, p256dh = "abc123", auth = "def456" };

        var first = await client.PostAsJsonAsync("/push/subscribe", body);
        Assert.Equal(HttpStatusCode.OK, first.StatusCode);
        // Erneutes Abo mit derselben Endpoint-URL -> weiterhin OK (Upsert, kein Duplikat).
        var second = await client.PostAsJsonAsync("/push/subscribe", body);
        Assert.Equal(HttpStatusCode.OK, second.StatusCode);

        var bad = await client.PostAsJsonAsync("/push/subscribe", new { endpoint = "", p256dh = "", auth = "" });
        Assert.Equal(HttpStatusCode.BadRequest, bad.StatusCode);

        var unsub = await client.PostAsJsonAsync("/push/unsubscribe", new { endpoint });
        Assert.Equal(HttpStatusCode.NoContent, unsub.StatusCode);
    }
}
