<?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',
			'additionalProperties' => false,
			'properties' => array(
				'search' => array( 'type' => 'string', 'maxLength' => 255 ),
				'page' => array( 'type' => 'integer', 'minimum' => 1, 'default' => 1 ),
				'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 20 ),
			),
		),
		'output_schema' => wpagent_forms_public_object_schema(
			array(
				'forms' => wpagent_forms_public_list_schema( wpagent_cf7_form_list_item_schema(), WPAGENT_FORMS_PUBLIC_MAX_FORMS ),
				'total' => array( 'type' => 'integer', 'minimum' => 0, 'maximum' => 1000000 ),
				'page' => array( 'type' => 'integer', 'minimum' => 1 ),
				'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => WPAGENT_FORMS_PUBLIC_MAX_FORMS ),
			), array( 'forms', 'total', 'page', 'per_page' )
		),
		'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 );
	if ( ! is_array( $forms ) || count( $forms ) > WPAGENT_FORMS_PUBLIC_MAX_FORMS ) {
		return wpagent_forms_public_output_error( 'cf7', 'forms' );
	}
	$rows = array();
	foreach ( $forms as $form ) {
		if ( ! $form instanceof WPCF7_ContactForm ) {
			return wpagent_forms_public_output_error( 'cf7', 'forms' );
		}
		$payload = wpagent_cf7_form_payload( $form );
		if ( is_wp_error( $payload ) ) {
			return $payload;
		}
		$rows[] = array_intersect_key( $payload, array_flip( array( 'id', 'hash', 'state_hash', 'slug', 'title', 'locale', 'shortcode' ) ) );
	}
	$total = (int) WPCF7_ContactForm::count();
	if ( $total > 1000000 ) {
		return wpagent_forms_public_output_error( 'cf7', 'total' );
	}
	return array( 'forms' => $rows, 'total' => $total, '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', 'additionalProperties' => false, 'properties' => array( 'form_id' => array( 'type' => 'integer', 'minimum' => 1 ) ), '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 );
}
