using System.Net.Http.Json;
using System.Text.Json;
using EnvelopeGenerator.ReceiverUI.Models;
using EnvelopeGenerator.ReceiverUI.Options;
using Microsoft.Extensions.Options;
namespace EnvelopeGenerator.ReceiverUI.Services;
///
/// Retrieves annotation positions from the API.
/// The URL is composed as {BaseUrl}/api/Annotation/{envelopeKey}.
/// During development, BaseUrl is empty so the request resolves to the
/// YARP-proxied route on the same origin, which currently serves
/// fake-data/annotations.json. To switch to real data, update the
/// YARP route in yarp.json — no code change required.
///
public class AnnotationService(HttpClient http, IOptions apiOptions)
{
private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web);
public async Task> GetAnnotationsAsync(string envelopeKey, CancellationToken cancel = default)
{
var url = $"{apiOptions.Value.BaseUrl}/api/Annotation/{Uri.EscapeDataString(envelopeKey)}";
var response = await http.GetAsync(url, cancel);
if (!response.IsSuccessStatusCode)
return [];
var result = await response.Content.ReadFromJsonAsync>(_jsonOptions, cancel);
return result ?? [];
}
}