<?php
/**
 * Mature-install feedback prompt for 4.5.4.
 *
 * The prompt is deliberately delayed, non-incentivized, dismissible, and
 * never shown before the store has meaningful completed-booking history.
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class YATOON_Release_Polish {
    const FIRST_SEEN_OPTION = 'yatoon_first_seen_at';
    const REVIEW_STATE_OPTION = 'yatoon_review_prompt_state';

    public static function init() {
        if ( ! get_option( self::FIRST_SEEN_OPTION, 0 ) ) {
            add_option( self::FIRST_SEEN_OPTION, time(), '', false );
        }
        add_action( 'admin_notices', array( __CLASS__, 'review_notice' ) );
        add_action( 'admin_post_yatoon_review_prompt', array( __CLASS__, 'handle_review_prompt' ) );
    }

    public static function review_notice() {
        if ( ! current_user_can( 'manage_options' ) || ! self::is_yatoon_screen() || ! self::is_eligible() ) {
            return;
        }
        $later_url = wp_nonce_url( admin_url( 'admin-post.php?action=yatoon_review_prompt&choice=later' ), 'yatoon_review_prompt' );
        $never_url = wp_nonce_url( admin_url( 'admin-post.php?action=yatoon_review_prompt&choice=never' ), 'yatoon_review_prompt' );
        ?>
        <div class="notice notice-info yatoon-honest-review-notice"><p><strong><?php esc_html_e( 'Has Yatoon helped your business?', 'yatoon-booking-system' ); ?></strong></p><p><?php esc_html_e( 'If you have used it long enough to judge it fairly, an honest WordPress.org review helps other appointment businesses. There is no reward or incentive, and constructive feedback is equally welcome.', 'yatoon-booking-system' ); ?></p><p><a class="button button-primary" href="https://wordpress.org/support/plugin/yatoon-booking-system/reviews/#new-post" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Leave an honest review', 'yatoon-booking-system' ); ?></a> <a class="button" href="<?php echo esc_url( $later_url ); ?>"><?php esc_html_e( 'Ask again in 90 days', 'yatoon-booking-system' ); ?></a> <a href="<?php echo esc_url( $never_url ); ?>"><?php esc_html_e( 'Do not ask again', 'yatoon-booking-system' ); ?></a></p></div>
        <?php
    }

    private static function is_eligible() {
        $state = (array) get_option( self::REVIEW_STATE_OPTION, array() );
        if ( ! empty( $state['never'] ) || ( ! empty( $state['next_at'] ) && time() < (int) $state['next_at'] ) ) {
            return false;
        }
        if ( time() - (int) get_option( self::FIRST_SEEN_OPTION, time() ) < 30 * DAY_IN_SECONDS ) {
            return false;
        }
        global $wpdb;
        $completed = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}yatoon_bookings WHERE status='completed'" );
        return $completed >= 50;
    }

    private static function is_yatoon_screen() {
        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
        return $screen && false !== strpos( (string) $screen->id, 'yatoon' );
    }

    public static function handle_review_prompt() {
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_die( esc_html__( 'You do not have permission to change this preference.', 'yatoon-booking-system' ) );
        }
        check_admin_referer( 'yatoon_review_prompt' );
        $choice = sanitize_key( wp_unslash( $_GET['choice'] ?? 'later' ) );
        update_option( self::REVIEW_STATE_OPTION, 'never' === $choice ? array( 'never' => 1 ) : array( 'next_at' => time() + 90 * DAY_IN_SECONDS ), false );
        wp_safe_redirect( wp_get_referer() ?: admin_url( 'admin.php?page=yatoon-booking-system' ) );
        exit;
    }
}
