<?php
/**
 * WooCommerce Measurement Price Calculator
 *
 * This source file is subject to the GNU General Public License v3.0
 * that is bundled with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.html
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@skyverge.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade WooCommerce Measurement Price Calculator to newer
 * versions in the future. If you wish to customize WooCommerce Measurement Price Calculator for your
 * needs please refer to http://docs.woocommerce.com/document/measurement-price-calculator/ for more information.
 *
 * @author    SkyVerge
 * @copyright Copyright (c) 2012-2026, SkyVerge, Inc. (info@skyverge.com)
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
 */

namespace SkyVerge\WooCommerce\Measurement_Price_Calculator\Products\Abilities;

use SkyVerge\WooCommerce\Measurement_Price_Calculator\Abilities\Provider;
use SkyVerge\WooCommerce\PluginFramework\v6_2_0\Abilities\Contracts\MakesAbilityContract;
use SkyVerge\WooCommerce\PluginFramework\v6_2_0\Abilities\DataObjects\Ability;
use SkyVerge\WooCommerce\PluginFramework\v6_2_0\Abilities\DataObjects\AbilityAnnotations;
use WC_Price_Calculator_Settings;
use WP_Error;

defined( 'ABSPATH' ) or exit;

/**
 * Ability that returns the measurement price calculator configuration for a product.
 *
 * @since 3.24.0
 */
class GetProductCalculatorSettings implements MakesAbilityContract {

	const NAME = 'woocommerce-measurement-price-calculator/product-calculator-settings-get';

	/** @inheritDoc */
	public function makeAbility() : Ability
	{
		return new Ability(
			static::NAME,
			__( 'Get Product Calculator Settings', 'woocommerce-measurement-price-calculator' ),
			__( 'Retrieves the measurement price calculator configuration for a product.', 'woocommerce-measurement-price-calculator' ),
			Provider::PRODUCTS_CATEGORY_SLUG,
			function ( int $productId ) {
				$product = wc_get_product( $productId );

				if ( ! $product ) {
					return new WP_Error(
						'product_not_found',
						/* translators: %d: product ID */
						sprintf( __( 'Product with ID %d was not found.', 'woocommerce-measurement-price-calculator' ), $productId ),
						[ 'status' => 404 ]
					);
				}

				return new WC_Price_Calculator_Settings( $product );
			},
			function () {
				return current_user_can( 'manage_woocommerce' );
			},
			$this->getInputSchema(),
			WC_Price_Calculator_Settings::getJsonSchema(),
			new AbilityAnnotations( true, false, true ),
			true
		);
	}

	/**
	 * Gets the input schema for the ability.
	 *
	 * @since 3.24.0
	 *
	 * @return array<string, mixed>
	 */
	protected function getInputSchema() : array
	{
		return [
			'type'        => 'integer',
			'description' => __( 'The product ID.', 'woocommerce-measurement-price-calculator' ),
			'required'    => true,
			'minimum'     => 1,
		];
	}
}
