<?php

/**
 * Sophia Lite Theme Admin Notice and Theme Page
 * 
 * @since 1.0.0
 */

/**
 * Add theme info page
 * 
 * @since 1.0.0
 */
add_action('admin_menu', 'sophia_add_theme_page');

function sophia_add_theme_page()
{
    add_theme_page(
        'Sophia Theme',
        'Explore Sophia',
        'edit_theme_options',
        'sophia-lite-theme',
        'sophia_theme_page'
    );
}

/**
 * Theme info page content
 * 
 * @since 1.0.0
 */
function sophia_theme_page()
{
    $theme = wp_get_theme();
?>

    <!-- NEW THEME INFO WRAPPER STARTS HERE -->
    <div class="theme-info-page ">
        <div class="theme-info-inner ">
            <div class="theme-info-wrapper">
                <div class="primary">
                    <div class="info-heading">
                        <h1><?php esc_html_e('Welcome to Sophia Lite', 'sophia-lite'); ?><span><?php esc_html_e('Free', 'sophia-lite'); ?></span></h1>
                        <p><?php esc_html_e('Thank you for choosing Sophia Free Version!', 'sophia-lite'); ?></p>
                        <p><?php esc_html_e('With its clean, modern design, Sophia helps you create professional websites effortlessly.', 'sophia-lite'); ?></p>
                        <p><?php esc_html_e('This version gives you everything needed to get started, including basic patterns and a beautiful blog template.', 'sophia-lite'); ?></p>
                    </div>
                    <div class="info-content">
                        <h3><?php esc_html_e('Exclusive Pro Features', 'sophia-lite'); ?></h3>
                        <ul>
                            <li><?php esc_html_e('Advanced patterns and custom block', 'sophia-lite'); ?></li>
                            <li><?php esc_html_e('Professional landing pages', 'sophia-lite'); ?></li>
                            <li><?php esc_html_e('WooCommerce store templates', 'sophia-lite'); ?></li>
                            <li><?php esc_html_e('Premium page designs', 'sophia-lite'); ?></li>
                            <li><?php esc_html_e('Priority support and updates', 'sophia-lite'); ?></li>
                        </ul>
                        <div class="sophia-banner-buttons">
                            <a href="<?php echo esc_url('https://wpcustomthemes.com/wordpress-themes/sophia/?utm_source=explore-free-theme'); ?>" target="_blank" class="sophia-btn-primary"><?php esc_html_e('Get Sophia Pro', 'sophia-lite'); ?></a>
                            <a href="<?php echo esc_url('https://demo-sophia-pro.wpcustomthemes.com/?utm_source=explore-free-theme'); ?>" target="_blank" class="sophia-btn-secondary"><?php esc_html_e('View Demo', 'sophia-lite'); ?></a>
                        </div>
                    </div>

                </div>
                <div class="secondary">
                    <figure>
                        <img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/theme-info-lite.webp' ); ?>" alt="">
                    </figure>
                </div>
            </div>
            <footer>
                <div class="ftr-links">
                    <a class="ftr-link" href="<?php echo esc_url('https://wordpress.org/support/theme/sophia-lite/reviews'); ?>" target="_blank">
                        <span class="title"><?php esc_html_e('Leave us a review', 'sophia-lite'); ?></span>
                        <span class="content"><?php esc_html_e('We\'d love a review! Your rating helps us connect with more users.', 'sophia-lite'); ?></span>
                        <span class="btn-arrows">
                            <img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/arrow-btn-default.svg' ); ?>" alt="">
                            <img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/arrow-btn-hover.svg' ); ?>" alt="">
                        </span>
                    </a>
                    <a class="ftr-link" href="<?php echo esc_url('https://demo-sophia-free.wpcustomthemes.com/?utm_source=explore-free-theme'); ?>" target="_blank">
                        <span class="title"><?php esc_html_e('View Site Demo', 'sophia-lite'); ?></span>
                        <span class="content"><?php esc_html_e('Explore the live demo of this version and see what you can achieve.', 'sophia-lite'); ?></span>
                        <span class="btn-arrows">
                            <img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/arrow-btn-default.svg' ); ?>" alt="">
                            <img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/arrow-btn-hover.svg' ); ?>" alt="">
                        </span>
                    </a>
                    <a class="ftr-link" href="<?php echo esc_url('https://wpcustomthemes.com/docs/sophia/?utm_source=explore-free-theme'); ?>" target="_blank">
                        <span class="title"><?php esc_html_e('Theme Instructions', 'sophia-lite'); ?></span>
                        <span class="content"><?php esc_html_e('Get instructions, customisation tips, and troubleshooting help.', 'sophia-lite'); ?></span>
                        <span class="btn-arrows">
                            <img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/arrow-btn-default.svg' ); ?>" alt="">
                            <img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/arrow-btn-hover.svg' ); ?>" alt="">
                        </span>
                    </a>
                </div>

            </footer>
        </div>
    </div>
    <style>
        .theme-info-inner {
            background: #fff;
            box-shadow: 0px 1px 2px 0px #18181C0A;
            border-radius: 20px;
            margin: 40px 60px;
            max-width: 1340px;
            margin: 30px auto;

        }

        .theme-info-page {
            padding: 0 70px;
        }

        .theme-info-wrapper {
            padding: 50px 120px;
            display: flex;
            padding-bottom: 0;
            width: 100%;
            gap: 1rem;
            box-sizing: border-box;
            justify-content: center;

        }



        .theme-info-wrapper>* {
            flex: 1;
            max-width: 560px;
        }

        .theme-info-wrapper .primary * {
            max-width: 500px;
        }

        .theme-info-page footer {
            box-shadow: 0px 0px 80px -15px #8E848526;
            padding: 42px 30px;

        }

        .theme-info-page h1 {
            font-size: clamp(1.2rem, -0.875rem + 8.333vw, 2.5rem);
            margin-top: 0;
            line-height: 1.25;
            font-weight: bold;
            margin-bottom: 14px;
        }

        .theme-info-page h1 span {
            font-size: 14px;
            background: #F7DACA;
            font-weight: 500;
            border-radius: 12px;
            padding: 2px 10px;
            position: relative;
            top: -5px;
            margin-left: 7px;
            text-transform: uppercase;
        }

        .primary {
            padding-bottom: 70px;
        }

        .theme-info-page p {
            color: #6a6a6a;
            font-size: 14px;
        }

        .secondary {
            display: flex;
            align-items: flex-end;
        }

        .secondary figure {
            margin: 70px auto 0 0;
        }

        .secondary figure img {
            display: block;
            max-width: 560px;
            margin: 0 auto;
            mix-blend-mode: multiply;
        }

        .info-content {
            margin-top: 42px;
        }

        .info-content h3 {
            font-size: 18px;
            font-weight: bold;
        }

        .theme-info-wrapper .primary ul {
            margin: 21px 0;
        }

        .info-content ul li {
            color: #000;
            border-bottom: 1px solid #ddd;
            padding-bottom: 12px;
            margin-bottom: 12px;
            padding-right: 30px;
            position: relative;
            font-size: 14px;

        }

        .info-content ul li:last-child {
            border: none;
        }

        .info-content ul li:before {
            content: "";
    position: absolute;
    right: 0;
    top: 0;
    width: 1.35em;
    height: 1.35em;
            background-image: url('data:image/svg+xml;utf8,<svg fill="%233C8832" stroke="%233C8832" stroke-width=".5" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M6.00039 10.8002L2.80039 7.6002L1.86606 8.53353L6.00039 12.6669L15.0004 3.66687L14.0671 2.73353L6.00039 10.8002Z"/></svg>');
            background-size: contain;
            background-repeat: no-repeat;
        }


        .ftr-links {
            display: flex;
            gap: 2rem;
            justify-content: center;
        }

        .ftr-links .ftr-link {
            flex: 1;
            border: 1px solid #F3F1F1;
            border-radius: 10px;
            text-decoration: none;
            padding: 20px 60px 20px 20px;
            max-width: 360px;
            box-sizing: border-box;
            position: relative;
        }

        .ftr-link .btn-arrows {
            position: absolute;
            width: 24px;
            height: 24px;
            right: 20px;
            bottom: 20px;
        }

        .btn-arrows img {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: auto;
            transition: opacity 0.3s ease, transform 0.3s ease;
        }

        .btn-arrows img:nth-child(1) {
            opacity: 1;
            z-index: 1;
        }

        .btn-arrows img:nth-child(2) {
            opacity: 0;
            z-index: 2;
            transform: scale(0.95);
        }

        .ftr-link:hover .btn-arrows img:nth-child(1) {
            opacity: 0;
            transform: scale(1.05);
        }

        .ftr-link:hover .btn-arrows img:nth-child(2) {
            opacity: 1;
            transform: scale(1);
        }

        .ftr-links .ftr-link:hover {
            background-color: #FBFAFA;
        }

        .ftr-links .ftr-link span {
            display: block;
        }

        .ftr-links .ftr-link span.title {
            font-size: 15px;
            font-weight: bold;
            color: #1e1e1e;
            margin-bottom: 10px;

        }

        .ftr-links .ftr-link span.content {
            font-size: 14px;
            color: #000;
        }

        .sophia-banner-buttons {
            display: flex;
            gap: 0.75rem;
        }

        .sophia-btn-primary {
            background: #3c8832;
            color: #f9ffff;
            border: none;
            border-radius: 2px;
            padding: 10px 40px;
            font-size: 13px;
            font-weight: 500;
            line-height: 20px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }

        .sophia-btn-secondary {
            background: #f0f0f1;
            color: #303136;
            border: none;
            border-radius: 2px;
            padding: 10px 40px;
            font-size: 13px;
            font-weight: 500;
            line-height: 20px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block.
        }

        .sophia-btn-primary:hover {
            background: #2d6b26;
            color: #f9ffff;
        }

        .sophia-btn-secondary:hover {
            background: #e5e5e6;
            color: #303136;
        }

        @media (max-width: 1600px) {
            .theme-info-wrapper {
                padding: 42px 70px 0;

            }

            .secondary figure img {
                max-width: 480px;
            }
        }
    </style>
    <!-- NEW THEME INFO WRAPPER ENDS HERE -->
<?php
}

/**
 * Show styled admin notice (dismissible for 2 days)
 * 
 * @since 1.0.0
 */
add_action('admin_notices', 'sophia_admin_notice');

function sophia_admin_notice()
{
    // Only for admins
    if (!current_user_can('edit_theme_options')) {
        return;
    }

    // Check if dismissed recently
    $user_id = get_current_user_id();
    $dismissed = get_user_meta($user_id, 'sophia_lite_notice_dismissed', true);

    if ($dismissed && (time() - $dismissed) < 172800) { // 2 days = 172800 seconds
        return;
    }

    // Don't show on theme page
    $screen = get_current_screen();
    if (isset($screen->id) && $screen->id === 'appearance_page_sophia-lite-theme') {
        return;
    }

?>
    <div class="notice is-dismissible sophia-pro-banner" data-dismissible="sophia-notice">
        <div class="sophia-banner-content">
            <h1 class="sophia-banner-title"><?php esc_html_e('Upgrade to Sophia Pro - More Features, Better Design!', 'sophia-lite'); ?></h1>
            <p class="sophia-banner-subtitle"><?php esc_html_e('Access advanced patterns, custom blocks, landing pages, professional templates, and more.', 'sophia-lite'); ?></p>
            <div class="sophia-banner-buttons">
                <a href="<?php echo esc_url('https://wpcustomthemes.com/wordpress-themes/sophia/?utm_source=banner-free-theme'); ?>" target="_blank" class="sophia-btn-primary"><?php esc_html_e('Explore Sophia Pro', 'sophia-lite'); ?></a>
                <a href="<?php echo esc_url('https://demo-sophia-pro.wpcustomthemes.com/?utm_source=banner-free-theme'); ?>" target="_blank" class="sophia-btn-secondary"><?php esc_html_e('See Demo', 'sophia-lite'); ?></a>
            </div>
        </div>
    </div>


    <style>
        .sophia-pro-banner {
            position: relative;
            background: #ffffff;
            border-left: 4px solid #2d72b2 !important;
            box-shadow: 0px 3px 4px -5px rgba(24, 24, 28, 0.03), 0px 1px 2px 0px rgba(24, 24, 28, 0.04);
            padding: 15px 0;
            border: none;
        }

        .sophia-banner-content {
            padding: 10px 40px;
        }

        .sophia-banner-title {
            font-size: 22px;
            font-weight: 650 !important;
            color: #1e1e1e;
            line-height: 24px;
            margin: 0 0 4px 0;
            letter-spacing: -0.10px;
        }

        .sophia-banner-subtitle {
            font-size: 13px;
            font-weight: 400;
            color: #595656;
            line-height: 24px;
            margin: 0 0 14px 0 !important;
        }

        .sophia-banner-buttons {
            display: flex;
            gap: 20px;
            align-items: center;
        }

        .sophia-btn-primary {
            background: #3c8832;
            color: #f9ffff;
            border: none;
            border-radius: 2px;
            padding: 10px 40px;
            font-size: 13px;
            font-weight: 500;
            line-height: 20px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }

        .sophia-btn-secondary {
            background: #f0f0f1;
            color: #303136;
            border: none;
            border-radius: 2px;
            padding: 8px 15px;
            font-size: 13px;
            font-weight: 500;
            line-height: 20px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }

        .sophia-btn-primary:hover {
            background: #2d6b26;
            color: #f9ffff;
        }

        .sophia-btn-secondary:hover {
            background: #e5e5e6;
            color: #303136;
        }

        .sophia-pro-banner .notice-dismiss {
            top: 17px;
            right: 15px;
        }

        .sophia-pro-banner .notice-dismiss:before {
            color: #1e1e1e;
        }

        .sophia-pro-banner .notice-dismiss:hover:before {
            color: #666;
        }
    </style>
<?php
}

/**
 * Handle notice dismissal
 * 
 * @since 1.0.0
 */
add_action('wp_ajax_sophia_dismiss_notice', 'sophia_handle_dismiss_notice');

function sophia_handle_dismiss_notice()
{
    // Security check
    if (!current_user_can('edit_theme_options')) {
        wp_die();
    }

    // Save dismissal time
    update_user_meta(get_current_user_id(), 'sophia_lite_notice_dismissed', time());

    wp_die(); // Required for AJAX
}

/**
 * Enqueue dismiss script
 * 
 * @since 1.0.0
 */
add_action('admin_enqueue_scripts', 'sophia_enqueue_dismiss_script');

function sophia_enqueue_dismiss_script()
{
    // Only load if notice might be shown
    if (!current_user_can('edit_theme_options')) {
        return;
    }

    $user_id = get_current_user_id();
    $dismissed = get_user_meta($user_id, 'sophia_lite_notice_dismissed', true);

    if ($dismissed && (time() - $dismissed) < 172800) { 
        return;
    }

    // Inline script (no separate file used)
    wp_add_inline_script('jquery', '
        jQuery(document).ready(function($) {
            $(document).on("click", "[data-dismissible=\'sophia-notice\'] .notice-dismiss", function() {
                $.post(ajaxurl, {
                    action: "sophia_dismiss_notice"
                });
            });
        });
    ');
}