<?php
/** Read-only ACPT Lite content-model abilities. */

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

/** @return array<string,mixed> */
function wpagent_acpt_list_ability( string $kind ): array {
	$is_post_type = 'post_type' === $kind;
	return array(
		'label' => $is_post_type ? __( 'ACPT-Lite-Inhaltstypen auflisten', 'wp-agent-companion' ) : __( 'ACPT-Lite-Taxonomien auflisten', 'wp-agent-companion' ),
		'description' => $is_post_type
			? __( 'Listet ausschließlich nicht native, in ACPT Lite persistierte Inhaltstypdefinitionen mit Konflikthash und Inhaltszahl.', 'wp-agent-companion' )
			: __( 'Listet ausschließlich nicht native, in ACPT Lite persistierte Taxonomiedefinitionen mit Konflikthash und Begriffsanzahl.', 'wp-agent-companion' ),
		'category' => 'wp-agent',
		'input_schema' => array(
			'type' => 'object',
			'properties' => array(
				'search' => array( 'type' => 'string', 'maxLength' => 100 ),
				'page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 1000000, 'default' => 1 ),
				'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 50, 'default' => 20 ),
			),
			'additionalProperties' => false,
		),
		'output_schema' => array(
			'type' => 'object',
			'properties' => array(
				'models' => array( 'type' => 'array', 'items' => wpagent_acpt_output_schema( $kind ) ),
				'total' => array( 'type' => 'integer' ), 'page' => array( 'type' => 'integer' ), 'per_page' => array( 'type' => 'integer' ),
				'total_pages' => array( 'type' => 'integer' ), 'provider_version' => array( 'type' => 'string' ),
			),
			'required' => array( 'models', 'total', 'page', 'per_page', 'total_pages', 'provider_version' ),
			'additionalProperties' => false,
		),
		'execute_callback' => $is_post_type ? 'wpagent_exec_list_acpt_post_types' : 'wpagent_exec_list_acpt_taxonomies',
		'permission_callback' => 'wpagent_perm_manage_options',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

/** @return array<string,mixed> */
function wpagent_acpt_get_ability( string $kind ): array {
	$is_post_type = 'post_type' === $kind;
	return array(
		'label' => $is_post_type ? __( 'ACPT-Lite-Inhaltstyp lesen', 'wp-agent-companion' ) : __( 'ACPT-Lite-Taxonomie lesen', 'wp-agent-companion' ),
		'description' => __( 'Liest eine nicht native ACPT-Lite-Definition für eine konfliktgeschützte Folgeoperation.', 'wp-agent-companion' ),
		'category' => 'wp-agent',
		'input_schema' => array(
			'type' => 'object', 'properties' => array( 'slug' => array( 'type' => 'string', 'pattern' => '^[a-z0-9][a-z0-9_-]*$', 'maxLength' => $is_post_type ? 20 : 32 ) ),
			'required' => array( 'slug' ), 'additionalProperties' => false,
		),
		'output_schema' => wpagent_acpt_output_schema( $kind ),
		'execute_callback' => $is_post_type ? 'wpagent_exec_get_acpt_post_type' : 'wpagent_exec_get_acpt_taxonomy',
		'permission_callback' => 'wpagent_perm_manage_options',
		'meta' => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ) ),
	);
}

function wpagent_ability_list_acpt_post_types(): array {
	return wpagent_acpt_list_ability( 'post_type' );
}

function wpagent_ability_get_acpt_post_type(): array {
	return wpagent_acpt_get_ability( 'post_type' );
}

function wpagent_ability_list_acpt_taxonomies(): array {
	return wpagent_acpt_list_ability( 'taxonomy' );
}

function wpagent_ability_get_acpt_taxonomy(): array {
	return wpagent_acpt_get_ability( 'taxonomy' );
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_list_acpt_models( string $kind, $input ) {
	$provider = wpagent_acpt_provider();
	if ( is_wp_error( $provider ) ) {
		return $provider;
	}
	$input = is_array( $input ) ? $input : array();
	$search = wpagent_acpt_text( $input['search'] ?? '', 'search', 100, true );
	if ( is_wp_error( $search ) ) {
		return $search;
	}
	$page = $input['page'] ?? 1;
	$per_page = $input['per_page'] ?? 20;
	if ( ! is_int( $page ) || $page < 1 || $page > 1000000 || ! is_int( $per_page ) || $per_page < 1 || $per_page > 50 ) {
		return new WP_Error( 'wpagent_acpt_pagination', __( 'page und per_page liegen außerhalb des erlaubten Bereichs.', 'wp-agent-companion' ), array( 'status' => 400 ) );
	}
	$models = wpagent_acpt_models( $kind );
	if ( is_wp_error( $models ) ) {
		return $models;
	}
	$needle = function_exists( 'mb_strtolower' ) ? mb_strtolower( $search, 'UTF-8' ) : strtolower( $search );
	$filtered = array();
	foreach ( $models as $model ) {
		$raw = wpagent_acpt_raw_model( $kind, $model );
		if ( is_wp_error( $raw ) ) {
			return $raw;
		}
		if ( $raw['native'] ) {
			continue;
		}
		$haystack = implode( ' ', array( $raw['slug'], $raw['singular'], $raw['plural'] ) );
		$haystack = function_exists( 'mb_strtolower' ) ? mb_strtolower( $haystack, 'UTF-8' ) : strtolower( $haystack );
		if ( '' === $needle || false !== strpos( $haystack, $needle ) ) {
			$filtered[] = array( 'model' => $model, 'raw' => $raw );
		}
	}
	usort( $filtered, static fn( array $a, array $b ): int => strcasecmp( $a['raw']['plural'], $b['raw']['plural'] ) );
	$total = count( $filtered );
	$page_models = array_slice( $filtered, ( $page - 1 ) * $per_page, $per_page );
	$counts = wpagent_acpt_content_counts( $kind, array_column( array_column( $page_models, 'raw' ), 'slug' ) );
	if ( is_wp_error( $counts ) ) {
		return $counts;
	}
	$meta_link_counts = wpagent_acpt_meta_link_counts( $kind, array_column( array_column( $page_models, 'raw' ), 'slug' ) );
	if ( is_wp_error( $meta_link_counts ) ) {
		return $meta_link_counts;
	}
	$states = array();
	foreach ( $page_models as $entry ) {
		$slug = $entry['raw']['slug'];
		$state = wpagent_acpt_state( $kind, $entry['model'], $counts[ $slug ] ?? 0, $meta_link_counts[ $slug ] ?? 0 );
		if ( is_wp_error( $state ) ) {
			return $state;
		}
		$states[] = $state;
	}
	return array(
		'models' => $states, 'total' => $total, 'page' => $page, 'per_page' => $per_page,
		'total_pages' => 0 === $total ? 0 : (int) ceil( $total / $per_page ), 'provider_version' => $provider['version'],
	);
}

/** @return array<string,mixed>|WP_Error */
function wpagent_exec_get_acpt_model( string $kind, $input ) {
	$input = is_array( $input ) ? $input : array();
	$slug = wpagent_acpt_slug( $input['slug'] ?? '', $kind );
	if ( is_wp_error( $slug ) ) {
		return $slug;
	}
	$model = wpagent_acpt_model( $kind, $slug );
	return is_wp_error( $model ) ? $model : wpagent_acpt_state( $kind, $model );
}

function wpagent_exec_list_acpt_post_types( $input ) {
	return wpagent_exec_list_acpt_models( 'post_type', $input );
}

function wpagent_exec_get_acpt_post_type( $input ) {
	return wpagent_exec_get_acpt_model( 'post_type', $input );
}

function wpagent_exec_list_acpt_taxonomies( $input ) {
	return wpagent_exec_list_acpt_models( 'taxonomy', $input );
}

function wpagent_exec_get_acpt_taxonomy( $input ) {
	return wpagent_exec_get_acpt_model( 'taxonomy', $input );
}
