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

namespace SRFM\Inc\Abilities\Entries;

use SRFM\Inc\Abilities\Abstract_Ability;
use SRFM\Inc\Database\Tables\Entries as EntriesTable;
use SRFM\Inc\Helper;

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

/**
 * Get_Entry ability class.
 *
 * Retrieves detailed information about a specific form submission entry,
 * including parsed form data with decrypted labels.
 *
 * @since 2.5.2
 */
class Get_Entry extends Abstract_Ability {
	use Entry_Parser;

	/**
	 * Constructor.
	 *
	 * @since 2.5.2
	 */
	public function __construct() {
		$this->id          = 'sureforms/get-entry';
		$this->label       = __( 'Get Entry Details', 'sureforms' );
		$this->description = __( 'Retrieve detailed information about a specific SureForms form submission entry, including all submitted field data with labels.', 'sureforms' );
		$this->capability  = 'manage_options';
	}

	/**
	 * {@inheritDoc}
	 *
	 * @since 2.5.2
	 */
	public function get_annotations() {
		return [
			'readonly'      => true,
			'destructive'   => false,
			'idempotent'    => true,
			'priority'      => 1.0,
			'openWorldHint' => false,
		];
	}

	/**
	 * {@inheritDoc}
	 *
	 * @since 2.5.2
	 */
	public function get_input_schema() {
		return [
			'type'                 => 'object',
			'additionalProperties' => false,
			'properties'           => [
				'entry_id' => [
					'type'        => 'integer',
					'description' => __( 'The ID of the entry to retrieve.', 'sureforms' ),
				],
			],
			'required'             => [ 'entry_id' ],
		];
	}

	/**
	 * {@inheritDoc}
	 *
	 * @since 2.5.2
	 */
	public function get_output_schema() {
		return [
			'type'       => 'object',
			'properties' => [
				'id'              => [ 'type' => 'integer' ],
				'form_id'         => [ 'type' => 'integer' ],
				'form_name'       => [ 'type' => 'string' ],
				'status'          => [ 'type' => 'string' ],
				'created_at'      => [ 'type' => 'string' ],
				'form_data'       => [
					'type'  => 'array',
					'items' => [
						'type'       => 'object',
						'properties' => [
							'label'      => [ 'type' => 'string' ],
							'value'      => [],
							'block_name' => [ 'type' => 'string' ],
						],
					],
				],
				'submission_info' => [ 'type' => 'object' ],
				'user'            => [ 'type' => [ 'object', 'null' ] ],
			],
		];
	}

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

		if ( empty( $entry ) ) {
			return new \WP_Error(
				'srfm_entry_not_found',
				__( 'Entry not found.', 'sureforms' ),
				[ 'status' => 404 ]
			);
		}

		$parsed = $this->parse_entry( $entry );

		return array_merge( [ 'id' => $entry_id ], $parsed );
	}
}
