<?php
/**
 * Coupon Description AI REST Controller.
 *
 * @since   1.1.4
 * @package SAAI\REST
 */

namespace SAAI\REST;

use SAAI\Abstracts\Abstract_AI_Agent_REST;
use SAAI\AI_Agents\Coupon_Description_AI as AI_Agent;
use SAAI\Traits\Singleton_Trait;
use WP_Error;
use WP_REST_Response;
use WP_REST_Server;

defined( 'ABSPATH' ) || exit;

/**
 * Coupon_Description_AI REST class.
 *
 * @since 1.1.4
 */
class Coupon_Description_AI extends Abstract_AI_Agent_REST {

    use Singleton_Trait;

    /**
     * Holds the class instance object.
     *
     * @since 1.1.4
     * @var Coupon_Description_AI $instance object
     */
    protected static $instance;

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

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

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

        return $sanitized;
    }

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

        parent::register_routes();

        register_rest_route(
            $this->namespace,
            "/$this->rest_base/(?P<coupon_id>[\d]+)",
            array(
                'methods'             => WP_REST_Server::CREATABLE,
                'callback'            => array( $this, 'generate_coupon_description' ),
                'permission_callback' => array( $this, 'create_item_permissions_check' ),
            )
        );

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

    /**
     * Generate coupon description.
     *
     * @since 1.1.4
     *
     * @param \WP_REST_Request $request Request object.
     * @return WP_REST_Response|WP_Error Response object.
     */
    public function generate_coupon_description( $request ) {
        $coupon_id = absint( $request['coupon_id'] );

        if ( ! $coupon_id || 'shop_coupon' !== get_post_type( $coupon_id ) ) {
            return new WP_Error( 'invalid_coupon_id', __( 'Invalid coupon ID.', 'storeagent-ai-for-woocommerce' ), array( 'status' => 400 ) );
        }

        $result = AI_Agent::instance()->run_agent_query( $coupon_id );

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

        $description = is_array( $result ) && isset( $result['description'] ) ? $result['description'] : $result;

        return $this->rest_response( $this->with_usage_data( array( 'description' => $description ) ), $request );
    }

    /**
     * Get the Coupon Description AI settings.
     *
     * @since 1.1.4
     *
     * @param \WP_REST_Request $request Request object.
     * @return WP_REST_Response Response object.
     */
    public function get_settings( $request ) {
        $ai_agent_key = 'saai_' . str_replace( '-', '_', $this->rest_base );
        $settings     = get_option( $ai_agent_key, array() );
        $settings     = wp_parse_args( $settings, AI_Agent::instance()->default_settings );

        $response = array(
            'settings' => $settings,
        );

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