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

namespace SAAI\REST;

use SAAI\Abstracts\Abstract_AI_Agent_REST;
use SAAI\AI_Agents\Product_Tags_Generator_AI as ProductTagsGeneratorAIAgent;
use SAAI\Traits\Singleton_Trait;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;

/**
 * Product_Tags_Generator_AI class.
 *
 * @since 1.0.0
 */
class Product_Tags_Generator_AI extends Abstract_AI_Agent_REST {

    use Singleton_Trait;

    /**
     * Holds the class instance object
     *
     * @since 1.0.0
     * @var Product_Tags_Generator_AI $instance object
     */
    protected static $instance;

    /**
     * Register the routes.
     *
     * @since 1.0.0
     * @return void
     */
    public function register_routes() {
        parent::register_routes();

        register_rest_route(
            $this->namespace,
            "/$this->rest_base/generate",
            array(
                'methods'             => WP_REST_Server::CREATABLE,
                'callback'            => array( $this, 'generate_product_tags' ),
                'permission_callback' => array( $this, 'create_item_permissions_check' ),
            )
        );
    }

    /**
     * Sanitize settings data before saving.
     *
     * @param array $params The parameters to sanitize.
     * @return array The sanitized parameters.
     */
    protected function sanitize_settings_data( $params ) {
        $sanitized = array();

        // Sanitize enabled setting.
        if ( isset( $params['enabled'] ) ) {
            $sanitized['enabled'] = sanitize_text_field( $params['enabled'] );
        }

        if ( isset( $params['maxTags'] ) ) {
            $sanitized['maxTags'] = absint( $params['maxTags'] );
        }

        if ( isset( $params['includeMeta'] ) ) {
            $sanitized['includeMeta'] = sanitize_text_field( $params['includeMeta'] );
        }

        if ( isset( $params['includeCategories'] ) ) {
            $sanitized['includeCategories'] = sanitize_text_field( $params['includeCategories'] );
        }

        if ( isset( $params['includeAttributes'] ) ) {
            $sanitized['includeAttributes'] = sanitize_text_field( $params['includeAttributes'] );
        }

        // Sanitize language (text).
        if ( isset( $params['language'] ) ) {
            $sanitized['language'] = sanitize_text_field( $params['language'] );
        }

        // Apply filter for additional sanitization.
        return apply_filters( 'saai_sanitize_product_tags_generator_settings', $sanitized, $params );
    }

    /**
     * Generate product tags via the REST API.
     *
     * @param WP_REST_Request $request The request object.
     *
     * @since 1.0.0
     * @return WP_REST_Response|WP_Error
     */
    public function generate_product_tags( $request ) {
        $product_id = $request->get_param( 'product_id' );
        $product    = wc_get_product( $product_id );

        if ( ! $product instanceof \WC_Product ) {
            return new WP_Error(
                'invalid_product',
                __( 'Invalid product ID.', 'storeagent-ai-for-woocommerce' ),
                array( 'status' => 400 )
            );
        }

        $product_tags_generator_ai = ProductTagsGeneratorAIAgent::instance();
        $result                    = $product_tags_generator_ai->generate_product_tags( $product_id );

        if ( is_wp_error( $result ) ) {
            return $result;
        }

        return rest_ensure_response(
            $this->with_usage_data(
                array(
                    'success' => true,
                    'tags'    => $result,
                )
            )
        );
    }
}
