using System.Text.Json.Serialization;
using BigRegister.Api.Contracts;
namespace BigRegister.Api.Zgw;
///
/// The subset of a ZGW Zaak (Zaken API / ZRC) the read slice needs. The full resource has
/// dozens of fields; we bind only what maps to . Note the
/// two ZGW traits that force an anti-corruption layer: is the resource's
/// identity (not a bare id), and is a URL into another service
/// (Catalogi/ZTC) that must be resolved to a human label.
///
public sealed record ZgwZaak(
[property: JsonPropertyName("url")] string Url,
[property: JsonPropertyName("identificatie")] string Identificatie,
[property: JsonPropertyName("zaaktype")] string Zaaktype,
[property: JsonPropertyName("startdatum")] DateOnly Startdatum,
[property: JsonPropertyName("einddatum")] DateOnly? Einddatum,
[property: JsonPropertyName("registratiedatum")] DateOnly? Registratiedatum);
///
/// Anti-corruption map: ZGW Zaak → the existing the FE
/// already renders (WP-49). This is where "URL as identity" and the cross-service zaaktype
/// join get flattened away, so nothing downstream (the FE) sees ZGW shapes.
///
public static class ZgwZaakMapper
{
/// Last path segment of a ZGW resource URL — the uuid that identifies it.
public static string Uuid(string url) => url.TrimEnd('/').Split('/').Last();
public static ApplicationSummaryDto ToSummaryDto(ZgwZaak z, string zaaktypeLabel)
{
// ponytail: coarse status map — an open zaak (no einddatum) is In behandeling, a closed
// one is Goedgekeurd. Real fidelity (statustype/resultaat lookups) is a later slice; the
// Afgewezen path needs the resultaat resource. Enough to prove the seam end-to-end.
var status = z.Einddatum is null
? new AanvraagStatusDto("InBehandeling", Referentie: z.Identificatie, Manual: true)
: new AanvraagStatusDto("Goedgekeurd", Referentie: z.Identificatie);
var created = Iso(z.Registratiedatum ?? z.Startdatum);
var updated = Iso(z.Einddatum ?? z.Registratiedatum ?? z.Startdatum);
return new ApplicationSummaryDto(
Id: Uuid(z.Url),
Type: zaaktypeLabel,
Status: status,
DocumentIds: Array.Empty(), // zaak↔document links arrive with WP-51 (DRC)
CreatedAt: created,
UpdatedAt: updated,
SubmittedAt: created,
Owner: z.Identificatie); // real initiator (rol/BSN) needs a rollen lookup — later slice
}
// Keep the wire shape identical to the local source (round-trip datetime): a ZGW date
// becomes midnight UTC so the FE's date parsing sees the same format either backend.
private static string Iso(DateOnly d) =>
d.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc).ToString("o");
}