using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
using Microsoft.AspNetCore.Mvc;

namespace Pgo.Backend.Helpers;

public static class ResponseExtensions
{
    public static IActionResult WithFhirResponseBundleSuccess(this ControllerBase controller, Bundle bundle)
    {
        return controller.Content(new FhirJsonSerializer().SerializeToString(bundle), "application/fhir+json");
    }

    public static IActionResult WithFhirResponseOperationOutcomeNotFound(this ControllerBase controller, string? diagnostics = null)
    {
        var error = new OperationOutcome();
        error.Issue.Add(new OperationOutcome.IssueComponent
        {
            Severity = OperationOutcome.IssueSeverity.Error,
            Code = OperationOutcome.IssueType.NotFound,
            Diagnostics = diagnostics
        });

        controller.Response.StatusCode = 404;
        return controller.Content(new FhirJsonSerializer().SerializeToString(error), "application/fhir+json");
    }
}
