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

namespace SAAI\REST;

use SAAI\Abstracts\Abstract_AI_Agent_REST;
use SAAI\Traits\Singleton_Trait;

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

    use Singleton_Trait;

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

    /**
     * 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'] );
        }

        // Sanitize minReviews (integer).
        if ( isset( $params['minReviews'] ) ) {
            $sanitized['minReviews'] = absint( $params['minReviews'] );
        }

        // Sanitize updateFrequency (option).
        if ( isset( $params['updateFrequency'] ) ) {
            $allowed_values               = array( 'on-review', 'daily', 'weekly' );
            $sanitized['updateFrequency'] = in_array( $params['updateFrequency'], $allowed_values, true )
                ? $params['updateFrequency']
                : 'on-review';
        }

        // Sanitize showDisclaimer (yes/no).
        if ( isset( $params['showDisclaimer'] ) ) {
            $sanitized['showDisclaimer'] = sanitize_text_field( $params['showDisclaimer'] );
        }

        // Sanitize disclaimerText (HTML).
        if ( isset( $params['disclaimerText'] ) ) {
            $sanitized['disclaimerText'] = wp_kses_post( $params['disclaimerText'] );
        }

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

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

    /**
     * Save response.
     *
     * @param array $response The response.
     * @param array $settings The current settings.
     *
     * @return mixed
     */
    public function save_response( $response, $settings ) {

        /***************************************************************************
         * Schedule an action to generate review summaries.
         ***************************************************************************
         *
         * We only schedule the action if the AI agent is enabled and the action
         * is not already scheduled or pending.
         */
        if ( 'yes' === $settings['enabled'] ) {
            if ( 'on-review' !== $settings['updateFrequency'] &&
                ! as_next_scheduled_action( \SAAI\AI_Agents\Review_Summaries_AI::GENERATE_REVIEW_SUMMARIES_HOOK, array(), 'saai' ) ) {
                $interval = DAY_IN_SECONDS;
                if ( 'weekly' === $settings['updateFrequency'] ) {
                    $interval = WEEK_IN_SECONDS;
                }

                as_schedule_recurring_action( time(), $interval, \SAAI\AI_Agents\Review_Summaries_AI::GENERATE_REVIEW_SUMMARIES_HOOK, array(), 'saai', true );
            } elseif ( ! as_next_scheduled_action( \SAAI\AI_Agents\Review_Summaries_AI::GENERATE_REVIEW_SUMMARIES_HOOK, array(), 'saai' ) ) {
                as_enqueue_async_action( \SAAI\AI_Agents\Review_Summaries_AI::GENERATE_REVIEW_SUMMARIES_HOOK, array(), 'saai', true );
            }
        } else {
            as_unschedule_action( \SAAI\AI_Agents\Review_Summaries_AI::GENERATE_REVIEW_SUMMARIES_HOOK, array(), 'saai' );
        }

        return $response;
    }

    /**
     * Run the class REST handlers.
     *
     * @return void
     */
    public function run() {

        parent::run();

        add_filter( "saai_rest_{$this->rest_base}_settings_save_response", array( $this, 'save_response' ), 10, 2 );
    }
}
