import { Injectable, inject, signal } from '@angular/core';
import { take } from 'rxjs/operators';
import { ReconciliationRecord, Settlement } from '../../../../core/finance/models/reconciliation.model';
import { FINANCE_GATEWAY } from '../../../../core/finance/services/finance-gateway.token';

@Injectable({ providedIn: 'root' })
export class AdminFinanceFacade {
  private readonly gateway = inject(FINANCE_GATEWAY);

  readonly reconciliation = signal<ReconciliationRecord[]>([]);
  readonly settlements = signal<Settlement[]>([]);
  readonly loading = signal(false);

  load(): void {
    this.loading.set(true);
    this.gateway.loadReconciliationQueue().pipe(take(1)).subscribe(items => {
      this.reconciliation.set(items);
      this.loading.set(false);
    });
    this.gateway.loadSettlements().pipe(take(1)).subscribe(items => this.settlements.set(items));
  }

  resolve(id: string, note: string): void {
    this.gateway.resolveReconciliation(id, note).pipe(take(1)).subscribe(() => this.load());
  }
}
