<?php
namespace Inoby;

use Inoby_Config;

class WPBS_Order_Details {
  private string $informations_template = "template-parts/wpbs-booking-informations";
  private string $prices_template = "template-parts/wpbs-booking-prices";

  public function __construct() {
    add_action("woocommerce_order_details_before_order_table", [$this, "output_order_details"]);
  }

  public function output_order_details(\Automattic\WooCommerce\Admin\Overrides\Order $order) {
    $wpbs_payments = wpbs_get_payments(["order_id" => $order->get_id()]);
    $payment = reset($wpbs_payments);
    if (!$payment) {
      return;
    }

    $booking = wpbs_get_booking_from_order($order);
    if (!$booking) {
      return;
    }

    $both_enabled = Inoby_Config::wpbs_order_details() === true;

    if ($both_enabled || Inoby_Config::wpbs_order_details()["informations"]) {
      require_once __DIR__ . "/class-inoby-wpbs-informations-outputter.php";
      $outputter = new WPBS_Informations_Outputter($booking);

      apply_filters("inoby_wpbs_informations_template", $this->informations_template);
      get_template_part($this->informations_template, null, compact("booking", "outputter"));
    }

    if ($both_enabled || Inoby_Config::wpbs_order_details()["prices"]) {
      require_once __DIR__ . "/class-inoby-wpbs-prices-outputter.php";
      $outputter = new WPBS_Prices_Outputter($payment);

      apply_filters("inoby_wpbs_prices_template", $this->prices_template);
      get_template_part($this->prices_template, null, compact("payment", "outputter"));
    }
  }
}
