import { inject, Injectable } from '@angular/core';
import { OMSPrintService, ResponseArgs } from '@generated/swagger/print-api';
import {
  PrintOrderConfirmation,
  PrintOrderConfirmationSchema,
} from '../schemas';
import { ResponseArgsError } from '@isa/common/data-access';
import { firstValueFrom } from 'rxjs';
import { logger } from '@isa/core/logging';

@Injectable({ providedIn: 'root' })
export class CheckoutPrintService {
  #logger = logger(() => ({ service: 'CheckoutPrintService' }));
  #omsPrintService = inject(OMSPrintService);

  async printOrderConfirmation(
    params: PrintOrderConfirmation,
  ): Promise<ResponseArgs> {
    const parsed = PrintOrderConfirmationSchema.parse(params);

    const req$ = this.#omsPrintService.OMSPrintAbholscheinById(parsed);

    const res = await firstValueFrom(req$);

    if (res.error) {
      const err = new ResponseArgsError(res);
      this.#logger.error('Failed to print order confirmation', err);
      throw err;
    }

    return res;
  }
}
