<?php
/**
 * Media Interviews System Theme
 * Dashboard-first WordPress theme for TechCon Agenda Pro + TechCon Booking Pro.
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

define( 'TECHCON_THEME_VERSION', '10.0.0' );

function techcon_theme_setup() {
    add_theme_support( 'automatic-feed-links' );
    add_theme_support( 'title-tag' );
    add_theme_support( 'post-thumbnails' );
    add_theme_support( 'custom-logo', [ 'height' => 96, 'width' => 320, 'flex-height' => true, 'flex-width' => true ] );
    add_theme_support( 'html5', [ 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script' ] );
    register_nav_menus( [
        'menu-1' => esc_html__( 'System Menu', 'techcon-theme' ),
        'footer' => esc_html__( 'Footer Menu', 'techcon-theme' ),
    ] );
}
add_action( 'after_setup_theme', 'techcon_theme_setup' );

function techcon_theme_default( string $key ) {
    $defaults = [
        'system_title'       => 'Media Interviews',
        'brand_name'         => 'TechCon',
        'event_name'         => 'TechCon SoCal 2026',
        'event_tagline'      => 'A private dashboard system for speaker and media interview coordination',
        'event_dates'        => 'May 21–23, 2026',
        'event_venue'        => 'San Diego State University',
        'support_email'      => 'support@techconglobal.com',
        'primary_color'      => '#e61a84',
        'secondary_color'    => '#4e24c0',
        'accent_color'       => '#7b5cff',
        'background_color'   => '#070716',
        'card_color'         => '#120820',
        'show_header'        => true,
        'show_footer'        => true,
        'portal_mode'        => true,
        'auto_redirect_home' => true,
        'agenda_url'         => '/agenda/',
        'booking_url'        => '/booking/',
        'speaker_login_url'  => '/speaker-login/',
        'partner_login_url'  => '/partner-login/',
        'speaker_dash_url'   => '/speaker-dashboard/',
        'partner_dash_url'   => '/partner-dashboard/',
        'logo_url'           => 'https://media.techconglobal.com/wp-content/uploads/2026/05/techcon-main-logo.png',
    ];
    return $defaults[ $key ] ?? '';
}

function techcon_theme_opt( string $key ) {
    return get_theme_mod( 'techcon_' . $key, techcon_theme_default( $key ) );
}

function techcon_theme_sanitize_bool( $value ) { return (bool) $value; }
function techcon_theme_sanitize_path( $value ) {
    $value = trim( (string) $value );
    if ( $value === '' ) return '';
    if ( preg_match( '#^https?://#i', $value ) ) return esc_url_raw( $value );
    return '/' . ltrim( sanitize_text_field( $value ), '/' );
}

function techcon_theme_customize_register( WP_Customize_Manager $wp_customize ) {
    $wp_customize->add_panel( 'techcon_branding_panel', [
        'title'       => __( 'Media Interviews System', 'techcon-theme' ),
        'description' => __( 'Control the dashboard system title, current event branding, portal links, and colors.', 'techcon-theme' ),
        'priority'    => 30,
    ] );

    $wp_customize->add_section( 'techcon_event_section', [
        'title' => __( 'System & Event Branding', 'techcon-theme' ),
        'panel' => 'techcon_branding_panel',
    ] );

    $text_settings = [
        'system_title'  => 'System Title',
        'brand_name'    => 'Parent Brand Name',
        'event_name'    => 'Current Event Name',
        'event_tagline' => 'System Subtitle',
        'event_dates'   => 'Event Dates',
        'event_venue'   => 'Event Venue',
        'support_email' => 'Support Email',
        'logo_url'      => 'Logo URL',
    ];
    foreach ( $text_settings as $key => $label ) {
        $wp_customize->add_setting( 'techcon_' . $key, [
            'default'           => techcon_theme_default( $key ),
            'sanitize_callback' => $key === 'support_email' ? 'sanitize_email' : 'sanitize_text_field',
        ] );
        $wp_customize->add_control( 'techcon_' . $key, [
            'label'   => __( $label, 'techcon-theme' ),
            'section' => 'techcon_event_section',
            'type'    => 'text',
        ] );
    }

    $wp_customize->add_section( 'techcon_colors_section', [
        'title' => __( 'System Colors', 'techcon-theme' ),
        'panel' => 'techcon_branding_panel',
    ] );
    $colors = [
        'primary_color'    => 'Primary Event Color',
        'secondary_color'  => 'Secondary Color',
        'accent_color'     => 'Accent Color',
        'background_color' => 'System Background',
        'card_color'       => 'Card Background',
    ];
    foreach ( $colors as $key => $label ) {
        $wp_customize->add_setting( 'techcon_' . $key, [
            'default'           => techcon_theme_default( $key ),
            'sanitize_callback' => 'sanitize_hex_color',
        ] );
        $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'techcon_' . $key, [
            'label'   => __( $label, 'techcon-theme' ),
            'section' => 'techcon_colors_section',
        ] ) );
    }

    $wp_customize->add_section( 'techcon_layout_section', [
        'title' => __( 'Dashboard Layout & Routes', 'techcon-theme' ),
        'panel' => 'techcon_branding_panel',
    ] );
    foreach ( [
        'show_header'        => 'Show System Header',
        'show_footer'        => 'Show System Footer',
        'portal_mode'        => 'Portal/System Mode Styling',
        'auto_redirect_home' => 'Redirect logged-in portal users from homepage to their dashboard',
    ] as $key => $label ) {
        $wp_customize->add_setting( 'techcon_' . $key, [
            'default'           => techcon_theme_default( $key ),
            'sanitize_callback' => 'techcon_theme_sanitize_bool',
        ] );
        $wp_customize->add_control( 'techcon_' . $key, [
            'label'   => __( $label, 'techcon-theme' ),
            'section' => 'techcon_layout_section',
            'type'    => 'checkbox',
        ] );
    }
    foreach ( [
        'agenda_url'        => 'Agenda URL',
        'booking_url'       => 'Media Booking URL',
        'speaker_login_url' => 'Speaker Login URL',
        'partner_login_url' => 'Media Partner Login URL',
        'speaker_dash_url'  => 'Speaker Dashboard URL',
        'partner_dash_url'  => 'Media Partner Dashboard URL',
    ] as $key => $label ) {
        $wp_customize->add_setting( 'techcon_' . $key, [
            'default'           => techcon_theme_default( $key ),
            'sanitize_callback' => 'techcon_theme_sanitize_path',
        ] );
        $wp_customize->add_control( 'techcon_' . $key, [
            'label'       => __( $label, 'techcon-theme' ),
            'description' => __( 'Use a relative path like /speaker-login/ or a full URL.', 'techcon-theme' ),
            'section'     => 'techcon_layout_section',
            'type'        => 'text',
        ] );
    }
}
add_action( 'customize_register', 'techcon_theme_customize_register' );



function techcon_theme_booking_mode(): string {
    return get_option( 'tcb_booking_mode', 'simple' ) === 'advanced' ? 'advanced' : 'simple';
}

function techcon_theme_agenda_enabled(): bool {
    return get_option( 'tca_enable_agenda_pages', 'yes' ) === 'yes';
}

function techcon_theme_url( string $key ) {
    $url = (string) techcon_theme_opt( $key );
    if ( $url === '' ) return home_url( '/' );
    if ( preg_match( '#^https?://#i', $url ) ) return esc_url( $url );
    return esc_url( home_url( $url ) );
}

function techcon_theme_user_has_role( string $role ) {
    if ( ! is_user_logged_in() ) return false;
    $user = wp_get_current_user();
    return in_array( $role, (array) $user->roles, true );
}

function techcon_theme_is_plugin_virtual_path() {
    $path = trim( (string) parse_url( $_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH ), '/' );
    $home_path = trim( (string) parse_url( home_url( '/' ), PHP_URL_PATH ), '/' );
    if ( $home_path && strpos( $path, $home_path . '/' ) === 0 ) {
        $path = substr( $path, strlen( $home_path ) + 1 );
    }
    $path = trim( $path, '/' );
    return in_array( $path, [ 'agenda', 'booking', 'speaker-login', 'partner-login', 'speaker-dashboard', 'partner-dashboard' ], true )
        || preg_match( '#^(book|speaker|partner)/[^/]+/?$#', $path );
}

function techcon_theme_home_redirect_for_portal_users() {
    if ( is_admin() || techcon_theme_is_plugin_virtual_path() || ! is_front_page() || ! techcon_theme_opt( 'auto_redirect_home' ) || ! is_user_logged_in() ) {
        return;
    }
    if ( techcon_theme_user_has_role( 'tc_speaker' ) ) {
        wp_safe_redirect( techcon_theme_url( 'speaker_dash_url' ) );
        exit;
    }
    if ( techcon_theme_user_has_role( 'tc_partner' ) ) {
        wp_safe_redirect( techcon_theme_url( 'partner_dash_url' ) );
        exit;
    }
}
add_action( 'template_redirect', 'techcon_theme_home_redirect_for_portal_users', 2 );

function techcon_theme_scripts() {
    wp_enqueue_style( 'techcon-style', get_stylesheet_uri(), [], TECHCON_THEME_VERSION );

    $css = sprintf(
        ':root{--tc-pink:%1$s;--tc-purple:%2$s;--tc-accent:%3$s;--tc-bg:%4$s;--tc-card-base:%5$s;--tc-glow:%1$s66;}body{--tc-event-name:"%6$s";--tc-system-title:"%7$s";}',
        esc_attr( techcon_theme_opt( 'primary_color' ) ),
        esc_attr( techcon_theme_opt( 'secondary_color' ) ),
        esc_attr( techcon_theme_opt( 'accent_color' ) ),
        esc_attr( techcon_theme_opt( 'background_color' ) ),
        esc_attr( techcon_theme_opt( 'card_color' ) ),
        esc_attr( techcon_theme_opt( 'event_name' ) ),
        esc_attr( techcon_theme_opt( 'system_title' ) )
    );
    wp_add_inline_style( 'techcon-style', $css );

    wp_enqueue_script( 'jquery' );
    wp_add_inline_script( 'jquery', 'document.addEventListener("DOMContentLoaded",function(){var t=document.querySelector(".menu-toggle"),n=document.querySelector(".main-navigation");if(t&&n){t.addEventListener("click",function(){n.classList.toggle("toggled");t.classList.toggle("is-active");});}document.querySelectorAll("[data-tc-back]").forEach(function(b){b.addEventListener("click",function(e){e.preventDefault();if(window.history.length>1){window.history.back();}else{window.location.href="/";}});});});' );
}
add_action( 'wp_enqueue_scripts', 'techcon_theme_scripts' );

function techcon_body_classes( $classes ) {
    $classes[] = 'techcon-dark-theme';
    $classes[] = 'media-interviews-system';
    if ( techcon_theme_opt( 'portal_mode' ) ) $classes[] = 'techcon-system-mode';
    if ( ! techcon_theme_opt( 'show_header' ) ) $classes[] = 'tc-header-hidden';
    if ( ! techcon_theme_opt( 'show_footer' ) ) $classes[] = 'tc-footer-hidden';
    return $classes;
}
add_filter( 'body_class', 'techcon_body_classes' );

function techcon_theme_document_title_parts( $title ) {
    if ( is_front_page() ) {
        $title['title'] = techcon_theme_opt( 'system_title' );
    }
    return $title;
}
add_filter( 'document_title_parts', 'techcon_theme_document_title_parts' );
