using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using GerbilManagerWebAPI.Import;
using GerbilManagerWebAPI.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace GerbilManager.Tests;
///
/// ERWERB: acquisition data per animal (purchase date/price/note).
/// - POST creates, GET ?gerbilId= filters, PUT updates, DELETE removes.
/// - CRITICAL: acquisition rows survive the import re-ingest wipe (loose, FK-free
/// GerbilId/SourceContactId), exactly like Feedback.
///
public class AcquisitionEndpointTests : IClassFixture
{
private readonly ApiFactory _factory;
public AcquisitionEndpointTests(ApiFactory factory) => _factory = factory;
[Fact]
public async Task Crud_roundtrip_create_filter_update_delete()
{
var client = _factory.CreateClient();
var gerbilId = Guid.NewGuid();
var contactId = Guid.NewGuid();
// CREATE
var resp = await client.PostAsJsonAsync("/acquisitions", new
{
gerbilId,
sourceContactId = contactId,
date = "2025-03-14",
price = 25.50m,
note = "Auf der Börse gekauft.",
});
Assert.Equal(HttpStatusCode.Created, resp.StatusCode);
var created = JsonDocument.Parse(await resp.Content.ReadAsStringAsync()).RootElement;
var id = created.GetProperty("id").GetString()!;
Assert.Equal(gerbilId.ToString(), created.GetProperty("gerbilId").GetString());
Assert.Equal(25.50m, created.GetProperty("price").GetDecimal());
Assert.Equal("2025-03-14", created.GetProperty("date").GetString());
// GET ?gerbilId= returns it
var listed = JsonDocument.Parse(await client.GetStringAsync($"/acquisitions?gerbilId={gerbilId}")).RootElement;
Assert.Contains(listed.EnumerateArray(),
a => a.GetProperty("note").GetString() == "Auf der Börse gekauft.");
// a different gerbilId yields nothing
var other = JsonDocument.Parse(await client.GetStringAsync($"/acquisitions?gerbilId={Guid.NewGuid()}")).RootElement;
Assert.Empty(other.EnumerateArray());
// UPDATE
var put = await client.PutAsJsonAsync($"/acquisitions/{id}", new
{
gerbilId,
sourceContactId = (Guid?)null,
date = "2025-04-01",
price = 30m,
note = "Korrigiert.",
});
Assert.Equal(HttpStatusCode.NoContent, put.StatusCode);
var afterPut = JsonDocument.Parse(await client.GetStringAsync($"/acquisitions/{id}")).RootElement;
Assert.Equal(30m, afterPut.GetProperty("price").GetDecimal());
Assert.Equal("Korrigiert.", afterPut.GetProperty("note").GetString());
Assert.True(afterPut.GetProperty("sourceContactId").ValueKind == JsonValueKind.Null);
// DELETE
var del = await client.DeleteAsync($"/acquisitions/{id}");
Assert.Equal(HttpStatusCode.NoContent, del.StatusCode);
var gone = await client.GetAsync($"/acquisitions/{id}");
Assert.Equal(HttpStatusCode.NotFound, gone.StatusCode);
}
[Fact]
public async Task Acquisition_survives_ingest_wipe()
{
var dir = Path.Combine(Path.GetTempPath(), "acq-ingest-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(dir);
try
{
var contactId = Guid.NewGuid();
var fatherId = Guid.NewGuid();
var motherId = Guid.NewGuid();
var litterId = Guid.NewGuid();
var data = new
{
Contacts = new[]
{
new { Id = contactId, Name = "Test Breeder", Email = "t@e.de", Phone = "", Address = "", Notes = (string?)null, IsBreeder = true, IsReceiver = false, NameSuffix = (string?)null, Provenance = (string?)null }
},
Litters = new[]
{
new { Id = litterId, Name = "Wurf A", Date = "2026-01-01", TotalBorn = 5, DeathsWithin8Weeks = 0, FatherId = fatherId, MotherId = motherId, ExpectedGoHomeDate = (string?)null, Notes = "", PairingCode = "PC01", ExternalRef = "ext-litter-1", LitterLetter = "A" }
},
Gerbils = new[]
{
Animal(fatherId, "Papa", "male", contactId),
Animal(motherId, "Mama", "female", contactId),
},
GerbilPhotos = Array.Empty