<?php
/** Ability definitions for transactional MB Settings Page management. */

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

/** @return array<string,mixed> */
function wpagent_metabox_settings_page_settings_schema(): array {
	return array( 'type' => 'object', 'additionalProperties' => true );
}

/** @return array<string,mixed> */
function wpagent_metabox_settings_page_state_schema(): array {
	$settings = wpagent_metabox_settings_page_settings_schema();
	return array(
		'type' => 'object',
		'properties' => array(
			'id' => array( 'type' => 'integer', 'minimum' => 1 ),
			'title' => array( 'type' => 'string' ),
			'slug' => array( 'type' => 'string' ),
			'status' => array( 'type' => 'string' ),
			'settings' => $settings,
			'runtime' => array( 'type' => array( 'object', 'null' ), 'additionalProperties' => true ),
			'hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
		),
		'required' => array( 'id', 'title', 'slug', 'status', 'settings', 'runtime', 'hash' ),
		'additionalProperties' => false,
	);
}

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

/** @return array<string,mixed> */
function wpagent_ability_list_meta_box_settings_pages(): array {
	return wpagent_metabox_settings_page_ability(
		__( 'Meta-Box-Einstellungsseiten auflisten', 'wp-agent-companion' ),
		__( 'Listet persistente MB-Settings-Page-Definitionen mit Laufzeitzustand und Konflikthash.', 'wp-agent-companion' ),
		array( 'type' => 'object', 'properties' => array( 'search' => array( 'type' => 'string', 'maxLength' => 100 ), 'limit' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 50 ), 'offset' => array( 'type' => 'integer', 'minimum' => 0, 'maximum' => 10000, 'default' => 0 ) ), 'additionalProperties' => false ),
		array( 'type' => 'object', 'properties' => array( 'settings_pages' => array( 'type' => 'array', 'items' => wpagent_metabox_settings_page_state_schema() ), 'total' => array( 'type' => 'integer', 'minimum' => 0 ) ), 'required' => array( 'settings_pages', 'total' ), 'additionalProperties' => false ),
		'wpagent_metabox_list_settings_pages',
		true
	);
}

/** @return array<string,mixed> */
function wpagent_ability_get_meta_box_settings_page(): array {
	return wpagent_metabox_settings_page_ability(
		__( 'Meta-Box-Einstellungsseite lesen', 'wp-agent-companion' ),
		__( 'Liest eine persistente MB-Settings-Page-Definition samt aktivem Laufzeitzustand.', 'wp-agent-companion' ),
		array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer', 'minimum' => 1 ) ), 'required' => array( 'id' ), 'additionalProperties' => false ),
		wpagent_metabox_settings_page_state_schema(),
		'wpagent_metabox_get_settings_page',
		true
	);
}

/** @return array<string,mixed> */
function wpagent_ability_create_meta_box_settings_page(): array {
	return wpagent_metabox_settings_page_ability(
		__( 'Meta-Box-Einstellungsseite erstellen', 'wp-agent-companion' ),
		__( 'Erstellt eine geprüfte MB-Settings-Page-Definition und verifiziert Builder- sowie Laufzeitzustand.', 'wp-agent-companion' ),
		array(
			'type' => 'object',
			'properties' => array(
				'title' => array( 'type' => 'string', 'minLength' => 1, 'maxLength' => 200 ),
				'status' => array( 'type' => 'string', 'enum' => array( 'publish', 'draft' ), 'default' => 'publish' ),
				'settings' => wpagent_metabox_settings_page_settings_schema(),
			),
			'required' => array( 'title', 'settings' ),
			'additionalProperties' => false,
		),
		wpagent_metabox_settings_page_state_schema(),
		'wpagent_metabox_create_settings_page'
	);
}

/** @return array<string,mixed> */
function wpagent_ability_update_meta_box_settings_page(): array {
	return wpagent_metabox_settings_page_ability(
		__( 'Meta-Box-Einstellungsseite aktualisieren', 'wp-agent-companion' ),
		__( 'Ersetzt eine unveränderte MB-Settings-Page-Definition mit Sperre, Read-back und Rollback.', 'wp-agent-companion' ),
		array(
			'type' => 'object',
			'properties' => array(
				'id' => array( 'type' => 'integer', 'minimum' => 1 ),
				'expected_hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
				'title' => array( 'type' => 'string', 'minLength' => 1, 'maxLength' => 200 ),
				'status' => array( 'type' => 'string', 'enum' => array( 'publish', 'draft' ) ),
				'settings' => wpagent_metabox_settings_page_settings_schema(),
			),
			'required' => array( 'id', 'expected_hash', 'settings' ),
			'additionalProperties' => false,
		),
		wpagent_metabox_settings_page_state_schema(),
		'wpagent_metabox_update_settings_page'
	);
}

/** @return array<string,mixed> */
function wpagent_ability_delete_meta_box_settings_page(): array {
	return wpagent_metabox_settings_page_ability(
		__( 'Meta-Box-Einstellungsseite löschen', 'wp-agent-companion' ),
		__( 'Verschiebt eine unveränderte Definition in den Papierkorb oder löscht sie nach ID-Bestätigung dauerhaft. Optionsdaten bleiben erhalten.', 'wp-agent-companion' ),
		array(
			'type' => 'object',
			'properties' => array(
				'id' => array( 'type' => 'integer', 'minimum' => 1 ),
				'expected_hash' => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
				'force' => array( 'type' => 'boolean', 'default' => false ),
				'confirm_settings_page_id' => array( 'type' => 'string' ),
			),
			'required' => array( 'id', 'expected_hash' ),
			'additionalProperties' => false,
		),
		array( 'type' => 'object', 'properties' => array( 'deleted' => array( 'type' => 'boolean' ), 'previous' => wpagent_metabox_settings_page_state_schema() ), 'required' => array( 'deleted', 'previous' ), 'additionalProperties' => false ),
		'wpagent_metabox_delete_settings_page',
		false,
		true
	);
}
