<?php
/**
 * DIMARK — functions.php
 *
 * Tema WordPress nativo generado automáticamente por SuperCerebro.
 * Estructura modular con template-parts, Customizer dinámico.
 */

// ============================================================================
// SETUP
// ============================================================================

function dimark_theme_setup() {
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_theme_support('html5', ['search-form','comment-form','comment-list','gallery','caption']);
    add_theme_support('automatic-feed-links');
    add_theme_support('custom-logo', ['height' => 60, 'width' => 60, 'flex-height' => true, 'flex-width' => true]);
}
add_action('after_setup_theme', 'dimark_theme_setup');

function dimark_theme_hide_admin_bar() {
    if (!current_user_can('administrator')) show_admin_bar(false);
}
add_action('after_setup_theme', 'dimark_theme_hide_admin_bar');

// ============================================================================
// ENQUEUE ASSETS
// ============================================================================

function dimark_theme_enqueue_assets() {
    $theme_uri  = get_template_directory_uri();
    $theme_path = get_template_directory();

    wp_enqueue_style('dimark_theme-main', get_stylesheet_uri(), [],
        file_exists($theme_path . '/style.css') ? filemtime($theme_path . '/style.css') : '1.0');

    // Lottie library (solo si hay archivos lottie en el tema)
    if (is_dir($theme_path . '/assets/lottie')) {
        wp_enqueue_script('lottie-web',
            'https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.12.2/lottie.min.js',
            [], '5.12.2', true);
    }

    if (file_exists($theme_path . '/assets/js/main.js')) {
        $deps = is_dir($theme_path . '/assets/lottie') ? ['lottie-web'] : [];
        wp_enqueue_script('dimark_theme-main', $theme_uri . '/assets/js/main.js',
            $deps, filemtime($theme_path . '/assets/js/main.js'), true);
        wp_localize_script('dimark_theme-main', 'THEME_CONFIG', ['themeUrl' => $theme_uri]);
    }
}
add_action('wp_enqueue_scripts', 'dimark_theme_enqueue_assets');

// ============================================================================
// ADMIN BAR FIX
// ============================================================================

function dimark_theme_admin_bar_fix() {
    if (!is_admin_bar_showing()) return;
    echo '<style>'
        . 'body.admin-bar .nav{top:32px!important;}'
        . '@media screen and (max-width:782px){body.admin-bar .nav{top:46px!important;}}'
        . '@media screen and (max-width:600px){body.admin-bar .nav{top:0!important;}}'
        . '</style>';
}
add_action('wp_head', 'dimark_theme_admin_bar_fix', 9999);

// ============================================================================
// CUSTOMIZER — Campos auto-detectados
// ============================================================================

function dimark_theme_customize_register($wp_customize) {
    $wp_customize->add_panel('dimark_theme_panel', [
        'title'       => 'DIMARK',
        'description' => 'Edita teléfonos, precios y datos de contacto.',
        'priority'    => 10,
    ]);
    $wp_customize->add_section('dimark_theme_contact', ['title' => 'Contacto', 'panel' => 'dimark_theme_panel']);
    $wp_customize->add_section('dimark_theme_prices',  ['title' => 'Precios',  'panel' => 'dimark_theme_panel']);

    $wp_customize->add_setting('dimark_theme_phone_0', ['default' => '573203920599', 'sanitize_callback' => 'sanitize_text_field']);
    $wp_customize->add_setting('dimark_theme_email_0', ['default' => 'info@dimark.co', 'sanitize_callback' => 'sanitize_email']);
    $wp_customize->add_setting('dimark_theme_instagram', ['default' => 'https://www.instagram.com/dimark.co', 'sanitize_callback' => 'esc_url_raw']);
    $wp_customize->add_setting('dimark_theme_facebook', ['default' => 'https://www.facebook.com/profile.php', 'sanitize_callback' => 'esc_url_raw']);
    $wp_customize->add_setting('dimark_theme_tiktok', ['default' => 'https://www.tiktok.com/@dimark.co', 'sanitize_callback' => 'esc_url_raw']);

    $wp_customize->add_control('dimark_theme_phone_0', ['label' => 'WhatsApp / Principal', 'description' => 'Solo dígitos con código país', 'section' => 'dimark_theme_contact', 'type' => 'text']);
    $wp_customize->add_control('dimark_theme_email_0', ['label' => 'Email principal', 'section' => 'dimark_theme_contact', 'type' => 'email']);
    $wp_customize->add_control('dimark_theme_instagram', ['label' => 'Instagram', 'section' => 'dimark_theme_contact', 'type' => 'url']);
    $wp_customize->add_control('dimark_theme_facebook', ['label' => 'Facebook', 'section' => 'dimark_theme_contact', 'type' => 'url']);
    $wp_customize->add_control('dimark_theme_tiktok', ['label' => 'Tiktok', 'section' => 'dimark_theme_contact', 'type' => 'url']);
}
add_action('customize_register', 'dimark_theme_customize_register');

// ============================================================================
// HELPER: formato teléfono colombiano (573XXXXXXXXX → +57 3XX XXX XXXX)
// ============================================================================

function dimark_theme_format_phone($num) {
    $n = preg_replace('/[^0-9]/', '', $num);
    if (strlen($n) === 12 && substr($n, 0, 2) === '57') {
        return '+57 ' . substr($n, 2, 3) . ' ' . substr($n, 5, 3) . ' ' . substr($n, 8, 4);
    }
    return '+' . $n;
}
