<?php
/**
 * Author: Rymera Web Co.
 *
 * @package SAAI\Abstracts
 */

namespace SAAI\Abstracts;

use SAAI\Classes\Plan_Manager;
use SAAI\Traits\Magic_Get_Trait;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;

/**
 * Abstract_AI_Agent_REST class.
 *
 * @since 1.0.0
 */
abstract class Abstract_AI_Agent_REST extends Abstract_REST {

    use Magic_Get_Trait;

    /**
     * Register the common routes.
     *
     * @return void
     */
    public function register_routes() {

        register_rest_route(
            $this->namespace,
            "/$this->rest_base/settings/save",
            array(
                'methods'             => WP_REST_Server::EDITABLE,
                'callback'            => array( $this, 'save_settings' ),
                'permission_callback' => array( $this, 'create_item_permissions_check' ),
            )
        );
    }

    /**
     * Get the option key for storing AI agent settings.
     *
     * @since 1.1.4
     * @return string The option key.
     */
    protected function get_option_key() {
        return 'saai_' . str_replace( '-', '_', $this->rest_base );
    }

    /**
     * Sanitize settings data before saving.
     *
     * Child classes should override this method to provide
     * specific sanitization for each agent's settings fields.
     *
     * @param array $params The parameters to sanitize.
     * @return array The sanitized parameters.
     */
    abstract protected function sanitize_settings_data( $params );

    /**
     * Save AI Agent settings.
     *
     * @param WP_REST_Request $request The request object.
     *
     * @return WP_REST_Response
     */
    public function save_settings( $request ) {

        $params = $request->get_params();

        if ( empty( $params ) ) {
            return new WP_Error( 'rest_invalid_params', __( 'Invalid parameters.', 'storeagent-ai-for-woocommerce' ), array( 'status' => 400 ) );
        }

        // Sanitize settings data.
        $params = $this->sanitize_settings_data( $params );

        $ai_agent_key = $this->get_option_key();
        $old_settings = (array) get_option( $ai_agent_key, array() );
        $settings     = wp_parse_args( $params, $old_settings );
        $settings     = apply_filters( "saai_before_rest_{$this->rest_base}_settings_save", $settings, $params, $old_settings, $request );

        update_option( $ai_agent_key, $settings, false );

        $response = array( 'message' => __( 'Agent settings saved!', 'storeagent-ai-for-woocommerce' ) );

        $response = apply_filters( "saai_rest_{$this->rest_base}_settings_save_response", $response, $settings, $old_settings, $request );

        return $this->rest_response( $response, $request );
    }

    /**
     * Append credit usage data to a response array.
     *
     * Increments the local AI actions usage count and merges
     * the updated usage data into the provided response array.
     *
     * @since 1.3.0
     *
     * @param array $response_data The response data to augment.
     * @return array The response data with usage key added.
     */
    protected function with_usage_data( $response_data ) {
        return Plan_Manager::instance()->append_usage_data( $response_data );
    }
}
