<?php
/**
 * Periodic "please review us" admin notice — re-shown every 14 days until the
 * admin rates the plugin or dismisses it for good.
 *
 * @package Brainwerk SEO Suite
 */

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

class Seoforge_Review_Notice {

    const OPTION  = 'seoforge_review_notice';
    const INTERVAL = 14 * DAY_IN_SECONDS;
    const REVIEW_URL = 'https://wordpress.org/support/plugin/brainwerk-seo-suite/reviews/?rate=5#new-post';

    public static function register( Seoforge_Loader $loader ) {
        $loader->add_action( 'admin_notices', __CLASS__, 'maybe_notice' );
        $loader->add_action( 'network_admin_notices', __CLASS__, 'maybe_notice' );
        $loader->add_action( 'admin_post_seoforge_review_remind_later', __CLASS__, 'handle_remind_later' );
        $loader->add_action( 'admin_post_seoforge_review_dismiss', __CLASS__, 'handle_dismiss' );
    }

    /** Called once from the activator on a fresh install. */
    public static function schedule_first_prompt() {
        $state = self::state();
        if ( ! empty( $state ) ) {
            return; // Already tracking (e.g. reactivation) — leave it as-is.
        }
        update_site_option( self::OPTION, array(
            'next'  => time() + self::INTERVAL,
            'never' => false,
        ) );
    }

    private static function state() {
        $state = get_site_option( self::OPTION, array() );
        return is_array( $state ) ? $state : array();
    }

    private static function set_state( array $changes ) {
        update_site_option( self::OPTION, array_merge( self::state(), $changes ) );
    }

    public static function maybe_notice() {
        if ( ! current_user_can( 'manage_options' ) && ! current_user_can( 'manage_network_options' ) ) {
            return;
        }
        $state = self::state();
        if ( ! empty( $state['never'] ) ) {
            return;
        }
        $next = isset( $state['next'] ) ? (int) $state['next'] : 0;
        if ( ! $next || time() < $next ) {
            return;
        }

        $remind_url  = self::action_url( 'seoforge_review_remind_later' );
        $dismiss_url = self::action_url( 'seoforge_review_dismiss' );

        echo '<div class="notice notice-info is-dismissible seoforge-review-notice"><p>';
        echo esc_html__( 'Enjoying Brainwerk SEO Suite? A quick 5-star review helps other site owners find the plugin.', 'brainwerk-seo-suite' );
        echo '</p><p>';
        echo '<a class="button button-primary" href="' . esc_url( self::REVIEW_URL ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Leave a review', 'brainwerk-seo-suite' ) . '</a> ';
        echo '<a class="button" href="' . esc_url( $remind_url ) . '">' . esc_html__( 'Remind me in 14 days', 'brainwerk-seo-suite' ) . '</a> ';
        echo '<a class="button-link" href="' . esc_url( $dismiss_url ) . '" style="margin-left:8px;">' . esc_html__( "I've already reviewed it / don't ask again", 'brainwerk-seo-suite' ) . '</a>';
        echo '</p></div>';
    }

    private static function action_url( $action ) {
        return wp_nonce_url( admin_url( 'admin-post.php?action=' . $action ), $action );
    }

    public static function handle_remind_later() {
        if ( ! current_user_can( 'manage_options' ) && ! current_user_can( 'manage_network_options' ) ) {
            wp_die( '403', 403 );
        }
        check_admin_referer( 'seoforge_review_remind_later' );
        self::set_state( array( 'next' => time() + self::INTERVAL ) );
        wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url() );
        exit;
    }

    public static function handle_dismiss() {
        if ( ! current_user_can( 'manage_options' ) && ! current_user_can( 'manage_network_options' ) ) {
            wp_die( '403', 403 );
        }
        check_admin_referer( 'seoforge_review_dismiss' );
        self::set_state( array( 'never' => true ) );
        wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url() );
        exit;
    }
}
