<?php
/**
 * Nihongo Theme Functions
 * 
 * @package Nihongo
 * @version 1.0.0
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Theme setup
 */
function nihongo_theme_setup() {
    // Add theme support for various features
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_theme_support('html5', array(
        'search-form',
        'comment-form',
        'comment-list',
        'gallery',
        'caption',
    ));
    add_theme_support('custom-logo');
    add_theme_support('customize-selective-refresh-widgets');
    
    // Add support for wide and full alignment
    add_theme_support('align-wide');
    
    // Add support for editor styles
    add_theme_support('editor-styles');
    add_editor_style('assets/css/editor-style.css');
    
    // Register navigation menus
    register_nav_menus(array(
        'primary' => __('Primary Menu', 'nihongo'),
        'footer' => __('Footer Menu', 'nihongo'),
    ));
    
    // Add image sizes
    add_image_size('course-thumbnail', 400, 300, true);
    add_image_size('testimonial-thumbnail', 300, 300, true);
    add_image_size('news-thumbnail', 600, 400, true);
    add_image_size('hero-image', 1920, 1080, true);
}
add_action('after_setup_theme', 'nihongo_theme_setup');

/**
 * Enqueue scripts and styles
 */
function nihongo_scripts() {
    $template_dir = get_template_directory_uri();
    $theme_version = '1.0.0';
    
    // ===== STYLES =====
    
    // Main theme stylesheet (required by WordPress)
    wp_enqueue_style('nihongo-style', get_stylesheet_uri(), array(), $theme_version);
    
    // Main CSS file
    wp_enqueue_style('nihongo-main-style', $template_dir . '/assets/css/style.css', array('nihongo-style'), $theme_version);
    
    // Google Fonts
    wp_enqueue_style('nihongo-fonts', 'https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Noto+Sans+JP:wght@300;400;500;700;900&family=M+PLUS+Rounded+1c:wght@400;500;700;800&family=BIZ+UDGothic:wght@400;700&family=Noto+Serif+JP:wght@400;600;700;900&family=Hachi+Maru+Pop&display=swap', array(), null);
    
    // ===== SCRIPTS =====
    
    // Main JavaScript file
    wp_enqueue_script('nihongo-main-js', $template_dir . '/assets/js/main.js', array('jquery'), $theme_version, true);
    
    // Navigation JavaScript
    wp_enqueue_script('nihongo-navigation', $template_dir . '/assets/js/navigation.js', array('jquery'), $theme_version, true);
    
    // Localize script for AJAX and theme data
    wp_localize_script('nihongo-main-js', 'nihongo_ajax', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('nihongo_nonce'),
        'theme_url' => $template_dir,
        'current_lang' => nihongo_get_current_language(),
        'is_home' => is_front_page(),
        'is_courses' => is_page('courses') || is_post_type_archive('course'),
        'is_contact' => is_page('contact'),
        'is_j_bridge' => is_page('j-bridge'),
        'is_news' => is_page('news') || is_post_type_archive('news'),
        'is_voices' => is_page('voices') || is_post_type_archive('testimonial'),
    ));
    
    // ===== CONDITIONAL SCRIPTS =====
    
    // Hero slider script for home page
    if (is_front_page()) {
        wp_enqueue_script('nihongo-hero-slider', $template_dir . '/assets/js/hero-slider.js', array('jquery'), $theme_version, true);
    }
    
    // Contact form validation for contact page
    if (is_page('contact')) {
        wp_enqueue_script('nihongo-contact-form', $template_dir . '/assets/js/contact-form.js', array('jquery'), $theme_version, true);
    }
    
    // Course filtering for courses page
    if (is_page('courses') || is_post_type_archive('course')) {
        wp_enqueue_script('nihongo-course-filter', $template_dir . '/assets/js/course-filter.js', array('jquery'), $theme_version, true);
    }
    
    // Testimonial carousel for voices page
    if (is_page('voices') || is_post_type_archive('testimonial')) {
        wp_enqueue_script('nihongo-testimonial-carousel', $template_dir . '/assets/js/testimonial-carousel.js', array('jquery'), $theme_version, true);
    }
    
    // News carousel for news page
    if (is_page('news') || is_post_type_archive('news')) {
        wp_enqueue_script('nihongo-news-carousel', $template_dir . '/assets/js/news-carousel.js', array('jquery'), $theme_version, true);
    }
    
    // J-Bridge specific scripts
    if (is_page('j-bridge')) {
        wp_enqueue_script('nihongo-j-bridge', $template_dir . '/assets/js/j-bridge.js', array('jquery'), $theme_version, true);
    }
    
    // ===== WORDPRESS CORE SCRIPTS =====
    
    // Comment reply script
    if (is_singular() && comments_open() && get_option('thread_comments')) {
        wp_enqueue_script('comment-reply');
    }
    
    // ===== ADMIN SCRIPTS =====
    
    // Admin scripts for custom post types
    if (is_admin()) {
        wp_enqueue_script('nihongo-admin', $template_dir . '/assets/js/admin.js', array('jquery'), $theme_version, true);
        wp_enqueue_style('nihongo-admin-style', $template_dir . '/assets/css/admin.css', array(), $theme_version);
    }
}
add_action('wp_enqueue_scripts', 'nihongo_scripts');

/**
 * Language Functions
 */
function nihongo_get_current_language() {
    // Check if language is set in cookie
    if (isset($_COOKIE['nihongo_language'])) {
        return $_COOKIE['nihongo_language'];
    }
    
    // Check browser language preference
    if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
        $browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        if (in_array($browser_lang, ['en', 'ja'])) {
            $lang = ($browser_lang === 'ja') ? 'jp' : 'en';
            return $lang;
        }
    }
    
    // Default to Japanese
    return 'jp';
}

/**
 * Set language preference
 */
function nihongo_set_language($lang) {
    if (in_array($lang, ['jp', 'en'])) {
        setcookie('nihongo_language', $lang, time() + (365 * 24 * 60 * 60), '/');
        $_COOKIE['nihongo_language'] = $lang;
    }
}

/**
 * Get translation for current language
 */
function nihongo_translate($key) {
    $translations = nihongo_get_translations();
    $current_lang = nihongo_get_current_language();
    
    if (isset($translations[$key][$current_lang])) {
        return $translations[$key][$current_lang];
    }
    
    // Fallback to English if translation not found
    if (isset($translations[$key]['en'])) {
        return $translations[$key]['en'];
    }
    
    // Return key if no translation found
    return $key;
}

/**
 * Get all translations
 */
function nihongo_get_translations() {
    return array(
        // Page titles
        'home_title' => array(
            'jp' => 'nihongo.net - 日本語学習サービス',
            'en' => 'nihongo.net - Japanese Learning Service'
        ),
        'courses_title' => array(
            'jp' => 'コース紹介 - nihongo.net',
            'en' => 'Course Introduction - nihongo.net'
        ),
        'j_bridge_title' => array(
            'jp' => 'J-Bridge - 外国人材マッチング - nihongo.net',
            'en' => 'J-Bridge - Foreign Talent Matching - nihongo.net'
        ),
        'contact_title' => array(
            'jp' => 'お問い合わせ - nihongo.net',
            'en' => 'Contact Us - nihongo.net'
        ),
        'news_title' => array(
            'jp' => 'ニュース・ブログ - nihongo.net',
            'en' => 'News & Blog - nihongo.net'
        ),
        'voices_title' => array(
            'jp' => '受講生の声 - nihongo.net',
            'en' => 'Student Voices - nihongo.net'
        ),
        
        // Navigation
        'nav_home' => array(
            'jp' => 'ホーム',
            'en' => 'Home'
        ),
        'nav_courses' => array(
            'jp' => 'コース',
            'en' => 'Courses'
        ),
        'nav_j_bridge' => array(
            'jp' => 'J-Bridge',
            'en' => 'J-Bridge'
        ),
        'nav_contact' => array(
            'jp' => 'お問い合わせ',
            'en' => 'Contact'
        ),
        'nav_news' => array(
            'jp' => 'ニュース',
            'en' => 'News'
        ),
        'nav_voices' => array(
            'jp' => '受講生の声',
            'en' => 'Student Voices'
        ),
        
        // Hero sections
        'hero_title' => array(
            'jp' => '日本語で素晴らしいあなたを創る',
            'en' => 'Create a wonderful you through Japanese'
        ),
        'hero_subtitle' => array(
            'jp' => 'オンライン × 個別最適化 × 文化理解を通じて新しいあなたを創る日本語学習サービス',
            'en' => 'A Japanese learning service that creates a new you through online × individual optimization × cultural understanding'
        ),
        'apply_counseling' => array(
            'jp' => 'カウンセリング申込',
            'en' => 'Apply for Counseling'
        ),
        'free_counseling' => array(
            'jp' => '無料カウンセリング',
            'en' => 'Free Counseling'
        ),
        'view_courses' => array(
            'jp' => 'コースを見る',
            'en' => 'View Courses'
        ),
        
        // About section
        'about_title' => array(
            'jp' => 'nihongo.netとは？',
            'en' => 'What is nihongo.net?'
        ),
        'about_description' => array(
            'jp' => '創刊以来40年の実績を持つひらがなタイムズのコンテンツを使用し、個別最適化された指導で日本語学習をサポートします。文化理解を含む包括的な学習を通じて、あなたの日本語力を次のレベルへと導きます。',
            'en' => 'We support Japanese language learning through individually optimized instruction using content from Hiragana Times, which has a 40-year track record since its founding. Through comprehensive learning that includes cultural understanding, we guide your Japanese language skills to the next level.'
        ),
        
        // Features
        'features_title' => array(
            'jp' => '私たちの日本語コースの特徴',
            'en' => 'Features of Our Japanese Courses'
        ),
        'features_subtitle' => array(
            'jp' => '安心、楽しさ、知識の拡大を重視する学習環境',
            'en' => 'A learning environment that values safety, enjoyment, and expanding knowledge'
        ),
        
        // Feature panels
        'feature_1_title' => array(
            'jp' => '① 40年の伝統があなたの味方！',
            'en' => '① 40 Years of Tradition on Your Side!'
        ),
        'feature_1_text' => array(
            'jp' => '私たちの教材の基盤は「ひらがなタイムズ」です。創刊以来40年の信頼と豊富な記事があなたの日本語力を後押しします。歴史ある雑誌が今、あなたの教科書に変身します！',
            'en' => 'The foundation of our materials is "Hiragana Times." 40 years of trust since its founding and abundant articles will boost your Japanese language skills. A magazine with history now transforms into your textbook!'
        ),
        'feature_2_title' => array(
            'jp' => '② あなただけのオーダーメイドクラス',
            'en' => '② Your Exclusive, Custom-Made Classes'
        ),
        'feature_2_text' => array(
            'jp' => '「大人数クラスで取り残される心配はありません...」少人数クラス＆カスタムサポートで、まるでパーソナルトレーナーがいるような感覚です。あなたの弱点と強みをすべて明確に見ることができます。',
            'en' => 'No worries about "getting lost in large classes..." Small class sizes & custom support mean it\'s like having a personal trainer. We can see all your weaknesses and strengths clearly.'
        ),
        'feature_3_title' => array(
            'jp' => '③ 日本語だけでなく「日本での生活スキル」も教えます',
            'en' => '③ Not Just Japanese, "Japanese Living Skills" Also Taught'
        ),
        'feature_3_text' => array(
            'jp' => 'もちろん敬語の使い方も教えますが、「どこで靴を脱ぐの？」「飲み会の暗黙のルールって？」など文化的な習慣も教えます。この知識があれば、あなたに有利な内緒のコツがいくつか手に入ります！',
            'en' => 'Of course, we teach keigo usage, but also "Where do you take off your shoes?" "What are the unspoken rules of drinking parties?" and other cultural customs. With this knowledge, you\'ll have some insider tips that give you an advantage!'
        ),
        'feature_4_title' => array(
            'jp' => '④ 学習の先にはキャリアの機会が待っています',
            'en' => '④ Beyond Learning Lies Career Opportunities'
        ),
        'feature_4_text' => array(
            'jp' => '「日本語を学んで終わり」ではありません。J-Bridgeとの連携により、あなたの学習は次のステップ＝働く機会につながります。まさに「学習は未来を開くチケット」です！',
            'en' => 'It\'s not just "learn Japanese and that\'s it." Through collaboration with J-Bridge, your learning leads to the next step = working opportunities. Truly "learning is the ticket that opens your future"!'
        ),
        
        // Feature panel alt texts
        'feature_1_alt' => array(
            'jp' => 'グローバル人材発掘',
            'en' => 'Global talent discovery'
        ),
        'feature_2_alt' => array(
            'jp' => 'オンデマンド人材獲得',
            'en' => 'On-demand talent acquisition'
        ),
        'feature_3_alt' => array(
            'jp' => '日本語研修',
            'en' => 'Japanese language training'
        ),
        'feature_4_alt' => array(
            'jp' => '日本語研修',
            'en' => 'Japanese language training'
        ),
        
        // Hero and learning column image alt texts
        'hero_image_alt' => array(
            'jp' => '日本語学習の学生たち',
            'en' => 'Japanese learning students'
        ),
        'learning_column_1_alt' => array(
            'jp' => '日本語講師と学習者',
            'en' => 'Japanese instructor and learner'
        ),
        'learning_column_2_alt' => array(
            'jp' => 'カフェでの日本語学習',
            'en' => 'Japanese learning at cafe'
        ),
        'learning_column_3_alt' => array(
            'jp' => '日本の文化 夏祭り',
            'en' => 'Japanese culture summer festival'
        ),
        'learning_column_4_alt' => array(
            'jp' => 'ひらがなタイムズを読む学習者',
            'en' => 'Learner reading Hiragana Times'
        ),
        'learning_column_5_alt' => array(
            'jp' => '多国籍学習者のグループ写真',
            'en' => 'Group photo of multinational learners'
        ),

        
        
        // Testimonials
        'testimonials_title' => array(
            'jp' => '受講生の声',
            'en' => 'Student Voices'
        ),
        'testimonials_subtitle' => array(
            'jp' => '実際の受講生の体験談',
            'en' => 'Experiences from actual students'
        ),
        'view_all_voices' => array(
            'jp' => 'すべての声を見る',
            'en' => 'View All Voices'
        ),
        
        // CTA sections
        'ready_start_title' => array(
            'jp' => '今すぐ始めませんか？',
            'en' => 'Ready to Start Now?'
        ),
        'ready_start_subtitle' => array(
            'jp' => 'あなたに最適な学習プランをご提案する無料カウンセリング',
            'en' => 'Free counseling to recommend the best learning plan for you'
        ),
        
        // Common
        'learn_more' => array(
            'jp' => '詳しく見る',
            'en' => 'Learn More'
        ),
        'view_details' => array(
            'jp' => '詳細を見る',
            'en' => 'View Details'
        ),
        'read_more' => array(
            'jp' => '続きを読む',
            'en' => 'Read More'
        ),
        'view_lesson_details' => array(
            'jp' => 'レッスン詳細を見る',
            'en' => 'View Lesson Details'
        ),
        
        // Header
        'open_menu' => array(
            'jp' => 'メニューを開く',
            'en' => 'Open menu'
        ),
        'logo_alt' => array(
            'jp' => 'nihongo',
            'en' => 'nihongo'
        )
    );
}

/**
 * Handle language switching
 */
function nihongo_handle_language_switch() {
    if (isset($_GET['lang'])) {
        $lang = $_GET['lang'];
        if (in_array($lang, ['jp', 'en'])) {
            nihongo_set_language($lang);
            // Redirect back to the same page without the lang parameter
            $redirect_url = strtok($_SERVER['REQUEST_URI'], '?');
            if (isset($_GET['id'])) {
                $redirect_url .= '?id=' . $_GET['id'];
            }
            wp_redirect($redirect_url);
            exit;
        }
    }
}
add_action('init', 'nihongo_handle_language_switch');

/**
 * Custom Post Types
 */
function nihongo_register_post_types() {
    // Courses Post Type
    register_post_type('course', array(
        'labels' => array(
            'name' => __('Courses', 'nihongo'),
            'singular_name' => __('Course', 'nihongo'),
            'add_new' => __('Add New Course', 'nihongo'),
            'add_new_item' => __('Add New Course', 'nihongo'),
            'edit_item' => __('Edit Course', 'nihongo'),
            'new_item' => __('New Course', 'nihongo'),
            'view_item' => __('View Course', 'nihongo'),
            'search_items' => __('Search Courses', 'nihongo'),
            'not_found' => __('No courses found', 'nihongo'),
            'not_found_in_trash' => __('No courses found in trash', 'nihongo'),
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'courses'),
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'),
        'menu_icon' => 'dashicons-book-alt',
        'show_in_rest' => true,
    ));
    
    // Testimonials Post Type
    register_post_type('testimonial', array(
        'labels' => array(
            'name' => __('Testimonials', 'nihongo'),
            'singular_name' => __('Testimonial', 'nihongo'),
            'add_new' => __('Add New Testimonial', 'nihongo'),
            'add_new_item' => __('Add New Testimonial', 'nihongo'),
            'edit_item' => __('Edit Testimonial', 'nihongo'),
            'new_item' => __('New Testimonial', 'nihongo'),
            'view_item' => __('View Testimonial', 'nihongo'),
            'search_items' => __('Search Testimonials', 'nihongo'),
            'not_found' => __('No testimonials found', 'nihongo'),
            'not_found_in_trash' => __('No testimonials found in trash', 'nihongo'),
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'testimonials'),
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
        'menu_icon' => 'dashicons-format-quote',
        'show_in_rest' => true,
    ));
    
    // News Post Type
    register_post_type('news', array(
        'labels' => array(
            'name' => __('News', 'nihongo'),
            'singular_name' => __('News Item', 'nihongo'),
            'add_new' => __('Add New News', 'nihongo'),
            'add_new_item' => __('Add New News', 'nihongo'),
            'edit_item' => __('Edit News', 'nihongo'),
            'new_item' => __('New News', 'nihongo'),
            'view_item' => __('View News', 'nihongo'),
            'search_items' => __('Search News', 'nihongo'),
            'not_found' => __('No news found', 'nihongo'),
            'not_found_in_trash' => __('No news found in trash', 'nihongo'),
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'news'),
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'),
        'menu_icon' => 'dashicons-megaphone',
        'show_in_rest' => true,
    ));
}
add_action('init', 'nihongo_register_post_types');

/**
 * Navigation Helper Functions
 */

/**
 * Get current URL for language switching
 */
function nihongo_get_current_url() {
    global $wp;
    return home_url(add_query_arg(array(), $wp->request));
}

/**
 * Get navigation items with proper links and icons
 */
function nihongo_get_nav_items() {
    return array(
        array(
            'title' => nihongo_translate('nav_home'),
            'url' => home_url(),
            'icon' => '🏠',
            'class' => 'nav-home',
            'is_current' => is_front_page()
        ),
        array(
            'title' => nihongo_translate('nav_courses'),
            'url' => nihongo_get_page_url('courses'),
            'icon' => '📚',
            'class' => 'nav-courses',
            'is_current' => is_page('courses') || is_post_type_archive('course') || is_singular('course')
        ),
        array(
            'title' => nihongo_translate('nav_j_bridge'),
            'url' => nihongo_get_page_url('j-bridge'),
            'icon' => '🌉',
            'class' => 'nav-j-bridge',
            'is_current' => is_page('j-bridge')
        ),
        array(
            'title' => nihongo_translate('nav_contact'),
            'url' => nihongo_get_page_url('contact'),
            'icon' => '📞',
            'class' => 'nav-contact',
            'is_current' => is_page('contact')
        )
    );
}

/**
 * Get page URL by slug with fallback
 */
function nihongo_get_page_url($page_slug) {
    $page = get_page_by_path($page_slug);
    if ($page) {
        return get_permalink($page->ID);
    }
    
    // Fallback URLs for common pages
    $fallback_urls = array(
        'courses' => home_url('/courses/'),
        'j-bridge' => home_url('/j-bridge/'),
        'news' => home_url('/news/'),
        'voices' => home_url('/voices/'),
        'contact' => home_url('/contact/')
    );
    
    return isset($fallback_urls[$page_slug]) ? $fallback_urls[$page_slug] : home_url();
}

/**
 * Generate navigation HTML
 */
function nihongo_render_navigation($nav_items = null, $class = 'nav-list') {
    if ($nav_items === null) {
        $nav_items = nihongo_get_nav_items();
    }
    
    $html = '<ul class="' . esc_attr($class) . '">';
    
    foreach ($nav_items as $item) {
        $active_class = $item['is_current'] ? ' active' : '';
        $html .= '<li class="nav-item' . $active_class . '">';
        $html .= '<a href="' . esc_url($item['url']) . '" class="nav-link ' . esc_attr($item['class']) . '">';
        $html .= '<span class="nav-icon">' . esc_html($item['icon']) . '</span>';
        $html .= '<span class="nav-text">' . esc_html($item['title']) . '</span>';
        $html .= '</a>';
        $html .= '</li>';
    }
    
    $html .= '</ul>';
    
    return $html;
}

/**
 * Breadcrumb Navigation
 */
function nihongo_breadcrumbs() {
    if (is_front_page()) {
        return;
    }
    
    $breadcrumbs = array();
    $breadcrumbs[] = array(
        'title' => nihongo_translate('nav_home'),
        'url' => home_url()
    );
    
    if (is_page()) {
        $page = get_post();
        $ancestors = get_post_ancestors($page->ID);
        
        if ($ancestors) {
            $ancestors = array_reverse($ancestors);
            foreach ($ancestors as $ancestor) {
                $breadcrumbs[] = array(
                    'title' => get_the_title($ancestor),
                    'url' => get_permalink($ancestor)
                );
            }
        }
        
        $breadcrumbs[] = array(
            'title' => get_the_title(),
            'url' => get_permalink()
        );
    } elseif (is_singular('course')) {
        $breadcrumbs[] = array(
            'title' => nihongo_translate('nav_courses'),
            'url' => nihongo_get_page_url('courses')
        );
        $breadcrumbs[] = array(
            'title' => get_the_title(),
            'url' => get_permalink()
        );
    } elseif (is_singular('testimonial')) {
        $breadcrumbs[] = array(
            'title' => nihongo_translate('nav_voices'),
            'url' => nihongo_get_page_url('voices')
        );
        $breadcrumbs[] = array(
            'title' => get_the_title(),
            'url' => get_permalink()
        );
    } elseif (is_singular('news')) {
        $breadcrumbs[] = array(
            'title' => nihongo_translate('nav_news'),
            'url' => nihongo_get_page_url('news')
        );
        $breadcrumbs[] = array(
            'title' => get_the_title(),
            'url' => get_permalink()
        );
    } elseif (is_post_type_archive('course')) {
        $breadcrumbs[] = array(
            'title' => nihongo_translate('nav_courses'),
            'url' => nihongo_get_page_url('courses')
        );
    } elseif (is_post_type_archive('testimonial')) {
        $breadcrumbs[] = array(
            'title' => nihongo_translate('nav_voices'),
            'url' => nihongo_get_page_url('voices')
        );
    } elseif (is_post_type_archive('news')) {
        $breadcrumbs[] = array(
            'title' => nihongo_translate('nav_news'),
            'url' => nihongo_get_page_url('news')
        );
    }
    
    if (count($breadcrumbs) <= 1) {
        return;
    }
    
    $html = '<nav class="breadcrumbs" aria-label="Breadcrumb">';
    $html .= '<ol class="breadcrumb-list">';
    
    foreach ($breadcrumbs as $index => $breadcrumb) {
        $is_last = ($index === count($breadcrumbs) - 1);
        
        $html .= '<li class="breadcrumb-item' . ($is_last ? ' current' : '') . '">';
        
        if ($is_last) {
            $html .= '<span class="breadcrumb-current">' . esc_html($breadcrumb['title']) . '</span>';
        } else {
            $html .= '<a href="' . esc_url($breadcrumb['url']) . '" class="breadcrumb-link">' . esc_html($breadcrumb['title']) . '</a>';
        }
        
        $html .= '</li>';
        
        if (!$is_last) {
            $html .= '<li class="breadcrumb-separator" aria-hidden="true">›</li>';
        }
    }
    
    $html .= '</ol>';
    $html .= '</nav>';
    
    return $html;
}

/**
 * Add custom body classes
 */
function nihongo_body_classes($classes) {
    $classes[] = 'nihongo-theme';
    $classes[] = 'lang-' . nihongo_get_current_language();
    
    if (is_front_page()) {
        $classes[] = 'page-bg';
    }
    
    return $classes;
}
add_filter('body_class', 'nihongo_body_classes');

/**
 * Theme activation hook
 */
function nihongo_theme_activation() {
    // Flush rewrite rules
    flush_rewrite_rules();
    
    // Create default pages
    $pages = array(
        'Home' => 'home.php',
        'Courses' => 'courses.php',
        'J-Bridge' => 'j-bridge.php',
        'Contact' => 'contact.php',
        'News' => 'news.php',
        'Voices' => 'voices.php'
    );
    
    foreach ($pages as $title => $template) {
        $page = get_page_by_title($title);
        if (!$page) {
            wp_insert_post(array(
                'post_title' => $title,
                'post_name' => strtolower(str_replace(' ', '-', $title)),
                'post_content' => '',
                'post_status' => 'publish',
                'post_type' => 'page',
                'meta_input' => array(
                    '_wp_page_template' => $template
                )
            ));
        }
    }
}
add_action('after_switch_theme', 'nihongo_theme_activation');
