<?php
/**
 * Frontend: Checkout Country/Region field visibility via WC filters.
 *
 * @package FoodOrd
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Foodord_Checkout_Country_Toggle
 *
 * Controls visibility of the Country/Region and State/Province fields on Checkout based on admin setting.
 */
class Foodord_Checkout_Country_Toggle {
	/**
	 * Bootstrap hooks (frontend only).
	 */
	public function init(): void {
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 20 );
		add_filter( 'woocommerce_is_checkout', array( $this, 'force_blocks_checkout_conditional' ), 10, 1 );
		add_filter( 'woocommerce_checkout_fields', array( $this, 'filter_checkout_fields' ), 10, 1 );
		add_filter( 'woocommerce_default_address_fields', array( $this, 'filter_default_address_fields' ), 10, 1 );
		// WooCommerce Blocks checkout fields (React-based checkout).
		add_filter( 'woocommerce_blocks_checkout_fields', array( $this, 'filter_blocks_checkout_fields' ), 10, 1 );
	}

	/**
	 * Enqueue hide helper for Blocks checkout when fields are disabled.
	 */
	public function enqueue_scripts(): void {
		if ( is_admin() ) {
			return;
		}
		if ( ! $this->is_checkout_context() ) {
			return;
		}
		$show_country = 'yes' === get_option( 'foodord_checkout_country_toggle_show', 'no' );
		if ( $show_country ) {
			return; // Nothing to do when visible.
		}
		wp_enqueue_script(
			'foodord-checkout-country-blocks',
			defined( 'FOODORD_URL' ) ? FOODORD_URL . 'public/js/foodord-checkout-country-blocks.js' : plugin_dir_url( __FILE__ ) . 'js/foodord-checkout-country-blocks.js',
			array(),
			FOODORD_VERSION,
			true
		);
	}

	/**
	 * Treat standalone pages containing the WooCommerce checkout block as checkout pages.
	 *
	 * @param bool $is_checkout Whether WooCommerce already considers the request checkout.
	 * @return bool
	 */
	public function force_blocks_checkout_conditional( bool $is_checkout ): bool {
		if ( $is_checkout || is_admin() ) {
			return $is_checkout;
		}

		return $this->page_has_checkout_block();
	}

	/**
	 * Filter WooCommerce checkout fields to show/hide Country/Region and State/Province.
	 *
	 * @param array $fields Checkout fields.
	 * @return array
	 */
	public function filter_checkout_fields( array $fields ): array {
		if ( is_admin() ) {
			return $fields;
		}
		// Default: do not show country unless explicitly enabled.
		$show_country = 'yes' === get_option( 'foodord_checkout_country_toggle_show', 'no' );
		if ( ! $show_country ) {
			if ( isset( $fields['billing']['billing_country'] ) ) {
				unset( $fields['billing']['billing_country'] );
			}
			if ( isset( $fields['billing']['billing_state'] ) ) {
				unset( $fields['billing']['billing_state'] );
			}
			if ( isset( $fields['shipping']['shipping_country'] ) ) {
				unset( $fields['shipping']['shipping_country'] );
			}
			if ( isset( $fields['shipping']['shipping_state'] ) ) {
				unset( $fields['shipping']['shipping_state'] );
			}
		}
		return $fields;
	}

	/**
	 * Filter WooCommerce default address fields to hide State/Province when country is hidden.
	 *
	 * @param array $fields Default address fields.
	 * @return array
	 */
	public function filter_default_address_fields( array $fields ): array {
		if ( is_admin() ) {
			return $fields;
		}

		$show_country = 'yes' === get_option( 'foodord_checkout_country_toggle_show', 'no' );
		if ( ! $show_country && isset( $fields['state'] ) ) {
			unset( $fields['state'] );
		}

		return $fields;
	}

	/**
	 * Filter WooCommerce Blocks checkout fields to hide Country and State when disabled.
	 *
	 * @param array $fields Blocks checkout fields schema.
	 * @return array
	 */
	public function filter_blocks_checkout_fields( array $fields ): array {
		// Default: hide unless explicitly enabled via option.
		$show_country = 'yes' === get_option( 'foodord_checkout_country_toggle_show', 'no' );
		if ( $show_country ) {
			return $fields;
		}

		// Billing/Shipping country/state keys in Blocks are 'country' and 'state'.
		if ( isset( $fields['billing']['country'] ) ) {
			unset( $fields['billing']['country'] );
		}
		if ( isset( $fields['billing']['state'] ) ) {
			unset( $fields['billing']['state'] );
		}
		if ( isset( $fields['billing']['billing_state'] ) ) {
			unset( $fields['billing']['billing_state'] );
		}
		if ( isset( $fields['shipping']['country'] ) ) {
			unset( $fields['shipping']['country'] );
		}
		if ( isset( $fields['shipping']['state'] ) ) {
			unset( $fields['shipping']['state'] );
		}
		if ( isset( $fields['shipping']['shipping_state'] ) ) {
			unset( $fields['shipping']['shipping_state'] );
		}

		return $fields;
	}

	/**
	 * Determine whether current page is a checkout context.
	 *
	 * @return bool
	 */
	private function is_checkout_context(): bool {
		if ( function_exists( 'is_checkout' ) && is_checkout() ) {
			return true;
		}

		return $this->page_has_checkout_block();
	}

	/**
	 * Detect standalone WooCommerce checkout block pages.
	 *
	 * @return bool
	 */
	private function page_has_checkout_block(): bool {
		$post = get_post();
		if ( ! $post instanceof WP_Post ) {
			return false;
		}

		if ( function_exists( 'has_block' ) && has_block( 'woocommerce/checkout', $post ) ) {
			return true;
		}

		return false !== strpos( (string) $post->post_content, 'wp:woocommerce/checkout' );
	}
}
