<?php
namespace RAOGSI_COMPOSER\Admin;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly 
class RGSIUtility {
    public function __construct() {
        add_action( 'plugins_loaded', [$this,'rgsi_check_activation_time'] );
        add_action( 'admin_notices', [ $this, 'rgsi_review_notice'] );
        add_action('wp_ajax_rgsi_dismiss_review_notice', [ $this, 'rgsi_dismiss_review_notice'] );
        add_action('admin_menu', [$this, 'rgsi_add_menu'], 9999 );
        add_action('wp_dashboard_setup', [ $this, 'rgsi_dashboard_widget'] );
        add_action('admin_footer', [ $this, 'custom_admin_js'] );
    }

    public function rgsi_review_notice() {
        // Check if the notice is dismissed and if 7 days have passed since activation.
        $activation_time = get_option('rgsi_activation_time', 0);
        if ( !get_option('rgsi_review_notice_dismissed') && $activation_time && time() > $activation_time + 7 * DAY_IN_SECONDS ) {
            ?>
            <div class="notice notice-success is-dismissible" id="rgsi_review_notice">
                <p>
                    Hey there! 🎉 If you're loving <strong>Gsheets Connector</strong>, please consider leaving us a 
                    <a href="https://wordpress.org/plugins/sheetlink/#reviews" target="_blank">review</a>. 
                    Your feedback keeps the vibes high and helps us get even cooler!
                </p>
            </div>
            <script>
                jQuery(document).on('click', '#rgsi_review_notice .notice-dismiss', function() {
                    jQuery.post(ajaxurl, { action: 'rgsi_dismiss_review_notice' });
                });
            </script>
            <?php
        }
    }

    public function rgsi_dismiss_review_notice() {
        update_option('srgsi_review_notice_dismissed', true);
        wp_die();
    }

    public function rgsi_add_menu() {
        add_submenu_page("raogsi", "Support", "Support", "manage_options", "raogsi-support",  array($this,'rgsi_feature_request_page'));
    }

    public function rgsi_feature_request_page() {
        //wp_safe_redirect("https://docs.google.com/forms/d/e/1FAIpQLSc8ZXNQWnLjNYnJraKUmZP29E1OhA0i7LB4LxHFCxrjLAV9jA/viewform");
    }

    public function rgsi_dashboard_widget() {
        wp_add_dashboard_widget('rgsi_dashboard_widget', 'Gsheets Connector Feedback', [ $this, 'rgsi_dashboard_content'] );
    }

    public function rgsi_dashboard_content() {
        ?>
        <p>🚀 Loving Gsheets Connector? <a href="https://wordpress.org/plugins/sheetlink/#reviews" target="_blank">Leave a review</a> and help us grow!</p>
        <hr>
        <p>💡 Got an idea for a new feature? <a href="https://docs.google.com/forms/d/e/1FAIpQLSc8ZXNQWnLjNYnJraKUmZP29E1OhA0i7LB4LxHFCxrjLAV9jA/viewform" target="_blank">Submit your feature request</a> and shape the future!</p>
        <?php
    }

    public function rgsi_check_activation_time() {
        if ( false === get_option( 'rgsi_activation_time' ) ) {
            update_option( 'rgsi_activation_time', time() );
        }
    }

    public function custom_admin_js() {
        ?>
        <script>
        jQuery(document).ready(function($) {
            $('#toplevel_page_raogsi ul li:nth-child(4) a')
                .attr('href', 'https://docs.google.com/forms/d/e/1FAIpQLSc8ZXNQWnLjNYnJraKUmZP29E1OhA0i7LB4LxHFCxrjLAV9jA/viewform') // Replace with your Google Form link
                .attr('target', '_blank'); // Opens in a new tab
        });
        </script>
        <?php
    }
}