<?php
/**
 * Displays a welcome notice and a rating notice for the Book Review Blog WordPress Theme.
 *
 * @package Book_Review_Blog
 * @since 1.0.0
 */

/**
 * Renders the admin welcome notice.
 */
function book_review_blog_general_admin_notice() {
    // Ensure the notice is only displayed for users with appropriate permissions.
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    // Use nonce for secure dismissal.
    $dismiss_url = wp_nonce_url(
        add_query_arg( 'book_review_blog_notice_dismissed', '1' ),
        'book_review_blog_dismiss_notice',
        'book_review_blog_nonce'
    );

    ?>
    <div data-dismissible="disable-done-notice-forever" class="notice notice-info is-dismissible book-review-blog-welcome-notice">
        <div class="book-review-blog-notice-wrapper">
            <div class="book-review-blog-notice-inner">
                <div class="notice-thumbnail-col">
                    <img src="<?php echo esc_url( get_theme_file_uri( 'assets/img/author-portfolio-pro-thumb.png' ) ); ?>" alt="<?php esc_attr_e( 'Author Portfolio Pro', 'book-review-blog' ); ?>">
                </div>
                <div class="notice-content-col">
                    <h3>
                        <?php esc_html_e( 'Thank you for installing the Book Review Blog WordPress Theme.', 'book-review-blog' ); ?>
                    </h3>
                    <p class="notice-desc">
                        <?php esc_html_e( 'Ready to create a stunning website? Click the Install Starter Website Templates button to visit our demo page and get started.', 'book-review-blog' ); ?>
                    </p>
                    <p>
                        <a class="book-review-blog-btn-get-started button button-primary book-review-blog-button-padding" href="#" data-name="" data-slug="">
                            <?php esc_html_e( 'Import Demo Content', 'book-review-blog' ); ?>
                        </a>
                        <a target="_blank" href="<?php echo esc_url( book_review_blog_utm_url( 'welcome_notice' ) ); ?>" class="button button-highlight upgrade-to-pro button-primary">
                            <?php esc_html_e( 'Get RS WP Book Showcase Pro', 'book-review-blog' ); ?>
                        </a>
                        <a href="<?php echo esc_url( admin_url( 'themes.php?page=book-review-blog-getting-started' ) ); ?>" class="button button-secondary">
                            <?php esc_html_e( 'Getting Started Video Tutorial', 'book-review-blog' ); ?>
                        </a>
                        <a href="<?php echo esc_url( $dismiss_url ); ?>" style="text-decoration: none; float: right;" class="book-review-blog-dismiss-notice">
                            <?php esc_html_e( 'Dismiss Notice', 'book-review-blog' ); ?>
                        </a>
                    </p>
                </div>
            </div>
        </div>
    </div>
    <?php
}

/**
 * Renders the rating notice.
 */
function book_review_blog_rating_admin_notice() {
    // Ensure the notice is only displayed for users with appropriate permissions.
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    // Check if the rating notice has been dismissed or delayed.
    if ( get_option( 'book_review_blog_rating_notice_dismissed', '' ) === 'dismissed' ) {
        return;
    }

    $remind_time = get_transient( 'book_review_blog_rating_notice_remind_time' );
    if ( $remind_time && time() < $remind_time ) {
        return;
    }

    // Use nonce for secure dismissal and remind actions.
    $dismiss_url = wp_nonce_url(
        add_query_arg( 'book_review_blog_rating_notice_dismissed', '1' ),
        'book_review_blog_rating_dismiss_notice',
        'book_review_blog_rating_nonce'
    );
    $remind_url = wp_nonce_url(
        add_query_arg( 'book_review_blog_rating_notice_remind', '1' ),
        'book_review_blog_rating_remind_notice',
        'book_review_blog_rating_nonce'
    );

    // Link to WordPress.org theme review page (replace with your theme's slug).
    $review_url = 'https://wordpress.org/support/theme/book-review-blog/reviews/#new-post';

    ?>
    <div data-dismissible="disable-rating-notice-forever" class="notice notice-info is-dismissible book-review-blog-rating-notice">
        <div class="book-review-blog-notice-wrapper">
            <h3>
                <?php esc_html_e( 'Enjoying the Book Review Blog Theme?', 'book-review-blog' ); ?>
            </h3>
            <p>
                <?php esc_html_e( 'We’d love to hear your feedback! Please take a moment to leave a review and help others discover our theme.', 'book-review-blog' ); ?>
            </p>
            <p>
                <a href="<?php echo esc_url( $review_url ); ?>" target="_blank" class="button button-primary">
                    <?php esc_html_e( 'Rate This Theme ★★★★★', 'book-review-blog' ); ?>
                </a>
                <a href="<?php echo esc_url( $dismiss_url ); ?>" style="text-decoration: none; margin-right: 10px;" class="book-review-blog-dismiss-rating-notice">
                    <?php esc_html_e( 'Dismiss', 'book-review-blog' ); ?>
                </a>
                <a href="<?php echo esc_url( $remind_url ); ?>" style="text-decoration: none;" class="book-review-blog-remind-rating-notice">
                    <?php esc_html_e( 'Remind Me Later', 'book-review-blog' ); ?>
                </a>
            </p>
        </div>
    </div>
    <?php
}

/**
 * Handles the dismissal of the welcome notice.
 */
function book_review_blog_handle_notice_dismissal() {
    if ( ! isset( $_GET['book_review_blog_notice_dismissed'] ) || ! isset( $_GET['book_review_blog_nonce'] ) ) {
        return;
    }

    // Verify nonce for security.
    if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['book_review_blog_nonce'] ) ), 'book_review_blog_dismiss_notice' ) ) {
        return;
    }

    // Update option and set transient for 2 minutes.
    update_option( 'book_review_blog_help_notice', 'notice_book_review_blog_dismissed' );
    set_transient( 'book_review_blog_welcome_notice_dismissed_time', time(), 120 );
}

/**
 * Handles the dismissal of the rating notice.
 */
function book_review_blog_handle_rating_notice_dismissal() {
    if ( ! isset( $_GET['book_review_blog_rating_notice_dismissed'] ) || ! isset( $_GET['book_review_blog_rating_nonce'] ) ) {
        return;
    }

    // Verify nonce for security.
    if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['book_review_blog_rating_nonce'] ) ), 'book_review_blog_rating_dismiss_notice' ) ) {
        return;
    }

    // Update option to permanently dismiss the rating notice.
    update_option( 'book_review_blog_rating_notice_dismissed', 'dismissed' );
}

/**
 * Handles the "Remind Me Later" action for the rating notice.
 */
function book_review_blog_handle_rating_notice_remind() {
    if ( ! isset( $_GET['book_review_blog_rating_notice_remind'] ) || ! isset( $_GET['book_review_blog_rating_nonce'] ) ) {
        return;
    }

    // Verify nonce for security.
    if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['book_review_blog_rating_nonce'] ) ), 'book_review_blog_rating_remind_notice' ) ) {
        return;
    }

    // Set transient to delay the notice for 1 day.
    set_transient( 'book_review_blog_rating_notice_remind_time', time() + DAY_IN_SECONDS, DAY_IN_SECONDS );
}

/**
 * Conditionally registers the welcome and rating notices.
 */
function book_review_blog_register_admin_notice() {
    $help_notice = get_option( 'book_review_blog_help_notice', '' );
    // If welcome notice has been dismissed, check the transient.
    if ( 'notice_book_review_blog_dismissed' === $help_notice ) {
        $dismissed_time = get_transient( 'book_review_blog_welcome_notice_dismissed_time' );
        if ( false === $dismissed_time || time() > $dismissed_time + 120 ) {
            // Transient has expired; delete it and show the rating notice.
            delete_transient( 'book_review_blog_welcome_notice_dismissed_time' );
            // Do not delete the help_notice option to prevent welcome notice from reappearing.
            add_action( 'admin_notices', 'book_review_blog_rating_admin_notice' );
        }
    } else {
        // Show welcome notice if it hasn't been dismissed.
        add_action( 'admin_notices', 'book_review_blog_general_admin_notice' );
    }
}

add_action( 'admin_init', 'book_review_blog_handle_notice_dismissal' );
add_action( 'admin_init', 'book_review_blog_handle_rating_notice_dismissal' );
add_action( 'admin_init', 'book_review_blog_handle_rating_notice_remind' );
add_action( 'admin_init', 'book_review_blog_register_admin_notice' );