<?php

namespace Bright_Deposits_For_WooCommerce\Setup;

/**
 * @package Bright_Deposits_For_WooCommerce\Setup
 * @since 1.0.0
 * @author Bright Plugins
 * Description: This class is responsible for setting up the template for the outstanding balance payment form.
 */
class Templates {
	
	public function __construct() {
		add_filter( 'woocommerce_locate_template', [$this, 'plugin_template'], 20, 3 );
	}

	/**
	 * Locate the template for the outstanding balance payment form.
	 * @since 1.0.0
	 * @param string $template
	 * @param string $template_name
	 * @param string $template_path
	 * @return string
	 */
	public function plugin_template( $template, $template_name, $template_path ) {
		global $wp;

		if ( $template_name === 'checkout/form-pay.php' ) {

			$order_id   = sanitize_text_field( $wp->query_vars['order-pay'] ); // The order ID
			$order      = wc_get_order( $order_id );

			if ( !$order instanceof \WC_Order ) {
				return $template;
			}

			if ( ! empty( $order->get_meta( '_deposit_id', true ) ) ) {
				$template = BDFWBP_PATH . '/templates/checkout/form-pay.php';
			}

		}

		/**
		 * BDFWBP_CUSTOM_FILTER_HOOK: Allows developers to locate the template for the outstanding balance payment form.
		 * @since 1.0.0
		 * @param string $template
		 * @param string $template_name
		 * @param string $template_path
		 * @return string
		 */
		return apply_filters( 'bdfwbp_locate_template_outstanding_balance_payment_form', $template, $template_name, $template_path );
	}
}