<?php
/**
 * Update Entry Status Ability.
 *
 * @package sureforms
 * @since 2.5.2
 */

namespace SRFM\Inc\Abilities\Entries;

use SRFM\Inc\Abilities\Abstract_Ability;
use SRFM\Inc\Entries;
use SRFM\Inc\Helper;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Update_Entry_Status ability class.
 *
 * Updates the status of one or more form submission entries.
 *
 * @since 2.5.2
 */
class Update_Entry_Status extends Abstract_Ability {
	/**
	 * Maximum number of entries that can be updated in a single call.
	 *
	 * @since 2.5.2
	 */
	public const MAX_ENTRIES = 50;

	/**
	 * Constructor.
	 *
	 * @since 2.5.2
	 */
	public function __construct() {
		$this->id          = 'sureforms/update-entry-status';
		$this->label       = __( 'Update Entry Status', 'sureforms' );
		$this->description = __( 'Update the status of one or more SureForms form submission entries. Supports read, unread, trash, and restore operations.', 'sureforms' );
		$this->capability  = 'manage_options';
		$this->gated       = 'srfm_abilities_api_edit';
	}

	/**
	 * {@inheritDoc}
	 *
	 * @since 2.5.2
	 */
	public function get_annotations() {
		return [
			'readonly'      => false,
			'destructive'   => true,
			'idempotent'    => true,
			'priority'      => 2.0,
			'openWorldHint' => false,
			'instructions'  => 'Confirm the target status with the user before executing. Trashing entries moves them toward permanent deletion.',
		];
	}

	/**
	 * {@inheritDoc}
	 *
	 * @since 2.5.2
	 */
	public function get_input_schema() {
		return [
			'type'                 => 'object',
			'additionalProperties' => false,
			'properties'           => [
				'entry_ids' => [
					'type'        => 'array',
					'description' => __( 'Array of entry IDs to update.', 'sureforms' ),
					'items'       => [ 'type' => 'integer' ],
				],
				'status'    => [
					'type'        => 'string',
					'description' => __( 'New status for the entries.', 'sureforms' ),
					'enum'        => [ 'read', 'unread', 'trash', 'restore' ],
				],
			],
			'required'             => [ 'entry_ids', 'status' ],
		];
	}

	/**
	 * {@inheritDoc}
	 *
	 * @since 2.5.2
	 */
	public function get_output_schema() {
		return [
			'type'       => 'object',
			'properties' => [
				'success' => [ 'type' => 'boolean' ],
				'updated' => [ 'type' => 'integer' ],
				'errors'  => [
					'type'  => 'array',
					'items' => [ 'type' => 'string' ],
				],
			],
		];
	}

	/**
	 * Execute the update-entry-status ability.
	 *
	 * @param array<string,mixed> $input Validated input data.
	 * @since 2.5.2
	 * @return array<string,mixed>|\WP_Error
	 */
	public function execute( $input ) {
		$entry_ids = $input['entry_ids'] ?? [];
		$status    = sanitize_text_field( Helper::get_string_value( $input['status'] ?? '' ) );

		if ( empty( $entry_ids ) || ! is_array( $entry_ids ) ) {
			return new \WP_Error(
				'srfm_missing_entry_ids',
				__( 'At least one entry ID is required.', 'sureforms' ),
				[ 'status' => 400 ]
			);
		}

		if ( count( $entry_ids ) > self::MAX_ENTRIES ) {
			return new \WP_Error(
				'srfm_too_many_entry_ids',
				/* translators: %d is the maximum number of entries allowed. */
				sprintf( __( 'Maximum %d entry IDs allowed per request.', 'sureforms' ), self::MAX_ENTRIES ),
				[ 'status' => 400 ]
			);
		}

		$entry_ids = array_map( 'absint', $entry_ids );

		if ( empty( $status ) ) {
			return new \WP_Error(
				'srfm_missing_status',
				__( 'Status is required.', 'sureforms' ),
				[ 'status' => 400 ]
			);
		}

		return Entries::update_status( $entry_ids, $status );
	}
}
