<?php 

namespace WPVOFW\Classes;

class ReviewReminder {
    private $reminder_interval;
    private $option_prefix;
    private $display_condition_callback;
    private $notice_message;

    public function __construct( $args ) {
        $defaults = array(
            'reminder_interval'         => 30,
            'option_prefix'             => 'plugin_review_',
            'display_condition_callback'=> null,
            'notice_message'            => '',
        );

        $args = wp_parse_args( $args, $defaults );

        $this->reminder_interval        = $args['reminder_interval'];
        $this->option_prefix            = $args['option_prefix'];
        $this->display_condition_callback = $args['display_condition_callback'];
        $this->notice_message           = $args['notice_message'];

        add_action( 'admin_notices', array( $this, 'display_review_notice' ) );
        add_action( 'admin_post_remind_later', array( $this, 'handle_remind_later' ) );
        add_action( 'admin_post_mark_reviewed', array( $this, 'handle_mark_reviewed' ) );
    }

    public function display_review_notice() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        // Check if the custom display condition callback is defined and callable
        if ( is_callable( $this->display_condition_callback ) && ! call_user_func( $this->display_condition_callback ) ) {
            return;
        }

        $last_reminder_time = get_option( $this->option_prefix . 'last_reminder_time' );

        // Check if it's time to display the reminder
        if ( ! $last_reminder_time || time() - $last_reminder_time > $this->reminder_interval * 24 * 3600 ) {
            ?>
            <div class="notice notice-info is-dismissible">
                <p><?php echo  $this->notice_message; ?></p>
                <p>
                    <a href="<?php echo esc_url( admin_url( 'admin-post.php?action=remind_later&_wpnonce=' . wp_create_nonce( 'remind_later' ) ) ); ?>"><?php _e( 'Maybe Later', 'text-domain' ); ?></a> | 
                    <a href="<?php echo esc_url( admin_url( 'admin-post.php?action=mark_reviewed&_wpnonce=' . wp_create_nonce( 'mark_reviewed' ) ) ); ?>"><?php _e( 'Already Done', 'text-domain' ); ?></a>
                </p>
            </div>
            <?php
        }
    }

    public function handle_remind_later() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        check_admin_referer( 'remind_later' );

        update_option( $this->option_prefix . 'last_reminder_time', time() );

        wp_safe_redirect( wp_get_referer() );
        exit;
    }

    public function handle_mark_reviewed() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        check_admin_referer( 'mark_reviewed' );

        // Set last reminder time to a future date (e.g., 10 years from now)
        update_option( $this->option_prefix . 'last_reminder_time', time() + 10 * 365 * 24 * 3600 );

        wp_safe_redirect( wp_get_referer() );
        exit;
    }
}
