<?php
declare(strict_types=1);
if (!defined('ABSPATH')) { exit; }
wp_register_ability('tpaep/tpaep-post-search', [
'label' => __('Post Search', 'theplus'),
    'description' => __('Adds The Plus "Post Search" widget (tp-post-search) to an Elementor container.', 'theplus'),
    'category' => 'tpae',
    'input_schema' => ['type' => 'object', 'properties' => [
        'post_id' => ['type' => 'integer', 'description' => 'Elementor page/post ID'],
        'parent_id' => ['type' => 'string', 'description' => 'Target Elementor container ID'],
        'position' => ['type' => 'integer', 'description' => 'Insert position (-1 = append)', 'default' => -1],
        'deprecated_notice_post_search' => ['type' => 'string', 'description' => 'deprecated_notice_post_search'],
        'form_style' => ['type' => 'string', 'description' => 'Style'],
        'content_align' => ['type' => 'object', 'description' => 'Alignment', 'enum' => ['center', 'devices', 'icon', 'left', 'right']],
        'search_field_placeholder' => ['type' => 'string', 'description' => 'Search Field Placeholder'],
        'search_icon' => ['type' => 'string', 'description' => 'Icon Font', 'enum' => ['font_awesome', 'font_awesome_5']],
        'search_icon_fontawesome' => ['type' => 'string', 'description' => 'Search Icon Prefix'],
        'search_icon_fontawesome_5' => ['type' => 'string', 'description' => 'Icon Library'],
        'button_text' => ['type' => 'string', 'description' => 'Button Text'],
        'button_icon_style' => ['type' => 'string', 'description' => 'Icon Font', 'enum' => ['font_awesome', 'font_awesome_5', 'icon_mind', 'none']],
        'button_icon_fontawesome' => ['type' => 'string', 'description' => 'Icon Library'],
        'button_icon_fontawesome_5' => ['type' => 'string', 'description' => 'Icon Library'],
        'button_icons_mind' => ['type' => 'string', 'description' => 'Icon Library', 'enum' => ['button_icon_style']],
        'icon_align' => ['type' => 'string', 'description' => 'Icon Position', 'enum' => ['button_icon_style!', 'left', 'right']],
        'button_icon_indent' => ['type' => 'object', 'description' => 'Icon Spacing (Slider/Size Object)'],
        'button_icon_size' => ['type' => 'object', 'description' => 'Icon Size (Slider/Size Object)'],
        'prefix_icon_size' => ['type' => 'object', 'description' => 'Icon Size (Slider/Size Object)'],
        'prefix_icon_color' => ['type' => 'string', 'description' => 'Icon Color (Color Hex/RGBA)'],
        'prefix_icon_adjust' => ['type' => 'object', 'description' => 'Icon Adjust (Slider/Size Object)'],
        'search_placeholder_color' => ['type' => 'string', 'description' => 'Placeholder Color (Color Hex/RGBA)'],
        'search_inner_padding' => ['type' => 'object', 'description' => 'Inner Padding (Dimensions Object)'],
        'input_color' => ['type' => 'string', 'description' => 'Text Color (Color Hex/RGBA)'],
        'input_focus_color' => ['type' => 'string', 'description' => 'Text Color (Color Hex/RGBA)'],
        'box_border' => ['type' => 'string', 'description' => 'Box Border', 'enum' => ['yes', 'no']],
        'border_style' => ['type' => 'string', 'description' => 'Border Style', 'enum' => ['box_border', '{{WRAPPER}} .theplus-post-search-form input.form-control']],
        'box_border_width' => ['type' => 'object', 'description' => 'Border Width (Dimensions Object)'],
        'box_border_color' => ['type' => 'string', 'description' => 'Border Color (Color Hex/RGBA)'],
        'border_radius' => ['type' => 'object', 'description' => 'Border Radius (Dimensions Object)'],
        'box_border_hover_color' => ['type' => 'string', 'description' => 'Border Color (Color Hex/RGBA)'],
        'border_hover_radius' => ['type' => 'object', 'description' => 'Border Radius (Dimensions Object)'],
        'button_inner_padding' => ['type' => 'object', 'description' => 'Inner Padding (Dimensions Object)'],
        'button_margin' => ['type' => 'object', 'description' => 'Margin (Dimensions Object)'],
        'button_color' => ['type' => 'string', 'description' => 'Text Color (Color Hex/RGBA)'],
        'button_hover_color' => ['type' => 'string', 'description' => 'Text Color (Color Hex/RGBA)'],
        'button_box_border' => ['type' => 'string', 'description' => 'Box Border', 'enum' => ['yes', 'no']],
        'button_border_style' => ['type' => 'string', 'description' => 'Border Style', 'enum' => ['button_box_border', '{{WRAPPER}} .theplus-post-search-wrapper button.search-btn-submit']],
        'button_box_border_width' => ['type' => 'object', 'description' => 'Border Width (Dimensions Object)'],
        'button_box_border_color' => ['type' => 'string', 'description' => 'Border Color (Color Hex/RGBA)'],
        'button_border_radius' => ['type' => 'object', 'description' => 'Border Radius (Dimensions Object)'],
        'button_box_border_hover_color' => ['type' => 'string', 'description' => 'Border Color (Color Hex/RGBA)'],
        'button_border_hover_radius' => ['type' => 'object', 'description' => 'Border Radius (Dimensions Object)'],
        'content_max_width' => ['type' => 'object', 'description' => 'Maximum Width (Slider/Size Object)'],
        'settings'       => ['type' => 'object',  'description' => 'Raw Elementor control key/value pairs to merge. Supports predictive AJAX search and custom result templates.']
    ], 'required' => ['post_id', 'parent_id'], 'additionalProperties' => false],
    'output_schema' => ['type' => 'object', 'properties' => ['element_id' => ['type' => 'string'], 'widget_type' => ['type' => 'string'], 'post_id' => ['type' => 'integer']]],
    'execute_callback' => 'tpaep_mcp_theplus_pro_post_search_ability',
    'permission_callback' => 'tpaep_mcp_theplus_pro_post_search_permission',
    'meta' => ['show_in_rest' => true, 'mcp' => ['public' => true, 'type' => 'tool']],
]);
function tpaep_mcp_theplus_pro_post_search_permission(?array $input = null): bool {
    if (!current_user_can('edit_posts')) { return false; }
    $post_id = absint($input['post_id'] ?? 0);
    if ($post_id > 0 && !current_user_can('edit_post', $post_id)) { return false; }
    return true;
}
function tpaep_mcp_theplus_pro_post_search_ability(array $input) {
    if (!tpaep_mcp_has_elementor()) { return new WP_Error('elementor_missing', __('Elementor must be active.', 'theplus')); }
    $widget_type = 'tp-post-search';
    if (!tpaep_mcp_has_registered_widget($widget_type)) { return new WP_Error('widget_missing', __('The Plus Post Search widget is not registered.', 'theplus')); }
    $post_id = absint($input['post_id'] ?? 0);
    $parent_id = sanitize_text_field((string)($input['parent_id'] ?? ''));
    $position = intval($input['position'] ?? -1);
    if ($post_id <= 0 || $parent_id === '') { return new WP_Error('missing_params', __('post_id and parent_id are required.', 'theplus')); }
    $post = get_post($post_id);
    if (!$post instanceof WP_Post) { return new WP_Error('invalid_post', __('Target post was not found.', 'theplus')); }
    $page_data = tpaep_mcp_get_elementor_page_data($post_id);
    if (is_wp_error($page_data)) { return $page_data; }
    $settings = [];
    if (isset($input['deprecated_notice_post_search'])) { $settings['deprecated_notice_post_search'] = sanitize_text_field($input['deprecated_notice_post_search']); }
    if (isset($input['form_style'])) { $settings['form_style'] = sanitize_text_field($input['form_style']); }
    if (isset($input['content_align'])) { $settings['content_align'] = $input['content_align']; }
    if (isset($input['search_field_placeholder'])) { $settings['search_field_placeholder'] = sanitize_text_field($input['search_field_placeholder']); }
    if (isset($input['search_icon'])) { $settings['search_icon'] = sanitize_text_field($input['search_icon']); }
    if (isset($input['search_icon_fontawesome'])) { $settings['search_icon_fontawesome'] = sanitize_text_field($input['search_icon_fontawesome']); }
    if (isset($input['search_icon_fontawesome_5'])) { $settings['search_icon_fontawesome_5'] = sanitize_text_field($input['search_icon_fontawesome_5']); }
    if (isset($input['button_text'])) { $settings['button_text'] = sanitize_text_field($input['button_text']); }
    if (isset($input['button_icon_style'])) { $settings['button_icon_style'] = sanitize_text_field($input['button_icon_style']); }
    if (isset($input['button_icon_fontawesome'])) { $settings['button_icon_fontawesome'] = sanitize_text_field($input['button_icon_fontawesome']); }
    if (isset($input['button_icon_fontawesome_5'])) { $settings['button_icon_fontawesome_5'] = sanitize_text_field($input['button_icon_fontawesome_5']); }
    if (isset($input['button_icons_mind'])) { $settings['button_icons_mind'] = sanitize_text_field($input['button_icons_mind']); }
    if (isset($input['icon_align'])) { $settings['icon_align'] = sanitize_text_field($input['icon_align']); }
    if (isset($input['button_icon_indent'])) { $settings['button_icon_indent'] = $input['button_icon_indent']; }
    if (isset($input['button_icon_size'])) { $settings['button_icon_size'] = $input['button_icon_size']; }
    if (isset($input['prefix_icon_size'])) { $settings['prefix_icon_size'] = $input['prefix_icon_size']; }
    if (isset($input['prefix_icon_color'])) { $settings['prefix_icon_color'] = sanitize_text_field($input['prefix_icon_color']); }
    if (isset($input['prefix_icon_adjust'])) { $settings['prefix_icon_adjust'] = $input['prefix_icon_adjust']; }
    if (isset($input['search_placeholder_color'])) { $settings['search_placeholder_color'] = sanitize_text_field($input['search_placeholder_color']); }
    if (isset($input['search_inner_padding'])) { $settings['search_inner_padding'] = $input['search_inner_padding']; }
    if (isset($input['input_color'])) { $settings['input_color'] = sanitize_text_field($input['input_color']); }
    if (isset($input['input_focus_color'])) { $settings['input_focus_color'] = sanitize_text_field($input['input_focus_color']); }
    if (isset($input['box_border'])) { $settings['box_border'] = sanitize_text_field($input['box_border']); }
    if (isset($input['border_style'])) { $settings['border_style'] = sanitize_text_field($input['border_style']); }
    if (isset($input['box_border_width'])) { $settings['box_border_width'] = $input['box_border_width']; }
    if (isset($input['box_border_color'])) { $settings['box_border_color'] = sanitize_text_field($input['box_border_color']); }
    if (isset($input['border_radius'])) { $settings['border_radius'] = $input['border_radius']; }
    if (isset($input['box_border_hover_color'])) { $settings['box_border_hover_color'] = sanitize_text_field($input['box_border_hover_color']); }
    if (isset($input['border_hover_radius'])) { $settings['border_hover_radius'] = $input['border_hover_radius']; }
    if (isset($input['button_inner_padding'])) { $settings['button_inner_padding'] = $input['button_inner_padding']; }
    if (isset($input['button_margin'])) { $settings['button_margin'] = $input['button_margin']; }
    if (isset($input['button_color'])) { $settings['button_color'] = sanitize_text_field($input['button_color']); }
    if (isset($input['button_hover_color'])) { $settings['button_hover_color'] = sanitize_text_field($input['button_hover_color']); }
    if (isset($input['button_box_border'])) { $settings['button_box_border'] = sanitize_text_field($input['button_box_border']); }
    if (isset($input['button_border_style'])) { $settings['button_border_style'] = sanitize_text_field($input['button_border_style']); }
    if (isset($input['button_box_border_width'])) { $settings['button_box_border_width'] = $input['button_box_border_width']; }
    if (isset($input['button_box_border_color'])) { $settings['button_box_border_color'] = sanitize_text_field($input['button_box_border_color']); }
    if (isset($input['button_border_radius'])) { $settings['button_border_radius'] = $input['button_border_radius']; }
    if (isset($input['button_box_border_hover_color'])) { $settings['button_box_border_hover_color'] = sanitize_text_field($input['button_box_border_hover_color']); }
    if (isset($input['button_border_hover_radius'])) { $settings['button_border_hover_radius'] = $input['button_border_hover_radius']; }
    if (isset($input['content_max_width'])) { $settings['content_max_width'] = $input['content_max_width']; }
    $settings = tpaep_mcp_merge_widget_settings($settings, $input['settings'] ?? []);
    $widget = ['id' => tpaep_mcp_generate_elementor_element_id(), 'elType' => 'widget', 'widgetType' => $widget_type, 'isInner' => false, 'settings' => $settings, 'elements' => []];
    if (!tpaep_mcp_insert_elementor_element($page_data, $parent_id, $widget, $position)) { return new WP_Error('parent_not_found', __('Parent container not found.', 'theplus')); }
    $save_result = tpaep_mcp_save_elementor_page_data($post_id, $page_data);
    if (is_wp_error($save_result)) { return $save_result; }
    return ['element_id' => $widget['id'], 'widget_type' => $widget_type, 'post_id' => $post_id];
}
