<?php
declare(strict_types=1);

// THIS FILE IS AUTO-GENERATED. DO NOT EDIT MANUALLY.

namespace Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLQueries;

use Automattic\WooCommerce\Api\Queries\Coupons\ListCoupons as ListCouponsCommand;
use Automattic\WooCommerce\Api\Infrastructure\QueryInfoExtractor;
use Automattic\WooCommerce\Api\Infrastructure\ResolverHelpers;
use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Pagination\CouponConnection as CouponConnectionType;
use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Enums\CouponStatus as CouponStatusType;
use Automattic\WooCommerce\Api\Infrastructure\Schema\ResolveInfo;
use Automattic\WooCommerce\Api\Infrastructure\Schema\Type;

class ListCoupons {
	public static function get_field_definition(): array {
		return array(
			'type'          => Type::nonNull( CouponConnectionType::get() ),
			'description'   => __( 'List coupons with cursor-based pagination.', 'woocommerce' ),
			'authorization' => array(
				array(
					'attribute' => 'RequiredCapability',
					'args'      => array(
						0 => 'read_private_shop_coupons',
					),
				),
			),
			'args'          => array(
				'first'  => array(
					'type'         => Type::int(),
					'description'  => __( 'Return the first N results. Must be between 0 and 100.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'last'   => array(
					'type'         => Type::int(),
					'description'  => __( 'Return the last N results. Must be between 0 and 100.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'after'  => array(
					'type'         => Type::string(),
					'description'  => __( 'Return results after this cursor.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'before' => array(
					'type'         => Type::string(),
					'description'  => __( 'Return results before this cursor.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'status' => array(
					'type'         => CouponStatusType::get(),
					'description'  => __( 'Filter by coupon status.', 'woocommerce' ),
					'defaultValue' => null,
				),
			),
			'complexity'    => ResolverHelpers::complexity_from_pagination( ... ),
			'resolve'       => array( self::class, 'resolve' ),
		);
	}

	public static function resolve( mixed $root, array $args, mixed $context, ResolveInfo $info ): mixed {
		// Standalone authorization gate: no authorize() method on the command,
		// so the autodiscovered authorization attributes are the sole guard.
		if ( ! self::compute_preauthorized( $context['principal'] ) ) {
			throw ResolverHelpers::build_authorization_error( $context['principal'] );
		}

		// Publish the root query's metadata so downstream field-level
		// authorization gates can read it via `$_metadata['query']`.
		// $context is an ArrayObject (see GraphQLController::process_request())
		// so the mutation propagates to nested resolvers.
		$context['_query_metadata'] = array();

		$command = \Automattic\WooCommerce\Api\Infrastructure\ClassResolver::resolve_class( ListCouponsCommand::class );

		$execute_args               = array();
		$execute_args['pagination'] = ResolverHelpers::create_pagination_params( $args );
		if ( array_key_exists( 'status', $args ) ) {
			$execute_args['status'] = $args['status'];
		}

		$result = ResolverHelpers::execute_command( $command, $execute_args );

		return $result;
	}

	/**
	 * Compute the value `_preauthorized` would carry for a given principal —
	 * the AND of the autodiscovered authorization attributes' authorize()
	 * outcomes on this command. Single source of truth for both the resolver's
	 * own gates and external (code-API) callers asking about authorization
	 * without going through GraphQL execution.
	 *
	 * Returns true vacuously when the command has no authorization attributes
	 * (in that case authorize() on the command is the sole guard, and that
	 * method should be consulted instead).
	 */
	public static function compute_preauthorized( \Automattic\WooCommerce\Api\Infrastructure\Principal $principal ): bool {
		return ( new \Automattic\WooCommerce\Api\Attributes\RequiredCapability( 'read_private_shop_coupons' ) )->authorize( $principal );
	}
}
