<?php
/**
 * Admin notices for the WordPress.org free build.
 *
 * @package CF7MasterPro\Admin
 */

namespace CF7MasterPro\Admin;

defined( 'ABSPATH' ) || exit;

/**
 * Lightweight admin notices.
 */
class Notices {

	/**
	 * Register hooks.
	 */
	public function register() {
		add_action( 'admin_notices', array( $this, 'render_activation_welcome' ) );
		add_action( 'admin_notices', array( $this, 'render_review' ) );
		add_action( 'wp_ajax_cf7mp_dismiss_notice', array( $this, 'dismiss' ) );
	}

	/**
	 * Initial activation welcome.
	 */
	public function render_activation_welcome() {
		if ( ! get_transient( 'cf7mp_activation_redirect' ) ) {
			return;
		}
		delete_transient( 'cf7mp_activation_redirect' );

		$url = admin_url( 'admin.php?page=frontierline-forms' );
		echo '<div class="notice notice-success is-dismissible">';
		echo '<p>';
		echo '<strong>Frontierline Forms</strong> ' . esc_html__( ' has been installed.', 'frontierline-forms' );
		echo ' <a href="' . esc_url( $url ) . '">' . esc_html__( 'Go to settings', 'frontierline-forms' ) . '</a>';
		echo '</p></div>';
	}

	/**
	 * レビュー依頼通知（プラグイン管理画面のみ・送信3件以上 or 有効化7日後に発火）
	 */
	public function render_review() {
		if ( get_user_meta( get_current_user_id(), 'cf7mp_dismiss_review', true ) ) {
			return;
		}

		$screen = get_current_screen();
		if ( ! $screen || false === strpos( $screen->id, 'cf7-master' ) ) {
			return;
		}

		$since = (int) get_option( 'cf7mp_review_since', 0 );
		if ( ! $since ) {
			update_option( 'cf7mp_review_since', time() );
			$since = time();
		}

		global $wpdb;
		$table = $wpdb->prefix . CF7MP_DB_PREFIX . 'submissions';
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$count = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM `' . esc_sql( $table ) . '`' );

		$ready = ( $count >= 3 ) || ( ( time() - $since ) >= 7 * DAY_IN_SECONDS );
		if ( ! $ready ) {
			return;
		}

		$review_url = 'https://wordpress.org/support/plugin/frontierline-forms/reviews/?rate=5#new-post';
		$btn        = sprintf(
			'<a href="%s" target="_blank" rel="noopener" class="button button-primary cf7mp-review-cta" data-notice="review">%s</a>',
			esc_url( $review_url ),
			esc_html__( '★ Write a review', 'frontierline-forms' )
		);
		$later = sprintf(
			'<a href="#" class="cf7mp-dismiss" data-notice="review">%s</a>',
			esc_html__( 'Do not show again', 'frontierline-forms' )
		);

		echo '<div class="notice notice-info" style="display:flex;align-items:center;gap:16px;">';
		echo '<p style="flex:1;margin:0;">';
		echo '⭐ ' . esc_html__( 'Is Frontierline Forms helpful? A 5-star review would really help and takes about 30 seconds.', 'frontierline-forms' );
		echo '</p>';
		echo '<p style="margin:0;white-space:nowrap;">' . wp_kses_post( $btn . ' ' . $later ) . '</p>';
		echo '</div>';
	}

	/**
	 * Dismiss notice.
	 */
	public function dismiss() {
		check_ajax_referer( 'cf7mp_dismiss', '_wpnonce' );
		$notice = isset( $_POST['notice'] ) ? sanitize_text_field( wp_unslash( $_POST['notice'] ) ) : '';
		if ( $notice ) {
			update_user_meta( get_current_user_id(), 'cf7mp_dismiss_' . $notice, 1 );
		}
		wp_send_json_success();
	}
}
