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

namespace SAAI\Classes;

use SAAI\Actions\Activation;
use SAAI\Traits\Singleton_Trait;

defined( 'ABSPATH' ) || exit;

/**
 * Review_Request class.
 *
 * Schedules and creates a dashboard notification encouraging users
 * to leave a review on WordPress.org after 14 days of plugin usage.
 *
 * @since 1.2.0
 */
class Review_Request {

    use Singleton_Trait;

    /**
     * Holds the class instance object
     *
     * @var Review_Request $instance
     * @since 1.2.0
     */
    protected static $instance;

    /**
     * Option flag indicating the review request notification has been created.
     */
    const REVIEW_REQUEST_CREATED_OPTION = 'saai_review_request_notification_created';

    /**
     * Action Scheduler hook for creating the review request notification.
     */
    const REVIEW_REQUEST_HOOK = 'saai_create_review_request_notification';

    /**
     * WordPress.org review URL.
     */
    const REVIEW_URL = 'https://wordpress.org/support/plugin/storeagent-ai-for-woocommerce/reviews/#new-post';

    /**
     * Number of days after install before showing the review request.
     */
    const DAYS_BEFORE_REVIEW_REQUEST = 14;

    /**
     * Run the class functionality.
     *
     * @since 1.2.0
     * @return void
     */
    public function run() {
        add_action( self::REVIEW_REQUEST_HOOK, array( $this, 'create_review_request_notification' ) );
    }

    /**
     * Schedule the review request notification via Action Scheduler.
     *
     * Called from Activation::schedule_initial_tasks() during plugin
     * activation or version upgrade.
     *
     * @since 1.2.0
     * @return void
     */
    public function schedule_review_request() {
        // Already created — never reschedule.
        if ( get_option( self::REVIEW_REQUEST_CREATED_OPTION ) ) {
            return;
        }

        // Bail if Action Scheduler is not yet available (e.g., during early activation).
        if ( ! function_exists( 'as_has_scheduled_action' ) ) {
            return;
        }

        // Already scheduled — don't duplicate.
        if ( as_has_scheduled_action( self::REVIEW_REQUEST_HOOK ) ) {
            return;
        }

        $install_timestamp = get_option( Activation::PLUGIN_INSTALLED_TIMESTAMP );
        if ( empty( $install_timestamp ) ) {
            return;
        }

        $review_time = (int) $install_timestamp + ( self::DAYS_BEFORE_REVIEW_REQUEST * DAY_IN_SECONDS );

        if ( time() >= $review_time ) {
            // Install is already past 14 days — fire ASAP.
            as_enqueue_async_action( self::REVIEW_REQUEST_HOOK, array(), 'storeagent' );
        } else {
            // Schedule for the future.
            as_schedule_single_action( $review_time, self::REVIEW_REQUEST_HOOK, array(), 'storeagent' );
        }
    }

    /**
     * Action Scheduler callback — creates the review request notification.
     *
     * @since 1.2.0
     * @return void
     */
    public function create_review_request_notification() {
        // Double-check flag in case of race conditions.
        if ( get_option( self::REVIEW_REQUEST_CREATED_OPTION ) ) {
            return;
        }

        $actions = array(
            array(
                'label'  => __( 'Leave a Review', 'storeagent-ai-for-woocommerce' ),
                'action' => 'leave_review',
                'url'    => self::REVIEW_URL,
                'target' => '_blank',
            ),
        );

        $result = Notification::instance()->create_info_notification(
            /* translators: This is the title of a notification asking users to leave a review */
            __( 'Enjoying StoreAgent?', 'storeagent-ai-for-woocommerce' ),
            /* translators: This is the message body of a notification asking users to leave a review on WordPress.org */
            __( "If you are happy with our plugin, please leave us a review on WordPress.org. You'll be helping others plus giving us motivation to keep building the plugin. Thank you!", 'storeagent-ai-for-woocommerce' ),
            'StoreAgent',
            null,
            array(),
            $actions
        );

        if ( ! is_wp_error( $result ) ) {
            update_option( self::REVIEW_REQUEST_CREATED_OPTION, true, 'no' );
        }
    }
}
