<?php
/** Read-only Site Editor content abilities. */

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

/** @return array<string,mixed> */
function wpagent_site_editor_read_ability( string $label, string $description, array $input, array $output, string $callback ): array {
	return array(
		'label' => $label, 'description' => $description, 'category' => 'wp-agent',
		'input_schema' => $input, 'output_schema' => $output, 'execute_callback' => $callback,
		'permission_callback' => 'wpagent_perm_edit_theme_options',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @return array<string,mixed> */
function wpagent_site_editor_list_input_schema( bool $with_area = false ): array {
	$properties = array(
		'search'   => array( 'type' => 'string', 'maxLength' => 200, 'default' => '' ),
		'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 50 ),
	);
	if ( $with_area ) {
		$properties['area'] = array( 'type' => 'string', 'maxLength' => 50 );
	}
	return array( 'type' => 'object', 'properties' => $properties, 'additionalProperties' => false, 'default' => array() );
}

/** @return array<string,mixed> */
function wpagent_site_editor_list_output_schema( array $item_schema ): array {
	return array(
		'type' => 'object', 'properties' => array(
			'items' => array( 'type' => 'array', 'items' => $item_schema ),
			'total' => array( 'type' => 'integer' ), 'truncated' => array( 'type' => 'boolean' ),
		),
		'required' => array( 'items', 'total', 'truncated' ), 'additionalProperties' => false,
	);
}

function wpagent_ability_list_site_templates(): array {
	return wpagent_site_editor_read_ability(
		__( 'Site-Templates auflisten', 'wp-agent-companion' ),
		__( 'Listet Theme-, Plugin- und benutzerdefinierte Blocktemplates des aktiven Themes samt tiefem Blockbericht und Zustands-Hash auf.', 'wp-agent-companion' ),
		wpagent_site_editor_list_input_schema(), wpagent_site_editor_list_output_schema( wpagent_site_editor_resource_schema() ),
		'wpagent_exec_list_site_templates'
	);
}

function wpagent_ability_get_site_template(): array {
	return wpagent_site_editor_read_ability(
		__( 'Site-Template lesen', 'wp-agent-companion' ),
		__( 'Liest ein effektives Blocktemplate des aktiven Themes einschließlich Ursprung, Rohinhalt, tiefem Blockbericht und CAS-Hash.', 'wp-agent-companion' ),
		array( 'type' => 'object', 'properties' => array( 'slug' => array( 'type' => 'string', 'maxLength' => 200 ) ), 'required' => array( 'slug' ), 'additionalProperties' => false ),
		wpagent_site_editor_resource_schema(), 'wpagent_exec_get_site_template'
	);
}

function wpagent_ability_list_template_parts(): array {
	return wpagent_site_editor_read_ability(
		__( 'Template-Teile auflisten', 'wp-agent-companion' ),
		__( 'Listet alle Template-Teile des aktiven Themes auf, optional nach Bereich gefiltert.', 'wp-agent-companion' ),
		wpagent_site_editor_list_input_schema( true ), wpagent_site_editor_list_output_schema( wpagent_site_editor_resource_schema() ),
		'wpagent_exec_list_template_parts'
	);
}

function wpagent_ability_get_template_part(): array {
	return wpagent_site_editor_read_ability(
		__( 'Template-Teil lesen', 'wp-agent-companion' ),
		__( 'Liest ein effektives Template-Teil einschließlich Bereich, Ursprung, Rohinhalt und CAS-Hash.', 'wp-agent-companion' ),
		array( 'type' => 'object', 'properties' => array( 'slug' => array( 'type' => 'string', 'maxLength' => 200 ) ), 'required' => array( 'slug' ), 'additionalProperties' => false ),
		wpagent_site_editor_resource_schema(), 'wpagent_exec_get_template_part'
	);
}

function wpagent_ability_list_block_navigations(): array {
	return wpagent_site_editor_read_ability(
		__( 'Blocknavigationen auflisten', 'wp-agent-companion' ),
		__( 'Listet veröffentlichte Blocknavigationen samt Eintragszahl, tiefem Blockbericht und CAS-Hash auf.', 'wp-agent-companion' ),
		wpagent_site_editor_list_input_schema(), wpagent_site_editor_list_output_schema( wpagent_site_editor_navigation_schema() ),
		'wpagent_exec_list_block_navigations'
	);
}

function wpagent_ability_get_block_navigation(): array {
	return wpagent_site_editor_read_ability(
		__( 'Blocknavigation lesen', 'wp-agent-companion' ),
		__( 'Liest eine Blocknavigation einschließlich Rohinhalt, Eintragszahl, tiefem Blockbericht und CAS-Hash.', 'wp-agent-companion' ),
		array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer', 'minimum' => 1 ) ), 'required' => array( 'id' ), 'additionalProperties' => false ),
		wpagent_site_editor_navigation_schema(), 'wpagent_exec_get_block_navigation'
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_list_site_templates( $input ) {
	return wpagent_site_editor_list_templates( 'wp_template', is_array( $input ) ? $input : array() );
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_site_template( $input ) {
	return wpagent_site_editor_get_resource( 'wp_template', is_array( $input ) ? $input : array() );
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_list_template_parts( $input ) {
	return wpagent_site_editor_list_templates( 'wp_template_part', is_array( $input ) ? $input : array() );
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_template_part( $input ) {
	return wpagent_site_editor_get_resource( 'wp_template_part', is_array( $input ) ? $input : array() );
}

/** @return array<string,mixed>|WP_Error */
function wpagent_site_editor_get_resource( string $type, array $input ) {
	$slug = wpagent_site_editor_slug( $input['slug'] ?? '' );
	if ( is_wp_error( $slug ) ) return $slug;
	$template = wpagent_site_editor_get_template( $type, $slug );
	return is_wp_error( $template ) ? $template : wpagent_site_editor_resource_state( $template );
}

/** @return array<string,mixed>|WP_Error */
function wpagent_site_editor_list_templates( string $type, array $input ) {
	$search = isset( $input['search'] ) ? wpagent_site_editor_plain_text( $input['search'], 'search', 200, true ) : '';
	if ( is_wp_error( $search ) ) return $search;
	$per_page = max( 1, min( 100, absint( $input['per_page'] ?? 50 ) ) );
	$query = array();
	if ( 'wp_template_part' === $type && isset( $input['area'] ) ) {
		$area = wpagent_site_editor_plain_text( $input['area'], 'area', 50, true );
		if ( is_wp_error( $area ) ) return $area;
		if ( '' !== $area ) $query['area'] = $area;
	}
	$items = array();
	foreach ( get_block_templates( $query, $type ) as $template ) {
		$state = wpagent_site_editor_resource_state( $template );
		if ( '' !== $search && false === stripos( $state['slug'] . "\n" . $state['title'] . "\n" . $state['description'], $search ) ) continue;
		$items[] = $state;
	}
	usort( $items, static fn( array $a, array $b ): int => strnatcasecmp( $a['title'] ?: $a['slug'], $b['title'] ?: $b['slug'] ) );
	$total = count( $items );
	return array( 'items' => array_slice( $items, 0, $per_page ), 'total' => $total, 'truncated' => $total > $per_page );
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_list_block_navigations( $input ) {
	$input  = is_array( $input ) ? $input : array();
	$search = isset( $input['search'] ) ? wpagent_site_editor_plain_text( $input['search'], 'search', 200, true ) : '';
	if ( is_wp_error( $search ) ) return $search;
	$per_page = max( 1, min( 100, absint( $input['per_page'] ?? 50 ) ) );
	$query = new WP_Query(
		array(
			'post_type' => 'wp_navigation', 'post_status' => 'publish', 'posts_per_page' => $per_page,
			'orderby' => 'title', 'order' => 'ASC', 's' => $search, 'no_found_rows' => false,
		)
	);
	$items = array();
	foreach ( $query->posts as $post ) {
		$state = wpagent_site_editor_navigation_state( (int) $post->ID );
		if ( ! is_wp_error( $state ) ) $items[] = $state;
	}
	return array( 'items' => $items, 'total' => (int) $query->found_posts, 'truncated' => (int) $query->found_posts > count( $items ) );
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_block_navigation( $input ) {
	$input = is_array( $input ) ? $input : array();
	return wpagent_site_editor_navigation_state( absint( $input['id'] ?? 0 ) );
}
