<?php
/** Read-only, secret-free WooCommerce store settings snapshot. */

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

/** @return true|WP_Error */
function wpagent_wc_require_store_settings_runtime() {
	$woocommerce = wpagent_require_woocommerce();
	if ( is_wp_error( $woocommerce ) ) {
		return $woocommerce;
	}
	foreach ( array( 'get_woocommerce_currency', 'get_woocommerce_currency_symbol', 'wc_get_price_thousand_separator', 'wc_get_price_decimal_separator', 'wc_get_price_decimals', 'wc_tax_enabled', 'wc_prices_include_tax', 'wc_get_page_id' ) as $function ) {
		if ( ! function_exists( $function ) ) {
			return new WP_Error( 'wpagent_wc_store_settings_unavailable', __( 'Diese WooCommerce-Version stellt den sicheren Store-Einstellungslesepfad nicht bereit.', 'wp-agent-companion' ), array( 'status' => 409 ) );
		}
	}
	return true;
}

function wpagent_perm_manage_woocommerce( $input = null ): bool {
	unset( $input );
	return current_user_can( 'manage_woocommerce' );
}

/** @return true|WP_Error */
function wpagent_wc_require_store_settings_permission() {
	if ( ! current_user_can( 'manage_woocommerce' ) ) {
		return new WP_Error( 'wpagent_forbidden', __( 'Du darfst WooCommerce-Store-Einstellungen nicht lesen.', 'wp-agent-companion' ), array( 'status' => 403 ) );
	}
	return true;
}

/** @return array<string,mixed> */
function wpagent_wc_store_settings_payload(): array {
	$currency_position = (string) get_option( 'woocommerce_currency_pos', 'left' );
	if ( ! in_array( $currency_position, array( 'left', 'right', 'left_space', 'right_space' ), true ) ) {
		$currency_position = 'left';
	}
	$tax_basis = (string) get_option( 'woocommerce_tax_based_on', 'shipping' );
	if ( ! in_array( $tax_basis, array( 'shipping', 'billing', 'base' ), true ) ) {
		$tax_basis = 'shipping';
	}
	$shipping_destination = (string) get_option( 'woocommerce_ship_to_destination', 'billing' );
	if ( ! in_array( $shipping_destination, array( 'shipping', 'billing', 'billing_only' ), true ) ) {
		$shipping_destination = 'billing';
	}

	$payload = array(
		'currency' => array(
			'code'               => (string) get_woocommerce_currency(),
			'symbol'             => (string) get_woocommerce_currency_symbol(),
			'position'           => $currency_position,
			'thousand_separator' => (string) wc_get_price_thousand_separator(),
			'decimal_separator'  => (string) wc_get_price_decimal_separator(),
			'price_decimals'     => (int) wc_get_price_decimals(),
		),
		'taxes'    => array(
			'enabled'            => (bool) wc_tax_enabled(),
			'prices_include_tax' => (bool) wc_prices_include_tax(),
			'calculation_basis'  => $tax_basis,
		),
		'shipping' => array(
			'calculator_enabled'     => 'yes' === (string) get_option( 'woocommerce_enable_shipping_calc', 'yes' ),
			'destination'            => $shipping_destination,
			'cost_requires_address'  => 'yes' === (string) get_option( 'woocommerce_shipping_cost_requires_address', 'no' ),
			'hide_rates_when_free'   => 'yes' === (string) get_option( 'woocommerce_shipping_hide_rates_when_free', 'no' ),
		),
		'checkout' => array(
			'guest_checkout_enabled' => 'yes' === (string) get_option( 'woocommerce_enable_guest_checkout', 'yes' ),
			'coupons_enabled'        => 'yes' === (string) get_option( 'woocommerce_enable_coupons', 'yes' ),
		),
		'pages'    => array(
			'shop_page_id'      => (int) wc_get_page_id( 'shop' ),
			'cart_page_id'      => (int) wc_get_page_id( 'cart' ),
			'checkout_page_id'  => (int) wc_get_page_id( 'checkout' ),
			'myaccount_page_id' => (int) wc_get_page_id( 'myaccount' ),
			'terms_page_id'     => (int) wc_get_page_id( 'terms' ),
		),
	);
	$payload['state_hash'] = hash( 'sha256', (string) wp_json_encode( $payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) );
	return $payload;
}

/** @return array<string,mixed> */
function wpagent_wc_store_settings_schema(): array {
	return array(
		'type'                 => 'object',
		'properties'           => array(
			'currency' => array(
				'type'                 => 'object',
				'properties'           => array(
					'code'               => array( 'type' => 'string' ),
					'symbol'             => array( 'type' => 'string' ),
					'position'           => array( 'type' => 'string', 'enum' => array( 'left', 'right', 'left_space', 'right_space' ) ),
					'thousand_separator' => array( 'type' => 'string' ),
					'decimal_separator'  => array( 'type' => 'string' ),
					'price_decimals'     => array( 'type' => 'integer', 'minimum' => 0 ),
				),
				'required'             => array( 'code', 'symbol', 'position', 'thousand_separator', 'decimal_separator', 'price_decimals' ),
				'additionalProperties' => false,
			),
			'taxes'    => array(
				'type'                 => 'object',
				'properties'           => array(
					'enabled'            => array( 'type' => 'boolean' ),
					'prices_include_tax' => array( 'type' => 'boolean' ),
					'calculation_basis'  => array( 'type' => 'string', 'enum' => array( 'shipping', 'billing', 'base' ) ),
				),
				'required'             => array( 'enabled', 'prices_include_tax', 'calculation_basis' ),
				'additionalProperties' => false,
			),
			'shipping' => array(
				'type'                 => 'object',
				'properties'           => array(
					'calculator_enabled'    => array( 'type' => 'boolean' ),
					'destination'           => array( 'type' => 'string', 'enum' => array( 'shipping', 'billing', 'billing_only' ) ),
					'cost_requires_address' => array( 'type' => 'boolean' ),
					'hide_rates_when_free'  => array( 'type' => 'boolean' ),
				),
				'required'             => array( 'calculator_enabled', 'destination', 'cost_requires_address', 'hide_rates_when_free' ),
				'additionalProperties' => false,
			),
			'checkout' => array(
				'type'                 => 'object',
				'properties'           => array(
					'guest_checkout_enabled' => array( 'type' => 'boolean' ),
					'coupons_enabled'        => array( 'type' => 'boolean' ),
				),
				'required'             => array( 'guest_checkout_enabled', 'coupons_enabled' ),
				'additionalProperties' => false,
			),
			'pages'    => array(
				'type'                 => 'object',
				'properties'           => array(
					'shop_page_id'      => array( 'type' => 'integer' ),
					'cart_page_id'      => array( 'type' => 'integer' ),
					'checkout_page_id'  => array( 'type' => 'integer' ),
					'myaccount_page_id' => array( 'type' => 'integer' ),
					'terms_page_id'     => array( 'type' => 'integer' ),
				),
				'required'             => array( 'shop_page_id', 'cart_page_id', 'checkout_page_id', 'myaccount_page_id', 'terms_page_id' ),
				'additionalProperties' => false,
			),
			'state_hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
		),
		'required'             => array( 'currency', 'taxes', 'shipping', 'checkout', 'pages', 'state_hash' ),
		'additionalProperties' => false,
	);
}

function wpagent_ability_get_woocommerce_store_settings(): array {
	return array(
		'label'               => __( 'WooCommerce-Store-Einstellungen lesen', 'wp-agent-companion' ),
		'description'         => __( 'Liest einen begrenzten, geheimnisfreien Snapshot der effektiven WooCommerce-Store-Einstellungen.', 'wp-agent-companion' ),
		'category'            => 'wp-agent',
		'input_schema'        => array( 'type' => 'object', 'properties' => array(), 'additionalProperties' => false, 'default' => array() ),
		'output_schema'       => wpagent_wc_store_settings_schema(),
		'execute_callback'    => 'wpagent_exec_get_woocommerce_store_settings',
		'permission_callback' => 'wpagent_perm_manage_woocommerce',
		'meta'                => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_woocommerce_store_settings( $input = array() ) {
	unset( $input );
	$permitted = wpagent_wc_require_store_settings_permission();
	if ( is_wp_error( $permitted ) ) {
		return $permitted;
	}
	$available = wpagent_wc_require_store_settings_runtime();
	return is_wp_error( $available ) ? $available : wpagent_wc_store_settings_payload();
}
