<?php
/**
 * WakemUp Productions - Professional Master Functions
 * Optimized for Stability, Performance, and Aesthetic Consistency.
 */

// 1. CORE SYSTEM SUPPORT
function wakemup_setup() {
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_theme_support('woocommerce'); 
    add_theme_support('wc-product-gallery-zoom');
    add_theme_support('wc-product-gallery-lightbox');
    add_theme_support('wc-product-gallery-slider');
    add_theme_support('html5', array('search-form', 'comment-form', 'comment-list', 'gallery', 'caption'));
    
    register_nav_menus(array(
        'primary' => __('Primary Menu', 'wakemup'),
    ));
}
add_action('after_setup_theme', 'wakemup_setup');

// 2. ASSET INJECTION & BRAND LOGIC
function wakemup_scripts() {
    wp_enqueue_style('wakemup-fonts', 'https://fonts.googleapis.com/css2?family=Inter:wght@400;700;900&family=Space+Grotesk:wght@500;700&display=swap', array(), null);
    wp_enqueue_script('tailwind-cdn', 'https://cdn.tailwindcss.com', array(), null, false);
    wp_enqueue_style('wakemup-style', get_stylesheet_uri());

    // BRAND CONFIGURATION
    wp_add_inline_script('tailwind-cdn', "
        tailwind.config = {
            theme: {
                extend: {
                    colors: { 
                        background: '#0a0a0a', 
                        foreground: '#ffffff', 
                        primary: '#2D1B4D', 
                        accent: '#E63946', 
                        surface: '#111111' 
                    },
                    fontFamily: { heading: ['Space Grotesk', 'sans-serif'], sans: ['Inter', 'sans-serif'] }
                }
            }
        }
    ");

    // STRESS-TESTED AUDIO ENGINE
    wp_add_inline_script('wakemup-style', "
        window.wakemupAudio = new Audio();
        let currentBtn = null;

        const PLAY_SVG = '<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"currentColor\"><polygon points=\"5 3 19 12 5 21 5 3\"/></svg>';
        const PAUSE_SVG = '<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"currentColor\"><rect x=\"6\" y=\"4\" width=\"4\" height=\"16\"/><rect x=\"14\" y=\"4\" width=\"4\" height=\"16\"/></svg>';

        window.togglePlayback = function(url, btnId) {
            const btn = document.getElementById(btnId);
            if (!btn) return;

            if (window.wakemupAudio.src.includes(url)) {
                if (window.wakemupAudio.paused) {
                    window.wakemupAudio.play().catch(e => console.error('Audio Playback Interrupted:', e));
                    btn.innerHTML = PAUSE_SVG;
                } else {
                    window.wakemupAudio.pause();
                    btn.innerHTML = PLAY_SVG;
                }
            } else {
                if (currentBtn) currentBtn.innerHTML = PLAY_SVG;
                window.wakemupAudio.src = url;
                window.wakemupAudio.play().catch(e => console.error('Audio Source Error:', e));
                btn.innerHTML = PAUSE_SVG;
                currentBtn = btn;
            }
        };

        window.wakemupAudio.onended = () => { if(currentBtn) currentBtn.innerHTML = PLAY_SVG; };
    ");
}
add_action('wp_enqueue_scripts', 'wakemup_scripts');

// 3. THE VAULT SHORTCODE [wakemup_vault]
function render_wakemup_vault() {
    ob_start(); ?>
    <style>
        .v-row { background: #0f0f0f; border: 1px solid rgba(255,255,255,0.03); transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1); }
        .v-row:hover { background: #141414; border-color: #2D1B4D; transform: translateX(5px); }
        .p-btn { background: #2D1B4D; color: #fff; font-weight: 900; letter-spacing: 0.1em; transition: 0.3s ease; }
        .p-btn:hover { background: #E63946; transform: scale(1.02); }
    </style>
    
    <div id="wakemup-vault" class="max-w-6xl mx-auto space-y-3 py-12">
        <div class="v-row p-5 flex items-center justify-between rounded-lg">
            <div class="flex items-center gap-6">
                <button id="p-btn-01" onclick="togglePlayback('https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3', 'p-btn-01')" class="text-white hover:text-[#2D1B4D] transition-colors">
                    <svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 24 24" fill="currentColor"><polygon points="5 3 19 12 5 21 5 3"/></svg>
                </button>
                <div>
                    <h3 class="text-lg font-black uppercase italic tracking-tighter text-white">NIGHTMARE PROTOCOL</h3>
                    <p class="text-[9px] uppercase tracking-[0.3em] text-gray-500 font-bold">140 BPM • TRAP • PROD. IM4K</p>
                </div>
            </div>
            <div class="flex items-center gap-8">
                <div class="text-right">
                    <p class="text-[8px] font-bold text-gray-600 uppercase tracking-widest">Lease Starting At</p>
                    <p class="text-xl font-black text-white italic">$25.00</p>
                </div>
                <button class="p-btn px-10 py-4 text-[10px] uppercase rounded-sm">ADD TO CART</button>
            </div>
        </div>
    </div>
    <?php
    return ob_get_clean();
}
add_shortcode('wakemup_vault', 'render_wakemup_vault');

// 4. POST TYPES & PAGE LOGIC
function wakemup_register_post_types() {
    register_post_type('beat', array(
        'labels' => array('name' => 'Beats', 'singular_name' => 'Beat'),
        'public' => true,
        'has_archive' => true,
        'menu_icon' => 'dashicons-format-audio',
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
    ));
    register_post_type('sound_kit', array(
        'labels' => array('name' => 'Sample Kits', 'singular_name' => 'Sample Kit'),
        'public' => true,
        'has_archive' => true,
        'menu_icon' => 'dashicons-archive',
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
    ));
}
add_action('init', 'wakemup_register_post_types');