import { Presenter } from "@erp/core/api";
import * as handlebars from "handlebars";
import { readFileSync } from "node:fs";
import path from "node:path";
import { CustomerInvoice } from "../../../../domain";

export class CustomerInvoiceReportHTMLPresenter extends Presenter {
  toOutput(customerInvoice: CustomerInvoice): string {
    const dtoPresenter = this.presenterRegistry.getPresenter({
      resource: "customer-invoice",
      projection: "FULL",
    });

    const prePresenter = this.presenterRegistry.getPresenter({
      resource: "customer-invoice",
      projection: "REPORT",
      format: "JSON",
    });

    const invoiceDTO = dtoPresenter.toOutput(customerInvoice);
    const prettyDTO = prePresenter.toOutput(invoiceDTO);

    // Obtener y compilar la plantilla HTML
    const templateHtml = readFileSync(
      path.join(__dirname, "./templates/customer-invoice/template.hbs")
    ).toString();
    const template = handlebars.compile(templateHtml, {});
    return template(prettyDTO);
  }
}
