<?php
/**
 * File: advanced-emt-payment-gateway/includes/advanced-emt-review.php
 * Dashboard review banner for Advanced EMT (≥5 orders or 14 days; 30-day snooze; no "never").
 */
if (!defined('ABSPATH')) { exit; }

// Allow external redirects to WordPress.org for the review flow
add_filter('allowed_redirect_hosts', function ($hosts) {
    $hosts[] = 'wordpress.org';
    $hosts[] = 'login.wordpress.org';
    $hosts[] = 'profiles.wordpress.org';
    $hosts[] = 'support.wordpress.org';
    return array_unique($hosts);
});

/* Boot: set defaults once */
if (!function_exists('advanced_emt_review_boot')) {
    function advanced_emt_review_boot() {
        if (!get_option('advanced_emt_installed_at')) {
            update_option('advanced_emt_installed_at', time(), false);
        }
        if (get_option('advanced_emt_order_count', null) === null) {
            update_option('advanced_emt_order_count', 0, false);
        }
        if (get_option('advanced_emt_review_done', null) === null) {
            update_option('advanced_emt_review_done', 0, false);
        }
    }
    add_action('plugins_loaded', 'advanced_emt_review_boot');
}

/* Count EMT orders when they hit processing/completed */
if (!function_exists('advanced_emt_review_count_orders')) {
    function advanced_emt_review_count_orders($order_id, $old, $new) {
        if (!in_array($new, ['processing','completed'], true)) return;
        $order = wc_get_order($order_id);
        if (!$order instanceof WC_Order) return;
        if ($order->get_payment_method() !== 'advanced_emt') return;
        update_option('advanced_emt_order_count', (int) get_option('advanced_emt_order_count', 0) + 1, false);
    }
    add_action('woocommerce_order_status_changed', 'advanced_emt_review_count_orders', 10, 3);
}

/* Action handler: server-side redirect to the REVIEWS page (reliable) */
if (!function_exists('advanced_emt_review_handle_action')) {
    function advanced_emt_review_handle_action() {
        if (!current_user_can('manage_woocommerce') && !current_user_can('manage_options')) {
            return;
        }

        // Pull params without touching $_GET (silences PHPCS)
        $action_raw = filter_input(INPUT_GET, 'advanced_emt_review_action', FILTER_DEFAULT);
        if ($action_raw === null) {
            return;
        }
        $action = sanitize_key((string) $action_raw);

        $nonce_raw = filter_input(INPUT_GET, '_wpnonce', FILTER_DEFAULT);
        $nonce     = is_string($nonce_raw) ? sanitize_text_field($nonce_raw) : '';
        if (!wp_verify_nonce($nonce, 'advanced_emt_review_nonce')) {
            return;
        }

        $reviews_url = 'https://wordpress.org/support/plugin/advanced-emt-payment-gateway/reviews/?rate=5#new-post';

        if ($action === 'later') {
            update_option('advanced_emt_review_snooze_until', time() + 30 * DAY_IN_SECONDS, false);
            wp_safe_redirect(remove_query_arg(['advanced_emt_review_action','_wpnonce']));
            exit;
        }

        if ($action === 'now') {
            update_option('advanced_emt_review_done', 1, false);
            wp_safe_redirect($reviews_url); // allowed via allowed_redirect_hosts
            exit;
        }
    }
    add_action('admin_init', 'advanced_emt_review_handle_action');
}



/* Banner: big dashboard-style notice (no "never"; monthly snooze) */
if (!function_exists('advanced_emt_review_admin_notice')) {
    function advanced_emt_review_admin_notice() {
        if (!current_user_can('manage_woocommerce') && !current_user_can('manage_options')) return;

        if ((int) get_option('advanced_emt_review_done', 0) === 1) return;

        $snooze_until = (int) get_option('advanced_emt_review_snooze_until', 0);
        if ($snooze_until && time() < $snooze_until) return;

        $installed_at = (int) get_option('advanced_emt_installed_at', time());
        $days_since   = (time() - $installed_at) / DAY_IN_SECONDS;
        $order_count  = (int) get_option('advanced_emt_order_count', 0);

        // Trigger: 5+ EMT orders OR 14+ days since install
        if ($order_count < 5 && $days_since < 14) return;

        $now_url   = wp_nonce_url(add_query_arg(['advanced_emt_review_action' => 'now']),   'advanced_emt_review_nonce');
        $later_url = wp_nonce_url(add_query_arg(['advanced_emt_review_action' => 'later']), 'advanced_emt_review_nonce');

        echo '<div class="notice notice-success" style="border-left-color:#46b450;">';
        echo '<p style="font-size:14px;margin:8px 0;"><strong>' . esc_html__('Happy with Interac e-Transfer / EMT Gateway for WooCommerce?', 'advanced-emt-payment-gateway') . '</strong></p>';
        echo '<p style="margin:6px 0;">' . esc_html__('A quick rating helps other Canadian stores find it and keeps development moving. ~30 seconds.', 'advanced-emt-payment-gateway') . '</p>';
        echo '<ul style="margin:6px 0 10px 18px;list-style:disc;">';
        echo '<li>' . esc_html__('Works with Classic & Block Checkout', 'advanced-emt-payment-gateway') . '</li>';
        echo '<li>' . esc_html__('HPOS-ready & lightweight', 'advanced-emt-payment-gateway') . '</li>';
        echo '<li>' . esc_html__('No paid upsells — just EMT that works', 'advanced-emt-payment-gateway') . '</li>';
        echo '</ul>';
        echo '<p style="margin:12px 0;">';
        echo '<a class="button button-primary" href="' . esc_url($now_url) . '">' . esc_html__('Leave a review', 'advanced-emt-payment-gateway') . '</a> ';
        echo '<a class="button" href="' . esc_url($later_url) . '">' . esc_html__('Remind me in 30 days', 'advanced-emt-payment-gateway') . '</a>';
        echo '</p>';
        echo '</div>';
    }
    add_action('admin_notices', 'advanced_emt_review_admin_notice');
}
