<?php
function aurora_lite_register_license_menu() {
    add_submenu_page(
        'themes.php',
        'Aurora Lite',
        'Aurora Lite',
        'manage_options',
        'aurora-lite',
        'aurora_lite_render_license_page'
    );
}
add_action( 'admin_menu', 'aurora_lite_register_license_menu' );


function aurora_lite_render_license_page() {
    $pro_url = 'https://aurorawptheme.com/?utm_source=aurora-lite&utm_medium=admin-page&utm_campaign=upgrade';
    ?>
    <style>
        #aurora-lite-admin-page {
            max-width: 900px;
            margin: 40px auto;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            color: #1d2327;
        }

        #aurora-lite-admin-page .aap-header {
            background: linear-gradient(135deg, #004354 0%, #006370 100%);
            border-radius: 12px;
            padding: 40px 48px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 32px;
            color: #fff;
        }
        #aurora-lite-admin-page .aap-header-text h1 {
            margin: 0 0 6px;
            font-size: 28px;
            font-weight: 700;
            color: #fff;
            line-height: 1.2;
        }
        #aurora-lite-admin-page .aap-header-text p {
            margin: 0;
            font-size: 15px;
            color: #a8d8e0;
        }
        #aurora-lite-admin-page .aap-header-badge {
            background: rgba(255,255,255,0.15);
            border: 1px solid rgba(255,255,255,0.25);
            border-radius: 20px;
            padding: 6px 16px;
            font-size: 13px;
            font-weight: 600;
            color: #fff;
            white-space: nowrap;
        }

        #aurora-lite-admin-page .aap-screenshots {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 12px;
            margin-bottom: 32px;
        }
        #aurora-lite-admin-page .aap-screenshots img {
            width: 100%;
            border-radius: 8px;
            border: 1px solid #e2e8f0;
            display: block;
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }
        #aurora-lite-admin-page .aap-screenshots img:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 24px rgba(0,0,0,0.12);
        }
        #aurora-lite-admin-page .aap-screenshots .aap-screenshot-main {
            grid-column: span 2;
        }

        #aurora-lite-admin-page .aap-cols {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 24px;
            margin-bottom: 24px;
        }

        #aurora-lite-admin-page .aap-card {
            background: #fff;
            border: 1px solid #e2e8f0;
            border-radius: 10px;
            padding: 28px 32px;
        }
        #aurora-lite-admin-page .aap-card h2 {
            margin: 0 0 18px;
            font-size: 15px;
            font-weight: 700;
            color: #1d2327;
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }

        #aurora-lite-admin-page .aap-features {
            list-style: none;
            margin: 0;
            padding: 0;
        }
        #aurora-lite-admin-page .aap-features li {
            display: flex;
            align-items: flex-start;
            gap: 10px;
            padding: 8px 0;
            font-size: 13.5px;
            color: #3c4149;
            border-bottom: 1px solid #f1f5f9;
            line-height: 1.4;
        }
        #aurora-lite-admin-page .aap-features li:last-child {
            border-bottom: none;
        }
        #aurora-lite-admin-page .aap-features li .aap-check {
            width: 18px;
            height: 18px;
            border-radius: 50%;
            flex-shrink: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 10px;
            font-weight: 700;
            margin-top: 1px;
        }
        #aurora-lite-admin-page .aap-features li .aap-check.yes {
            background: #dcfce7;
            color: #16a34a;
        }
        #aurora-lite-admin-page .aap-features li .aap-check.no {
            background: #f1f5f9;
            color: #94a3b8;
        }
        #aurora-lite-admin-page .aap-features li .aap-feat-label {
            flex: 1;
        }
        #aurora-lite-admin-page .aap-features li .aap-feat-val {
            font-size: 12px;
            font-weight: 600;
            white-space: nowrap;
        }
        #aurora-lite-admin-page .aap-features li .aap-feat-val.pro {
            color: #00b196;
        }
        #aurora-lite-admin-page .aap-features li .aap-feat-val.lite {
            color: #94a3b8;
        }

        #aurora-lite-admin-page .aap-stats {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 16px;
            margin-bottom: 24px;
        }
        #aurora-lite-admin-page .aap-stat {
            background: #fff;
            border: 1px solid #e2e8f0;
            border-radius: 10px;
            padding: 20px;
            text-align: center;
        }
        #aurora-lite-admin-page .aap-stat-number {
            font-size: 28px;
            font-weight: 800;
            color: #004354;
            line-height: 1;
            margin-bottom: 4px;
        }
        #aurora-lite-admin-page .aap-stat-label {
            font-size: 12px;
            color: #64748b;
            font-weight: 500;
        }

        #aurora-lite-admin-page .aap-cta {
            background-color: #007765;
            border-radius: 12px;
            padding: 48px;
            text-align: center;
            margin-top: 8px;
        }
        #aurora-lite-admin-page .aap-cta h2 {
            margin: 0 0 10px;
            font-size: 24px;
            font-weight: 800;
            color: #fff;
        }
        #aurora-lite-admin-page .aap-cta p {
            margin: 10px 0 28px;
            font-size: 15px;
            color: rgba(255,255,255,0.88);
            max-width: 480px;
            margin-left: auto;
            margin-right: auto;
        }
        #aurora-lite-admin-page .aap-cta-btn {
            display: inline-block;
            background: #fff;
            color: #007765;
            font-size: 15px;
            font-weight: 700;
            padding: 14px 36px;
            border-radius: 6px;
            text-decoration: none;
            transition: transform 0.15s ease, box-shadow 0.15s ease;
            box-shadow: 0 4px 16px rgba(0,0,0,0.15);
        }
        #aurora-lite-admin-page .aap-cta-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 24px rgba(0,0,0,0.2);
            color: #01b6ff;
        }
        #aurora-lite-admin-page .aap-cta-sub {
            margin-top: 14px;
            font-size: 12px;
            color: rgba(255,255,255,0.7);
        }
        #aurora-lite-admin-page .aap-cta-sub a {
            color: rgba(255,255,255,0.9);
            text-decoration: underline;
        }
    </style>

    <div id="aurora-lite-admin-page">

        <!-- Header -->
        <div class="aap-header">
            <div class="aap-header-text">
                <h1>Aurora</h1>
                <p>A flexible WordPress blog theme for creators &amp; publishers</p>
            </div>
            <span class="aap-header-badge">Free Version</span>
        </div>

        <!-- Screenshots -->
        <div class="aap-screenshots">
            <div class="aap-screenshot-main">
                <img src="<?php echo esc_url( get_template_directory_uri() . '/screenshot.png' ); ?>" alt="Aurora theme screenshot" />
            </div>
            <div style="display:flex;flex-direction:column;gap:12px;">
                <img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/Vertical.png' ); ?>" alt="Aurora layout preview" style="flex:1;object-fit:cover;object-position:left center;height:0;min-height:100%;" />
            </div>
        </div>

        <!-- Stats -->
        <div class="aap-stats">
            <div class="aap-stat">
                <div class="aap-stat-number">8</div>
                <div class="aap-stat-label">Header Layouts</div>
            </div>
            <div class="aap-stat">
                <div class="aap-stat-number">10</div>
                <div class="aap-stat-label">Single Post Layouts</div>
            </div>
            <div class="aap-stat">
                <div class="aap-stat-number">13</div>
                <div class="aap-stat-label">Archive Styles</div>
            </div>
            <div class="aap-stat">
                <div class="aap-stat-number">4,640+</div>
                <div class="aap-stat-label">Layout Combinations</div>
            </div>
        </div>

        <!-- Feature comparison -->
        <div class="aap-cols">

            <div class="aap-card">
                <h2>What's in the free version</h2>
                <ul class="aap-features">
                    <li><span class="aap-check yes">✓</span><span class="aap-feat-label">1 Header layout</span></li>
                    <li><span class="aap-check yes">✓</span><span class="aap-feat-label">1 Homepage style</span></li>
                    <li><span class="aap-check yes">✓</span><span class="aap-feat-label">1 Single post layout</span></li>
                    <li><span class="aap-check yes">✓</span><span class="aap-feat-label">1 Archive style</span></li>
                    <li><span class="aap-check yes">✓</span><span class="aap-feat-label">1 Footer layout</span></li>
                    <li><span class="aap-check yes">✓</span><span class="aap-feat-label">Social sharing on posts</span></li>
                    <li><span class="aap-check yes">✓</span><span class="aap-feat-label">RTL &amp; translation ready</span></li>
                </ul>
            </div>

            <div class="aap-card">
                <h2>Unlocked in Pro</h2>
                <ul class="aap-features">
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">Header layouts</span>
                        <span class="aap-feat-val pro">8 total</span>
                    </li>
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">Homepage styles</span>
                        <span class="aap-feat-val pro">5 total</span>
                    </li>
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">Single post layouts</span>
                        <span class="aap-feat-val pro">10 total</span>
                    </li>
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">Archive styles</span>
                        <span class="aap-feat-val pro">13 total</span>
                    </li>
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">Footer layouts</span>
                        <span class="aap-feat-val pro">5 total</span>
                    </li>
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">404 pages</span>
                        <span class="aap-feat-val pro">5 total</span>
                    </li>
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">Author and Search styles</span>
                        <span class="aap-feat-val pro">7 total</span>
                    </li>
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">Custom content sections on homepage</span>
                        <span class="aap-feat-val pro">4 total</span>
                    </li>
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">Vertical headers (2 styles)</span>
                    </li>
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">Contact details in header &amp; footer</span>
                    </li>
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">Recent posts in footer</span>
                    </li>
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">Author social media fields</span>
                    </li>
                    <li>
                        <span class="aap-check yes">✓</span>
                        <span class="aap-feat-label">Fields for custom JavaScript</span>
                    </li>
                </ul>
            </div>

        </div>

        <!-- CTA -->
        <div class="aap-cta">
            <h2>Ready for the full experience?</h2>
            <p>Unlock all layouts, styles, and Pro features for a one-time payment of $39. No subscriptions.</p>
            <a href="<?php echo esc_url( $pro_url ); ?>" target="_blank" class="aap-cta-btn">
                Upgrade to Aurora &rarr;
            </a>
            <p class="aap-cta-sub">
                <a href="https://demo.aurorawptheme.com/?utm_source=aurora-lite&utm_medium=admin-page&utm_campaign=demo" target="_blank">Or view the live demo first</a>
            </p>
        </div>

    </div>
    <?php
}


// -----------------------------------------------------------------------------
// Dashboard widget
// -----------------------------------------------------------------------------

function aurora_lite_register_info_widget() {
    wp_add_dashboard_widget(
        'aurora_lite_info_widget',
        'Aurora WP Theme',
        'aurora_lite_info_widget_content'
    );
}
add_action( 'wp_dashboard_setup', 'aurora_lite_register_info_widget' );

function aurora_lite_prioritize_info_widget() {
    global $wp_meta_boxes;

    $widget = $wp_meta_boxes['dashboard']['normal']['core']['aurora_lite_info_widget'] ?? null;
    if ( ! $widget ) return;

    unset( $wp_meta_boxes['dashboard']['normal']['core']['aurora_lite_info_widget'] );
    $wp_meta_boxes['dashboard']['normal']['core'] = array_merge(
        [ 'aurora_lite_info_widget' => $widget ],
        $wp_meta_boxes['dashboard']['normal']['core']
    );

    // Update user meta so WordPress respects the order on page load
    $user_id   = get_current_user_id();
    $meta_key  = 'meta-box-order_dashboard';
    $saved     = get_user_meta( $user_id, $meta_key, true );

    if ( empty( $saved ) || ! is_array( $saved ) ) {
        update_user_meta( $user_id, $meta_key, [
            'normal' => 'aurora_lite_info_widget',
        ] );
    } elseif ( isset( $saved['normal'] ) ) {
        $normal = array_filter( explode( ',', $saved['normal'] ) );
        $normal = array_diff( $normal, [ 'aurora_lite_info_widget' ] );
        array_unshift( $normal, 'aurora_lite_info_widget' );
        $saved['normal'] = implode( ',', $normal );
        update_user_meta( $user_id, $meta_key, $saved );
    }
}
add_action( 'wp_dashboard_setup', 'aurora_lite_prioritize_info_widget', 999 );


function aurora_lite_info_widget_content() {
    $contact_form_url = 'https://aurorawptheme.com/contact';
    $contact_email    = 'hello@aurorawptheme.com';

    $links = [
        [ 'title' => 'Theme Documentation',              'url' => 'https://aurorawptheme.com/docs/' ],
        [ 'title' => 'First steps after activating Aurora', 'url' => 'https://aurorawptheme.com/aurora-first-steps/' ],
        [ 'title' => 'Video Tutorials',                  'url' => 'https://www.youtube.com/@aurora-theme' ],
    ];

    $header   = get_theme_mod( 'header_layout',                     'Default' );
    $footer   = get_theme_mod( 'footer_layout',                     'Default' );
    $homepage = get_theme_mod( 'homepage_layout',                   'Default' );
    $archive  = get_theme_mod( 'archive_layout',                    'Default' );
    $single   = get_theme_mod( 'single_post_layout',                'Default' );
    $author   = get_theme_mod( 'author_page_layout',                'Default' );
    $search   = get_theme_mod( 'search_results_layout',             'Default' );
    $page_404 = get_theme_mod( '404_page_layout',                   'Default' );
    $archive_sidebar = get_theme_mod( 'aurora_show_archive_sidebar',    false );
    $archive_masonry = get_theme_mod( 'aurora_archive_masonry_enabled', false );
    $posts_per_page  = get_theme_mod( 'aurora_posts_per_page',          16    );
    $single_sidebar  = get_theme_mod( 'aurora_show_sidebar',            false );
    $banner_type     = get_theme_mod( 'archive_banner_type',            'classy' );

    $on  = '<span style="color:#2e7d32; font-weight:600;">On</span>';
    $off = '<span style="color:#aaa;">Off</span>';
    ?>

    <style>
        .aur-section            { margin-bottom: 18px; }
        .aur-section:last-child { margin-bottom: 0; }
        .aur-section h3         { font-size: 11px; text-transform: uppercase; letter-spacing: .07em; color: #aaa; margin: 0 0 8px; padding-bottom: 5px; border-bottom: 1px solid #f0f0f0; display: flex; justify-content: space-between; align-items: center; }
        .aur-links              { list-style: none; margin: 0; padding: 0; }
        .aur-links li           { margin-bottom: 6px; font-size: 13px; }
        .aur-links li a         { text-decoration: none; color: #2271b1; }
        .aur-links li a:hover   { text-decoration: underline; }
        .aur-links li::before   { content: '›'; color: #aaa; margin-right: 6px; }
        .aur-contact            { display: flex; gap: 12px; }
        .aur-contact a          { font-size: 13px; text-decoration: none; color: #2271b1; }
        .aur-contact a:hover    { text-decoration: underline; }
        .aur-grid               { display: grid; grid-template-columns: 100px 1fr; gap: 5px 10px; font-size: 13px; }
        .aur-grid .k            { color: #888; }
        .aur-grid .v            { font-weight: 500; }
        .aur-grid .sub          { grid-column: 2; font-size: 11px; color: #aaa; margin-top: -3px; }
        .aur-upgrade            { margin-top: 16px; padding-top: 14px; border-top: 1px solid #f0f0f0; font-size: 12px; color: #64748b; }
        .aur-upgrade a          { color: #007765; font-weight: 600; text-decoration: none; }
        .aur-upgrade a:hover    { text-decoration: underline; }
    </style>

    <div class="aur-section">
        <h3>Helpful Links</h3>
        <ul class="aur-links">
            <?php foreach ( $links as $link ) : ?>
            <li>
                <a href="<?php echo esc_url( $link['url'] ); ?>" target="_blank" rel="noopener">
                    <?php echo esc_html( $link['title'] ); ?>
                </a>
            </li>
            <?php endforeach; ?>
        </ul>
    </div>

    <div class="aur-section">
        <h3>Need Help?</h3>
        <div class="aur-contact">
            <a href="https://aurorawptheme.com/docs/" target="_blank" rel="noopener">Docs</a>
            <a href="<?php echo esc_url( $contact_form_url ); ?>" target="_blank" rel="noopener">Contact Form</a>
            <a href="mailto:<?php echo esc_attr( $contact_email ); ?>">Send Email</a>
        </div>
    </div>

    <div class="aur-section">
        <h3>Active Templates</h3>
        <div class="aur-grid">
            <span class="k">Header</span>   <span class="v"><?php echo esc_html( $header ); ?></span>
            <span class="k">Footer</span>   <span class="v"><?php echo esc_html( $footer ); ?></span>
            <span class="k">Homepage</span> <span class="v"><?php echo esc_html( $homepage ); ?></span>
            <span class="k">Single</span>   <span class="v"><?php echo esc_html( $single ); ?></span>
            <span class="sub">Sidebar <?php echo $single_sidebar ? $on : $off; ?></span>
            <span class="k">Archive</span>  <span class="v"><?php echo esc_html( $archive ); ?></span>
            <span class="sub">
                Sidebar <?php echo $archive_sidebar ? $on : $off; ?> &nbsp;&middot;&nbsp;
                Masonry <?php echo $archive_masonry ? $on : $off; ?> &nbsp;&middot;&nbsp;
                <?php echo (int) $posts_per_page; ?> per page &nbsp;&middot;&nbsp;
                Banner: <?php echo esc_html( ucfirst( $banner_type ) ); ?>
            </span>
            <span class="k">Author</span>   <span class="v"><?php echo esc_html( $author ); ?></span>
            <span class="k">Search</span>   <span class="v"><?php echo esc_html( $search ); ?></span>
            <span class="k">404</span>      <span class="v"><?php echo esc_html( $page_404 ); ?></span>
        </div>
    </div>

    <div class="aur-upgrade">
        Using the free version. <a href="<?php echo esc_url( admin_url( 'themes.php?page=aurora-lite' ) ); ?>">Upgrade to Aurora Pro</a> for 53 layouts and more.
    </div>

    <?php
}