<?php
/** Contact Form 7 discovery and read abilities. */

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

function wpagent_ability_list_contact_forms(): array {
	return array(
		'label' => __( 'Kontaktformulare auflisten', 'wp-agent-companion' ),
		'description' => __( 'Listet Contact-Form-7-Formulare mit Suche und Pagination über die native Plugin-API.', 'wp-agent-companion' ),
		'category' => 'wp-agent',
		'input_schema' => array(
			'type' => 'object',
			'properties' => array(
				'search' => array( 'type' => 'string' ),
				'page' => array( 'type' => 'integer', 'minimum' => 1, 'default' => 1 ),
				'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 20 ),
			),
		),
		'output_schema' => array(
			'type' => 'object',
			'properties' => array(
				'forms' => array( 'type' => 'array', 'items' => array( 'type' => 'object' ) ),
				'total' => array( 'type' => 'integer' ),
				'page' => array( 'type' => 'integer' ),
				'per_page' => array( 'type' => 'integer' ),
			),
		),
		'execute_callback' => 'wpagent_exec_list_contact_forms',
		'permission_callback' => 'wpagent_perm_cf7_read',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @param mixed $input @return array|WP_Error */
function wpagent_exec_list_contact_forms( $input ) {
	if ( ! wpagent_cf7_available() ) {
		return wpagent_cf7_missing_error();
	}
	$input = is_array( $input ) ? $input : array();
	$page = max( 1, (int) ( $input['page'] ?? 1 ) );
	$per_page = min( 100, max( 1, (int) ( $input['per_page'] ?? 20 ) ) );
	$args = array(
		'posts_per_page' => $per_page,
		'offset' => ( $page - 1 ) * $per_page,
		'orderby' => 'ID',
		'order' => 'DESC',
	);
	if ( '' !== trim( (string) ( $input['search'] ?? '' ) ) ) {
		$args['s'] = trim( (string) $input['search'] );
	}
		$forms = WPCF7_ContactForm::find( $args );
	$rows = array_map(
		static fn( WPCF7_ContactForm $form ): array => array(
			'id' => (int) $form->id(),
			'hash' => (string) $form->hash(),
			'state_hash' => wpagent_cf7_state_hash( $form ),
			'slug' => (string) $form->name(),
			'title' => (string) $form->title(),
			'locale' => (string) $form->locale(),
			'shortcode' => (string) $form->shortcode(),
		),
		$forms
	);
	return array( 'forms' => $rows, 'total' => (int) WPCF7_ContactForm::count(), 'page' => $page, 'per_page' => $per_page );
}

function wpagent_ability_get_contact_form(): array {
	return array(
		'label' => __( 'Kontaktformular lesen', 'wp-agent-companion' ),
		'description' => __( 'Liest ein Contact-Form-7-Formular einschließlich sicher begrenzter Mail- und Meldungseinstellungen, Shortcode, Zustands-Hash und Konfigurationsfehlern.', 'wp-agent-companion' ),
		'category' => 'wp-agent',
		'input_schema' => array( 'type' => 'object', 'properties' => array( 'form_id' => array( 'type' => 'integer' ) ), 'required' => array( 'form_id' ) ),
		'output_schema' => wpagent_cf7_form_output_schema(),
		'execute_callback' => 'wpagent_exec_get_contact_form',
		'permission_callback' => 'wpagent_perm_cf7_edit',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @param mixed $input @return array|WP_Error */
function wpagent_exec_get_contact_form( $input ) {
	$form = wpagent_cf7_get_authorized_form( $input, 'wpcf7_edit_contact_form' );
	return is_wp_error( $form ) ? $form : wpagent_cf7_form_payload( $form );
}
