<?php
/** Ability: wp-agent/delete-post-schema */

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

function wpagent_ability_delete_post_schema(): array {
	return array(
		'label'               => __( 'Post-Schema löschen und verifizieren', 'wp-agent-companion' ),
		'description'         => __( 'Bleibt für bestehende Clients registriert, lehnt aber endgültige Rank-Math-Schema-Löschungen ohne atomare Provider-Compare-and-Delete-Prüfung ab und verweist auf die WordPress-Verwaltung.', 'wp-agent-companion' ),
		'category'            => 'wp-agent',
		'input_schema'        => array(
			'type'                 => 'object',
			'properties'           => array(
				'post_id'           => array( 'type' => 'integer', 'minimum' => 1 ),
				'schema_id'         => array( 'type' => 'string', 'pattern' => '^schema-[1-9][0-9]*$' ),
				'confirm_schema_id' => array( 'type' => 'string', 'pattern' => '^schema-[1-9][0-9]*$' ),
				'expected_hash'     => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
			),
			'required'             => array( 'post_id', 'schema_id', 'confirm_schema_id', 'expected_hash' ),
			'additionalProperties' => false,
		),
		'output_schema'       => array(
			'type'                 => 'object',
			'properties'           => array(
				'post_id'               => array( 'type' => 'integer' ),
				'plugin'                => array( 'type' => 'string' ),
				'schema_id'             => array( 'type' => 'string' ),
				'deleted'               => array( 'type' => 'boolean' ),
				'rich_snippet_disabled' => array( 'type' => 'boolean' ),
				'previous_hash'          => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
				'state_hash'             => array( 'type' => 'string', 'pattern' => '^[a-f0-9]{64}$' ),
				'verified'               => array( 'type' => 'boolean' ),
				'rolled_back'            => array( 'type' => 'boolean' ),
				'rollback_verified'      => array( 'type' => array( 'boolean', 'null' ) ),
			),
			'required'             => array( 'post_id', 'plugin', 'schema_id', 'deleted', 'rich_snippet_disabled', 'previous_hash', 'state_hash', 'verified', 'rolled_back', 'rollback_verified' ),
			'additionalProperties' => false,
		),
		'execute_callback'    => 'wpagent_exec_delete_post_schema',
		'permission_callback' => 'wpagent_perm_edit_posts',
		'meta'                => array( 'show_in_rest' => true, 'annotations' => array( 'readonly' => false, 'destructive' => true, 'idempotent' => false, 'manual_only' => true ) ),
	);
}

/** @param mixed $input @return array<string,mixed>|WP_Error */
function wpagent_exec_delete_post_schema( $input ) {
	$input   = is_array( $input ) ? $input : array();
	$post_id = absint( $input['post_id'] ?? 0 );
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		return new WP_Error( 'wpagent_forbidden', __( 'Keine Berechtigung.', 'wp-agent-companion' ), array( 'status' => 403 ) );
	}
	if ( ! get_post( $post_id ) ) {
		return new WP_Error( 'wpagent_forbidden', __( 'Keine Berechtigung.', 'wp-agent-companion' ), array( 'status' => 403 ) );
	}
	$schema_id = (string) ( $input['schema_id'] ?? '' );
	$confirmed = (string) ( $input['confirm_schema_id'] ?? '' );
	if ( ! preg_match( '/^schema-[1-9][0-9]*$/', $schema_id ) ) {
		return new WP_Error( 'wpagent_invalid_schema_id', __( 'Die Rank-Math-Schema-ID ist ungültig.', 'wp-agent-companion' ), array( 'status' => 400 ) );
	}
	if ( ! preg_match( '/^schema-[1-9][0-9]*$/', $confirmed ) || $schema_id !== $confirmed ) {
		return new WP_Error( 'wpagent_schema_delete_confirmation', __( 'confirm_schema_id muss die zu löschende schema_id exakt wiederholen.', 'wp-agent-companion' ), array( 'status' => 400 ) );
	}
	$expected_hash = strtolower( (string) ( $input['expected_hash'] ?? '' ) );
	if ( ! preg_match( '/^[a-f0-9]{64}$/', $expected_hash ) ) {
		return new WP_Error( 'wpagent_schema_hash', __( 'expected_hash muss ein vollständiger SHA-256-Hash aus get-post-schema sein.', 'wp-agent-companion' ), array( 'status' => 400 ) );
	}
	return new WP_Error(
		'wpagent_schema_delete_manual_only',
		__( 'Post-Schemas werden nicht remote endgültig gelöscht, weil Rank Math keine atomare Compare-and-Delete-Prüfung gegen parallele Provideränderungen anbietet. Lösche das Schema manuell im WordPress-Admin.', 'wp-agent-companion' ),
		array( 'status' => 409, 'post_id' => $post_id, 'schema_id' => $schema_id, 'current_hash' => $expected_hash, 'manual_action_required' => true )
	);
}
