<?php
/**
 * Notice: Review / Rate Us — persists across versions, re-shows after 15 days on dismiss,
 * permanently hidden once the user clicks the review button, and tracks impression count.
 *
 * Key behaviours:
 *  - NOT tied to a specific plugin version (persists across all versions).
 *  - First shown only AFTER the What's New notice for the current version has been dismissed.
 *  - On dismiss → snooze for 15 days, then re-appear.
 *  - On "Leave a Review" click → permanently hidden (never shows again).
 *  - Impression count incremented each time the notice is displayed.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * ─── Constants ───────────────────────────────────────────────────────────────
 */
if ( ! defined( 'MANFIET_REVIEW_META_KEY' ) ) {
    define( 'MANFIET_REVIEW_META_KEY', 'manfiet_review_notice' );
}
if ( ! defined( 'MANFIET_REVIEW_SNOOZE_DAYS' ) ) {
    define( 'MANFIET_REVIEW_SNOOZE_DAYS', 15 );
}

/**
 * ─── Helper: Get review notice state for a user ──────────────────────────────
 */
function manfiet_get_review_notice_state( $user_id ) {
    $default = [
        'reviewed'    => false,
        'snoozed_at'  => 0,
        'impressions' => 0,
    ];

    $state = get_user_meta( (int) $user_id, MANFIET_REVIEW_META_KEY, true );

    if ( ! is_array( $state ) ) {
        return $default;
    }

    return array_merge( $default, $state );
}

/**
 * ─── Helper: Save review notice state for a user ─────────────────────────────
 */
function manfiet_save_review_notice_state( $user_id, array $state ) {
    update_user_meta( (int) $user_id, MANFIET_REVIEW_META_KEY, $state );
}

/**
 * ─── Helper: Check if What's New notice is still active for current user ──────
 */
function manfiet_is_whats_new_active( $user_id ) {
    $dismissed_meta = get_user_meta( (int) $user_id, 'manfiet_dismissed_notices', true );
    if ( ! is_array( $dismissed_meta ) ) {
        return true; // Not dismissed yet.
    }
    $runtime_key = 'whats-new@' . MANFIET_EXPENSE_TRACKER_VERSION;
    return ! isset( $dismissed_meta[ $runtime_key ] );
}

/**
 * ─── Helper: Decide whether to render the review notice for current user ──────
 */
function manfiet_should_show_review_notice( $user_id ) {
    // Never show while What's New is still active.
    if ( manfiet_is_whats_new_active( $user_id ) ) {
        return false;
    }

    $state = manfiet_get_review_notice_state( $user_id );

    // Permanently hidden once reviewed.
    if ( ! empty( $state['reviewed'] ) ) {
        return false;
    }

    // Not yet snoozed → show immediately.
    if ( empty( $state['snoozed_at'] ) ) {
        return true;
    }

    // Snoozed → only re-show after the configured number of days.
    $snooze_seconds = MANFIET_REVIEW_SNOOZE_DAYS * DAY_IN_SECONDS;
    return ( time() - (int) $state['snoozed_at'] ) >= $snooze_seconds;
}

/**
 * ─── Render ──────────────────────────────────────────────────────────────────
 */
add_action(
    'admin_notices',
    function () {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        $user_id = get_current_user_id();

        if ( ! $user_id || ! manfiet_should_show_review_notice( $user_id ) ) {
            return;
        }

        // Increment impression counter.
        $state               = manfiet_get_review_notice_state( $user_id );
        $state['impressions'] = (int) $state['impressions'] + 1;
        manfiet_save_review_notice_state( $user_id, $state );

        $review_url   = 'https://wordpress.org/support/plugin/manfi-expense-tracker/reviews/#new-post';
        $impressions  = (int) $state['impressions'];

        // Build nonces.
        $snooze_nonce  = wp_create_nonce( 'manfiet_review_snooze' );
        $reviewed_nonce = wp_create_nonce( 'manfiet_review_done' );
        $ajax_url      = admin_url( 'admin-ajax.php' );

        // Server-side snooze fallback URL.
        $snooze_url = wp_nonce_url(
            $ajax_url . '?action=manfiet_review_snooze',
            'manfiet_review_snooze'
        );
        ?>
        <div id="manfiet-review-notice"
             class="notice manfiet-review-notice"
             role="alert"
             data-impressions="<?php echo esc_attr( $impressions ); ?>"
             style="border: none; padding: 0; background: transparent; box-shadow: none; margin: 5px 0 15px;">

            <div style="
                display: flex;
                align-items: stretch;
                background: linear-gradient(135deg, #02081a 0%, #1e1b4b 40%, #1e3a8a 100%);
                border-radius: 10px;
                overflow: hidden;
                box-shadow: 0 4px 24px rgba(37,99,235,0.25), 0 1px 4px rgba(0,0,0,0.15);
                max-width: 780px;
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            ">
                <!-- Accent bar -->
                <div style="width: 5px; flex-shrink: 0; background: linear-gradient(180deg, #93c5fd, #3b82f6, #1d4ed8);"></div>

                <!-- Star column -->
                <div style="
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    padding: 20px 18px;
                    background: rgba(255,255,255,0.05);
                    flex-shrink: 0;
                ">
                    <span style="font-size: 34px; line-height: 1; filter: drop-shadow(0 2px 6px rgba(250,204,21,0.6));" aria-hidden="true">⭐</span>
                </div>

                <!-- Main content -->
                <div style="padding: 18px 16px 18px 4px; flex: 1; min-width: 0;">
                    <p style="margin: 0 0 4px; font-size: 15px; font-weight: 700; color: #f8fafc; line-height: 1.3;">
                        <?php esc_html_e( 'Loving Manfi Expense Tracker?', 'manfi-expense-tracker' ); ?>
                    </p>
                    <p style="margin: 0 0 14px; font-size: 13px; color: #93c5fd; line-height: 1.55;">
                        <?php esc_html_e( 'You\'ve been using the plugin for a while — a quick review on WordPress.org takes less than a minute and helps thousands of other users discover it. It means the world to us! 🙏', 'manfi-expense-tracker' ); ?>
                    </p>

                    <!-- Action row -->
                    <div style="display: flex; flex-wrap: wrap; gap: 10px; align-items: center;">

                        <!-- Primary CTA: Leave a Review -->
                        <a id="manfiet-review-btn"
                           href="<?php echo esc_url( $review_url ); ?>"
                           target="_blank"
                           rel="noopener noreferrer"
                           data-nonce="<?php echo esc_attr( $reviewed_nonce ); ?>"
                           data-ajax="<?php echo esc_url( $ajax_url ); ?>"
                           style="
                               display: inline-flex;
                               align-items: center;
                               gap: 7px;
                               padding: 9px 20px;
                               background: linear-gradient(135deg, #2563eb, #1d4ed8);
                               color: #fff;
                               font-size: 13px;
                               font-weight: 700;
                               text-decoration: none;
                               border-radius: 6px;
                               box-shadow: 0 2px 8px rgba(37,99,235,0.4);
                               transition: filter .2s;
                               letter-spacing: .3px;
                           "
                           onmouseover="this.style.filter='brightness(1.12)'"
                           onmouseout="this.style.filter='brightness(1)'">
                            <span aria-hidden="true" style="font-size:15px;">🌟</span>
                            <?php esc_html_e( 'Leave a Review', 'manfi-expense-tracker' ); ?>
                        </a>

                        <!-- Secondary: Maybe Later (snooze) -->
                        <button id="manfiet-review-snooze"
                                type="button"
                                data-nonce="<?php echo esc_attr( $snooze_nonce ); ?>"
                                data-ajax="<?php echo esc_url( $ajax_url ); ?>"
                                style="
                                    background: rgba(255,255,255,0.08);
                                    border: 1px solid rgba(255,255,255,0.18);
                                    color: #bfdbfe;
                                    font-size: 12px;
                                    padding: 8px 16px;
                                    border-radius: 6px;
                                    cursor: pointer;
                                    transition: background .2s;
                                    font-family: inherit;
                                "
                                onmouseover="this.style.background='rgba(255,255,255,0.14)'"
                                onmouseout="this.style.background='rgba(255,255,255,0.08)'">
                            <?php
                            printf(
                                /* translators: %d: number of days */
                                esc_html__( 'Maybe Later (remind me in %d days)', 'manfi-expense-tracker' ),
                                absint( MANFIET_REVIEW_SNOOZE_DAYS )
                            );
                            ?>
                        </button>
                    </div>
                </div><!-- /.main content -->

                <!-- Dismiss (×) -->
                <div style="padding: 14px 14px 0 4px; flex-shrink: 0;">
                    <button id="manfiet-review-close"
                            type="button"
                            data-nonce="<?php echo esc_attr( $snooze_nonce ); ?>"
                            data-ajax="<?php echo esc_url( $ajax_url ); ?>"
                            aria-label="<?php esc_attr_e( 'Dismiss notice', 'manfi-expense-tracker' ); ?>"
                            style="
                                background: none;
                                border: none;
                                color: rgba(191,219,254,0.6);
                                font-size: 20px;
                                cursor: pointer;
                                line-height: 1;
                                padding: 2px 4px;
                                border-radius: 4px;
                                transition: color .2s;
                                font-family: inherit;
                            "
                            onmouseover="this.style.color='#f8fafc'"
                            onmouseout="this.style.color='rgba(191,219,254,0.6)'">
                        &times;
                    </button>
                </div>
            </div><!-- /.card -->
        </div>
        <?php
    },
    30 // Renders after What's New.
);

/**
 * ─── Scripts ─────────────────────────────────────────────────────────────────
 */
add_action(
    'admin_enqueue_scripts',
    function () {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        $user_id = get_current_user_id();
        if ( ! $user_id || ! manfiet_should_show_review_notice( $user_id ) ) {
            return;
        }

        $script = <<<'JS'
(function(){
    // ── Snooze (dismiss / maybe later / × close) ──────────────────────────
    function manfietSnoozeReview(btn) {
        var ajaxUrl = btn.dataset.ajax;
        var nonce   = btn.dataset.nonce;
        if (!ajaxUrl || !nonce) return;

        var notice = document.getElementById('manfiet-review-notice');
        if (notice) {
            notice.style.transition = 'opacity .35s';
            notice.style.opacity    = '0';
            setTimeout(function(){ notice.style.display = 'none'; }, 380);
        }

        fetch(ajaxUrl, {
            method:  'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body:    'action=manfiet_review_snooze&_ajax_nonce=' + encodeURIComponent(nonce)
        });
    }

    // ── Mark as reviewed (primary CTA click) ──────────────────────────────
    function manfietMarkReviewed(btn) {
        var ajaxUrl = btn.dataset.ajax;
        var nonce   = btn.dataset.nonce;
        if (!ajaxUrl || !nonce) return;

        fetch(ajaxUrl, {
            method:  'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body:    'action=manfiet_review_done&_ajax_nonce=' + encodeURIComponent(nonce)
        }).then(function(){
            var notice = document.getElementById('manfiet-review-notice');
            if (notice) {
                notice.style.transition = 'opacity .35s';
                notice.style.opacity    = '0';
                setTimeout(function(){ notice.style.display = 'none'; }, 380);
            }
        });
    }

    document.addEventListener('DOMContentLoaded', function(){
        // Snooze button
        var snoozeBtn = document.getElementById('manfiet-review-snooze');
        if (snoozeBtn) {
            snoozeBtn.addEventListener('click', function(){ manfietSnoozeReview(this); });
        }

        // × close button
        var closeBtn = document.getElementById('manfiet-review-close');
        if (closeBtn) {
            closeBtn.addEventListener('click', function(){ manfietSnoozeReview(this); });
        }

        // Review CTA — still open the link but also call the handler.
        var reviewBtn = document.getElementById('manfiet-review-btn');
        if (reviewBtn) {
            reviewBtn.addEventListener('click', function(){ manfietMarkReviewed(this); });
        }
    });
})();
JS;

        wp_register_script( 'manfiet-review-notice', false, [], MANFIET_EXPENSE_TRACKER_VERSION, true );
        wp_enqueue_script( 'manfiet-review-notice' );
        wp_add_inline_script( 'manfiet-review-notice', $script );
    }
);

/**
 * ─── AJAX: Snooze ────────────────────────────────────────────────────────────
 */
add_action(
    'wp_ajax_manfiet_review_snooze',
    function () {
        check_ajax_referer( 'manfiet_review_snooze' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( [ 'message' => 'Forbidden' ], 403 );
        }

        $user_id = get_current_user_id();
        if ( ! $user_id ) {
            wp_send_json_error( [ 'message' => 'Invalid user' ], 400 );
        }

        $state               = manfiet_get_review_notice_state( $user_id );
        $state['snoozed_at'] = time();
        manfiet_save_review_notice_state( $user_id, $state );

        wp_send_json_success( [ 'snoozed_at' => $state['snoozed_at'] ] );
    }
);

/**
 * ─── AJAX: Mark as reviewed (permanently hide) ───────────────────────────────
 */
add_action(
    'wp_ajax_manfiet_review_done',
    function () {
        check_ajax_referer( 'manfiet_review_done' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( [ 'message' => 'Forbidden' ], 403 );
        }

        $user_id = get_current_user_id();
        if ( ! $user_id ) {
            wp_send_json_error( [ 'message' => 'Invalid user' ], 400 );
        }

        $state             = manfiet_get_review_notice_state( $user_id );
        $state['reviewed'] = true;
        manfiet_save_review_notice_state( $user_id, $state );

        wp_send_json_success();
    }
);

// Renders hooks directly.
return [];
