import { Injectable, inject, resource } from '@angular/core';
import { Aantekening, AantekeningType } from '../domain/registration';
import { ApiClient, AantekeningDto } from '@shared/infrastructure/api-client';

/**
 * Infrastructure adapter for the BIG-register source. Exposes signal-based
 * resources (Angular's `resource` over the generated typed client); each returns
 * a Resource with status()/value()/error()/reload(). Call from an injection
 * context (a field initializer in the store).
 *
 * Note: registration + person are now served via the aggregated dashboard-view
 * endpoint (see DashboardViewAdapter). Only the notes stream remains separate.
 */
@Injectable({ providedIn: 'root' })
export class BigRegisterAdapter {
  private client = inject(ApiClient);

  aantekeningenResource() {
    return resource({ loader: () => this.client.notes().then((ns) => ns.map(toAantekening)) });
  }
}

/** Map the wire DTO (all fields optional) onto our domain type. */
function toAantekening(n: AantekeningDto): Aantekening {
  return { type: n.type as AantekeningType, omschrijving: n.omschrijving ?? '', datum: n.datum ?? '' };
}
