<?php
if ( ! defined( 'ABSPATH' ) ) exit;

if ( ! class_exists( 'fashion_nook_theme_admin_page' ) ) :

class fashion_nook_theme_admin_page {

    private $cfg;

    public function __construct( array $cfg ) {
        $this->cfg = $cfg;
        add_action( 'admin_menu',            array( $this, 'add_page' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
        add_action( 'admin_notices',         array( $this, 'show_notice' ) );
        add_action( 'wp_ajax_' . $this->cfg['prefix'] . '_dismiss_notice',
                                             array( $this, 'dismiss_notice' ) );
        add_action( 'after_switch_theme',    array( $this, 'reset_notice' ) );
    }

    public function add_page() {
        add_theme_page(
            sprintf( __( 'About %s', 'fashion-nook' ), $this->cfg['name'] ),
            $this->cfg['name'],
            $this->cfg['cap'],
            $this->cfg['slug'] . '-about',
            array( $this, 'render_page' )
        );
    }

    public function enqueue_assets( $hook ) {
        $page_hook = 'appearance_page_' . $this->cfg['slug'] . '-about';

        if ( $hook === $page_hook || $hook === 'index.php' ) {
            wp_enqueue_style(
                $this->cfg['prefix'] . '-admin-css',
                $this->cfg['admin_css'],
                array(),
                wp_get_theme()->get( 'Version' )
            );
        }

        wp_enqueue_script(
            $this->cfg['prefix'] . '-admin-js',
            $this->cfg['admin_js'],
            array( 'jquery' ),
            wp_get_theme()->get( 'Version' ),
            true
        );

        wp_localize_script(
            $this->cfg['prefix'] . '-admin-js',
            'themeAdminNotice',
            array(
                'ajax_url' => admin_url( 'admin-ajax.php' ),
                'nonce'    => wp_create_nonce( $this->cfg['prefix'] . '_notice_nonce' ),
                'action'   => $this->cfg['prefix'] . '_dismiss_notice',
            )
        );
    }

    public function show_notice() {
        if ( is_child_theme() ) return;

        if ( ! current_user_can( $this->cfg['cap'] ) ) return;

        $screen = get_current_screen();
        if ( $screen && $screen->id === 'appearance_page_' . $this->cfg['slug'] . '-about' ) return;

        $dismissed = get_option( $this->cfg['prefix'] . '_dismissed_users', array() );
        if ( in_array( get_current_user_id(), $dismissed ) ) return;

        $about_url = admin_url( 'themes.php?page=' . $this->cfg['slug'] . '-about' );
        ?>
        <div id="<?php echo esc_attr( $this->cfg['prefix'] ); ?>-notice"
            class="notice theme-about-notice"
            data-prefix="<?php echo esc_attr( $this->cfg['prefix'] ); ?>"
            style="display:flex; align-items:center; justify-content:space-between; padding: 24px 28px; border-left: none; border-radius: 4px; gap: 40px; position: relative;">

            <div style="flex: 1;">
                <p style="margin: 0 0 6px; font-size: 13px; color: #666;">
                    <?php printf( esc_html__( 'Hello, %s 👋', 'fashion-nook' ), esc_html( wp_get_current_user()->display_name ) ); ?>
                </p>
                <h3 style="margin: 0 0 12px; font-size: 22px; font-weight: 700; color: #1d2327;">
                    <?php printf( esc_html__( 'Welcome to %s ✨', 'fashion-nook' ), esc_html( $this->cfg['name'] ) ); ?>
                </h3>
                <p style="margin: 0 0 20px; max-width: 600px; color: #444; font-size: 14px; line-height: 1.6;">
                    <?php printf(
                        esc_html__( '%s is now installed and ready to go. Visit the theme page to explore additional professionally styled design packs, to instantly transform your look!', 'fashion-nook' ),
                        esc_html( $this->cfg['name'] )
                    ); ?>
                </p>
                <p style="margin: 0; display: flex; gap: 12px; flex-wrap: wrap;">
                    <a href="<?php echo esc_url( $about_url ); ?>" class="button button-primary" style="font-size: 13px; height: 36px; line-height: 36px; padding: 0 18px;">
                        <?php esc_html_e( 'Theme Dashboard', 'fashion-nook' ); ?>
                    </a>
                </p>
            </div>

            <?php if ( ! empty( $this->cfg['notice_image'] ) ) : ?>
            <div style="flex-shrink: 0;">
                <img src="<?php echo esc_url( $this->cfg['notice_image'] ); ?>"
                    alt="<?php echo esc_attr( $this->cfg['name'] ); ?>"
                    style="max-width: 420px; height: auto; display: block; border-radius: 4px;">
            </div>
            <?php endif; ?>

            <button type="button" class="notice-dismiss" style="position: absolute; top: 12px; right: 12px;">
                <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'fashion-nook' ); ?></span>
            </button>
        </div>
        <?php
    }

    public function dismiss_notice() {
        check_ajax_referer( $this->cfg['prefix'] . '_notice_nonce', 'nonce' );

        if ( ! current_user_can( $this->cfg['cap'] ) ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ) );
        }

        $user_id   = get_current_user_id();
        $dismissed = get_option( $this->cfg['prefix'] . '_dismissed_users', array() );

        if ( ! in_array( $user_id, $dismissed ) ) {
            $dismissed[] = $user_id;
            update_option( $this->cfg['prefix'] . '_dismissed_users', $dismissed );
        }

        wp_send_json_success();
    }

    public function reset_notice() {
        delete_option( $this->cfg['prefix'] . '_dismissed_users' );
    }

    public function render_page() {
        $cfg = $this->cfg;
        ?>
        <div class="ttn-about-wrap">
            <div class="ttn-promo-layout">
                <div class="ttn-promo-text">

                    <h1><?php esc_html_e( 'Thank you for choosing Fashion Nook!', 'fashion-nook' ); ?></h1>
                    <p><?php esc_html_e( 'Fashion Nook is a flexible, design-led WooCommerce theme built to help you create a modern, professional store without custom code.', 'fashion-nook' ); ?></p>
                    <p><?php esc_html_e( 'Clean layouts, thoughtful typography, and seamless shopping flows give you everything you need to launch a store with confidence.', 'fashion-nook' ); ?></p>

                    <p class="ttn-review">
                        ⭐⭐⭐⭐⭐<br>
                        <?php esc_html_e( 'Enjoying Fashion Nook?', 'fashion-nook' ); ?>
                        <?php esc_html_e( 'Leave a quick review on WordPress.org!', 'fashion-nook' ); ?>
                        <?php esc_html_e( 'It takes 2 minutes and means a lot to a small independent maker.', 'fashion-nook' ); ?>
                         <a href="<?php echo esc_url( $cfg['review_url'] ); ?>" target="_blank" rel="noopener">
                            <?php esc_html_e( 'Leave a review ↗', 'fashion-nook' ); ?>
                        </a>
                    </p>

                    <h2><?php esc_html_e( 'Take Fashion Nook further with a fully styled look', 'fashion-nook' ); ?></h2>
                    <p><?php esc_html_e( 'Design Packs are premium child themes built exclusively for Fashion Nook.', 'fashion-nook' ); ?></p>
                    <p><?php esc_html_e( 'While Fashion Nook gives you complete control, our curated Design Packs transform it into a beautifully cohesive brand in minutes — no tweaking, no design guesswork.', 'fashion-nook' ); ?></p>

                    <h2><?php esc_html_e( 'What\'s included with a Design Pack', 'fashion-nook' ); ?></h2>
                    <ul class="ttn-feature-list">
                        <li><b><?php esc_html_e( 'One-time payment — no subscriptions.', 'fashion-nook' ); ?></b></li>
                        <li><?php esc_html_e( 'Professionally styled child theme (Design Pack).', 'fashion-nook' ); ?></li>
                        <li><?php esc_html_e( 'Demo content included for a fast start.', 'fashion-nook' ); ?></li>
                        <li><?php esc_html_e( 'Curated color palette and font pairing for a cohesive look.', 'fashion-nook' ); ?></li>
                        <li><?php esc_html_e( 'Free updates for compatibility and improvements.', 'fashion-nook' ); ?></li>
                        <li><?php esc_html_e( 'Direct support to help you match the demo look.', 'fashion-nook' ); ?></li>
                    </ul>

                </div>
                <hr />
                <div class="ttn-packs">

                    <div class="ttn-pack">
                        <a class="ttn-pack__image" href="<?php echo esc_url( $cfg['pro_url_1'] ); ?>" target="_blank" rel="noopener">
                            <img src="<?php echo esc_url( $cfg['image_dir'] . '/solana.jpg' ); ?>"
                                alt="<?php esc_attr_e( 'Solana Design Pack Preview', 'fashion-nook' ); ?>">
                        </a>
                        <div class="ttn-pack__body">
                            <h4 class="ttn-pack__title"><?php esc_html_e( 'Solana', 'fashion-nook' ); ?></h4>
                            <p class="ttn-pack__desc">
                                <?php esc_html_e( 'Soft bohemian styling with relaxed layouts and layered typography.', 'fashion-nook' ); ?>
                            </p>
                            <div class="ttn-pack__actions">
                                <a href="<?php echo esc_url( $cfg['demo_url_1'] ); ?>" class="button button-secondary" target="_blank" rel="noopener">
                                    <?php esc_html_e( 'View Demo', 'fashion-nook' ); ?>
                                </a>
                                <a href="<?php echo esc_url( $cfg['pro_url_1'] ); ?>" class="button button-primary" target="_blank" rel="noopener">
                                    <?php esc_html_e( 'Get Solana', 'fashion-nook' ); ?>
                                </a>
                            </div>
                        </div>
                    </div>

                    <div class="ttn-pack">
                        <a class="ttn-pack__image" href="<?php echo esc_url( $cfg['pro_url_2'] ); ?>" target="_blank" rel="noopener">
                            <img src="<?php echo esc_url( $cfg['image_dir'] . '/solmare.jpg' ); ?>"
                                alt="<?php esc_attr_e( 'Solmare Design Pack Preview', 'fashion-nook' ); ?>">
                        </a>
                        <div class="ttn-pack__body">
                            <h4 class="ttn-pack__title"><?php esc_html_e( 'Solmare', 'fashion-nook' ); ?></h4>
                            <p class="ttn-pack__desc">
                                <?php esc_html_e( 'Luminous coastal styling with warm tones and refined details.', 'fashion-nook' ); ?>
                            </p>
                            <div class="ttn-pack__actions">
                                <a href="<?php echo esc_url( $cfg['demo_url_2'] ); ?>" class="button button-secondary" target="_blank" rel="noopener">
                                    <?php esc_html_e( 'View Demo', 'fashion-nook' ); ?>
                                </a>
                                <a href="<?php echo esc_url( $cfg['pro_url_2'] ); ?>" class="button button-primary" target="_blank" rel="noopener">
                                    <?php esc_html_e( 'Get Solmare', 'fashion-nook' ); ?>
                                </a>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <?php
    }
}

endif;

new fashion_nook_theme_admin_page( array(
    'prefix'    => 'fashion_nook',
    'slug'      => 'fashion-nook',
    'name'      => is_child_theme() ? wp_get_theme()->get( 'Name' ) : 'Fashion Nook',
    'cap'       => 'edit_theme_options',
    'review_url'  => 'https://wordpress.org/support/theme/fashion-nook/reviews/#new-post',
    'pro_url_1'   => 'https://thethemenook.com/products/solana/?utm_source=wpadmin&utm_medium=upgrade&utm_campaign=fashion_nook',
    'pro_url_2'   => 'https://thethemenook.com/products/solmare/?utm_source=wpadmin&utm_medium=upgrade&utm_campaign=fashion_nook',
    'demo_url_1'  => 'https://thethemenook.com/solana/',
    'demo_url_2'  => 'https://thethemenook.com/solmare/',
    'image_dir' => get_template_directory_uri() . '/admin/images',
    'admin_css' => get_template_directory_uri() . '/admin/about-theme.css',
    'admin_js'  => get_template_directory_uri() . '/admin/admin-notice.js',
) );