<?php
/**
 * Display the admin notice for demo setup with popup.
 */
function book_author_blog_show_demo_setup_notice() {
    // Check if the notice should be dismissed forever
    if (get_option('book_author_blog_notice_dismissed_forever')) {
        return;
    }

    // Check if "Remind Me Later" is active (show after 2 days)
    $remind_time = get_option('book_author_blog_notice_remind_time');
    if ($remind_time && time() < $remind_time) {
        return;
    }

    $demoSetupComplete = get_option('book_author_blog_demo_setup_completed');
    $noticeClass = 'demo-setup-incomplete';
    if ($demoSetupComplete) {
        $noticeClass = 'demo-setup-complete';
    }
    ?>
    <div class="book-author-blog notice notice-success notice-info book-author-blog-notice book-author-blog-welcome-notice <?php echo esc_attr($noticeClass);?>">
        <div class="book-author-blog-notice-wrapper">
            <div class="book-author-blog-notice-inner">
                <div class="notice-content-col">
                    <?php if (!$demoSetupComplete) : ?>
                        <!-- Original notice content before demo setup -->
                        <h3 class="book-author-blog-notice__title"><?php esc_html_e('Craft Your Author Website with Ease!', 'book-author-blog'); ?></h3>
                        <p class="book-author-blog-notice__content notice-desc">
                            <?php
                            printf(
                                esc_html__('Ready to bring your author presence online? Click %s to instantly install a beautifully designed starter website, complete with sample books, blog posts, and author-specific templates—perfectly tailored for writers. This seamless process takes just a few minutes and sets up all the tools you need to showcase your work.', 'book-author-blog'),
                                '<strong>"Setup Author Website Templates"</strong>'
                            );
                            ?>
                        </p>
                        <p class="book-author-blog-notice__action">
                            <button id="setup-demo-import" class="button button-primary book-author-blog-notice__button"><?php esc_html_e('Setup Author Website Templates', 'book-author-blog'); ?></button>
                        </p>
                        <div id="setup-demo-progress" class="book-author-blog-notice__progress"></div>
                        <p class="book-author-blog-notice__content notice-desc">
                            <?php esc_html_e('Once set up, editing your site is as simple as drafting a chapter—update your bio, books, and blog with ease using WordPress’s user-friendly tools.', 'book-author-blog'); ?>
                        </p>
                    <?php else : ?>
                        <!-- Updated notice content after demo setup -->
                        <h3 class="book-author-blog-notice__title"><?php esc_html_e('Your Author Website is Ready!', 'book-author-blog'); ?></h3>
                        <p class="book-author-blog-notice__content notice-desc">
                            <?php esc_html_e('Congratulations! Your demo setup is complete. Below are the pages that have been imported for your author website:', 'book-author-blog'); ?>
                        </p>
                        <ul class="imported-pages-list">
                            <?php
                            $imported_page_titles = ['Home 6', 'Home 4', 'Home 1', 'About Me', 'Books', 'Blog'];
                            foreach ($imported_page_titles as $title) {
                                $page = book_author_blog_new_get_page_by_title($title);
                                if ($page) {
                                    $view_link = get_permalink($page->ID);
                                    $edit_link = get_edit_post_link($page->ID);
                                    $elementor_edit_link = '';

                                    $is_elementor_page = get_post_meta($page->ID, '_elementor_edit_mode', true) === 'builder';
                                    if ($is_elementor_page && class_exists('\Elementor\Plugin')) {
                                        $elementor_edit_link = \Elementor\Plugin::$instance->documents->get($page->ID)->get_edit_url();
                                    }

                                    echo '<li>';
                                    echo '<strong>' . esc_html($title) . '</strong> - ';
                                    echo '<a href="' . esc_url($view_link) . '" target="_blank">' . esc_html__('View', 'book-author-blog') . '</a>';
                                    echo ' | ';
                                    if ($elementor_edit_link) {
                                        echo '<a href="' . esc_url($elementor_edit_link) . '" target="_blank">' . esc_html__('Edit with Elementor', 'book-author-blog') . '</a>';
                                    } else {
                                        echo '<a href="' . esc_url($edit_link) . '" target="_blank">' . esc_html__('Edit', 'book-author-blog') . '</a>';
                                    }
                                    echo '</li>';
                                }
                            }
                            ?>
                        </ul>
                        <p class="book-author-blog-notice__content notice-desc">
                            <?php esc_html_e('You can now edit these pages to personalize your site.', 'book-author-blog'); ?>
                        </p>
                        <p class="book-author-blog-notice__content notice-desc">
                            <?php
                            printf(
                                esc_html__('To set website logo, customize header, footer, colors, typography, %s.', 'book-author-blog'),
                                '<a href="' . esc_url(admin_url('customize.php')) . '" target="_blank">' . esc_html__('go to Customizer', 'book-author-blog') . '</a>'
                            );
                            ?>
                        </p>
                        <!-- Upgrade to Pro Section -->
                        <div class="upgrade-to-pro-part">
                            <h3><?php esc_html_e('Help Us Keep Improving – Upgrade to Pro Today!', 'book-author-blog'); ?></h3>
                            <p class="book-author-blog-notice__content notice-desc"><?php esc_html_e('We’re working hard to make this product even better for you, and your support means the world to us. By upgrading to Pro, you’ll unlock amazing features while helping us continue to enhance and grow this platform. We’re dedicated to helping our customers—after upgrading to Pro, you’ll receive our full support until your website is live, including personalized call support with screensharing to ensure your success. Join us on this journey—upgrade now and let’s build something incredible together!', 'book-author-blog'); ?></p>
                            <a id="upgrade-top-btn-after-demo-setup" href="<?php echo esc_url('https://rswpthemes.com/author-portfolio-pro-wordpress-theme/');?>" target="_blank" class="button button-primary book-author-blog-notice__button"><?php esc_html_e('Upgrade To Pro', 'book-author-blog'); ?></a>
                        </div>
                    <?php endif; ?>
                    <p class="book-author-blog-notice__links">
                        <a href="#" class="book-author-blog-notice__dismiss-forever" data-action="<?php echo esc_attr('dismiss_forever'); ?>"><?php esc_html_e('Dismiss Forever', 'book-author-blog'); ?></a> |
                        <a href="#" class="book-author-blog-notice__remind-later" data-action="<?php echo esc_attr('remind_later'); ?>"><?php esc_html_e('Remind Me Later', 'book-author-blog'); ?></a>
                        <?php
                        if ($demoSetupComplete) : ?>
                         | <a href="#" class="book-author-blog-notice__setup-demo-again" data-action="<?php echo esc_attr('setup_demo_again'); ?>"><?php esc_html_e('Setup Demo Again', 'book-author-blog'); ?></a>
                        <?php
                        endif; ?>
                    </p>
                </div>
                <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-author-blog'); ?>">
                </div>
            </div>
        </div>

        <!-- Popup Modal (unchanged) -->
        <div id="demo-setup-modal" class="demo-setup-modal" style="display: none;">
            <div class="modal-content">
                <span id="demo-modal-close" class="modal-close"><?php echo esc_html__('×', 'book-author-blog'); ?></span>
                <h3><?php esc_html_e('Demo Setup Progress', 'book-author-blog'); ?></h3>
                <div class="warning-notice" style="background: #fff3cd; padding: 10px; margin-bottom: 15px; border: 1px solid #ffeeba; border-radius: 4px;">
                    <strong><?php esc_html_e('⚠️ Please Note:', 'book-author-blog'); ?></strong> <?php esc_html_e('Keep this tab open during setup. Closing it will interrupt the installation process.', 'book-author-blog'); ?>
                </div>
                <div id="demo-progress-content"></div>
            </div>
        </div>
    </div>
    <?php
}
add_action('admin_notices', 'book_author_blog_show_demo_setup_notice');
/**
 * Handle notice dismissal and remind actions via AJAX
 */
function handle_book_author_blog_notice_actions() {
    if (isset($_POST['action_type']) && check_admin_referer('book_author_blog_notice_action', 'notice_nonce')) {
        $action = sanitize_text_field($_POST['action_type']);

        if ($action === 'dismiss_forever') {
            update_option('book_author_blog_notice_dismissed_forever', true);
            wp_send_json_success();
        } elseif ($action === 'remind_later') {
            $remind_time = time() + (2 * 24 * 60 * 60); // 2 days
            update_option('book_author_blog_notice_remind_time', $remind_time);
            wp_send_json_success();
        }elseif ($action === 'setup_demo_again') {
            delete_option( 'book_author_blog_demo_setup_completed' );
            wp_send_json_success();
        }
    }
    wp_send_json_error();
}
add_action('wp_ajax_book_author_blog_notice_action', 'handle_book_author_blog_notice_actions');

/**
 * Enqueue JavaScript for demo setup process.
 */
function book_author_blog_enqueue_demo_setup_scripts($hook) {
    wp_enqueue_script('book-author-blog-setup-demo-script', get_theme_file_uri('inc/demo-setup/demo-setup.js'), ['jquery'], '1.0', true);

    wp_localize_script('book-author-blog-setup-demo-script', 'setupDemoAjax', [
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce'    => wp_create_nonce('setup_demo_nonce'),
        'notice_nonce' => wp_create_nonce('book_author_blog_notice_action'),
    ]);
}
add_action('admin_enqueue_scripts', 'book_author_blog_enqueue_demo_setup_scripts');

// add_action('admin_init', function(){
//     delete_option( 'book_author_blog_notice_dismissed_forever' );
//     delete_option( 'book_author_blog_demo_setup_completed' );
//     delete_option( 'book_author_blog_notice_remind_time' );
// });