/*
Theme Name: Ewebot - Child
Author: GT3themes
Author URI: http://gt3themes.com
Description: -
Version: 1.0
Template: ewebot
*/
@import url("../ewebot/style.css");

<?php

function create_expert_post_type() {
    $labels = array(
        'name'               => 'Experts',
        'singular_name'      => 'Expert',
        'menu_name'          => 'Experts',
        'add_new'            => 'Add New Expert',
        'add_new_item'       => 'Add New Expert',
        'edit_item'          => 'Edit Expert',
        'new_item'           => 'New Expert',
        'view_item'          => 'View Expert',
        'search_items'       => 'Search Experts',
        'not_found'          => 'No experts found',
        'not_found_in_trash' => 'No experts found in trash'
    );

    $args = array(
        'labels'              => $labels,
        'public'              => true,
        'publicly_queryable'  => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'query_var'           => true,
        'rewrite'             => array('slug' => 'expert'),
        'capability_type'     => 'post',
        'has_archive'         => true,
        'hierarchical'        => false,
        'menu_position'       => 5,
        'menu_icon'           => 'dashicons-businessperson',
        'supports'            => array('title', 'editor', 'thumbnail', 'excerpt'),
        'show_in_rest'        => true
    );

    register_post_type('expert', $args);
}
add_action('init', 'create_expert_post_type');

// ===========================================
// STEP 2: ACF Fields Setup (JSON Export)
// Import this JSON in ACF > Tools > Import Field Groups
// ===========================================

/*
{
    "key": "group_expert_fields",
    "title": "Expert Profile Fields",
    "fields": [
        {
            "key": "field_expert_image",
            "label": "Expert Image",
            "name": "expert_image",
            "type": "image",
            "required": 0,
            "return_format": "array",
            "preview_size": "medium"
        },
        {
            "key": "field_expert_title",
            "label": "Expert Title\/Name",
            "name": "expert_title",
            "type": "text",
            "required": 1,
            "default_value": "",
            "placeholder": "e.g., Mantu Kumar"
        },
        {
            "key": "field_expertise",
            "label": "Expertise",
            "name": "expertise",
            "type": "text",
            "required": 1,
            "placeholder": "e.g., Graphics Designer"
        },
        {
            "key": "field_short_bio",
            "label": "Short Bio",
            "name": "short_bio",
            "type": "textarea",
            "rows": 3,
            "placeholder": "Brief introduction about the expert"
        },
        {
            "key": "field_education",
            "label": "Education",
            "name": "education",
            "type": "text",
            "placeholder": "e.g., B.Tech"
        },
        {
            "key": "field_achievements",
            "label": "Achievements and Awards",
            "name": "achievements",
            "type": "textarea",
            "rows": 3
        },
        {
            "key": "field_consultation_fee",
            "label": "Hourly Consultation Fee",
            "name": "consultation_fee",
            "type": "number",
            "required": 1,
            "min": 0,
            "step": 1,
            "prepend": "₹"
        },
        {
            "key": "field_detailed_profile",
            "label": "Detailed Profile",
            "name": "detailed_profile",
            "type": "wysiwyg",
            "toolbar": "full",
            "media_upload": 1
        }
    ],
    "location": [
        [
            {
                "param": "post_type",
                "operator": "==",
                "value": "expert"
            }
        ]
    ],
    "menu_order": 0,
    "position": "normal",
    "style": "default",
    "label_placement": "top",
    "instruction_placement": "label"
}
*/

// ===========================================
// STEP 3: Single Expert Template
// Create: single-expert.php in your theme folder
// ===========================================

function get_expert_template() {
    return '
<?php get_header(); ?>

<style>
/* Add the CSS from previous design here */
.expert-profile {
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    line-height: 1.6;
    color: #333;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    padding: 20px 0;
}

.expert-container {
    max-width: 1200px;
    margin: 0 auto;
    background: white;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.1);
    overflow: hidden;
}

.expert-hero {
    background: linear-gradient(135deg, #2196F3 0%, #21CBF3 100%);
    padding: 60px 40px;
    text-align: center;
    position: relative;
    overflow: hidden;
}

.expert-profile-image {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    margin: 0 auto 20px;
    overflow: hidden;
    border: 5px solid rgba(255,255,255,0.2);
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}

.expert-profile-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.expert-default-avatar {
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 60px;
    color: white;
    font-weight: bold;
}

.expert-hero h1 {
    color: white;
    font-size: 2.5rem;
    margin-bottom: 10px;
}

.expert-expertise-badge {
    background: rgba(255,255,255,0.2);
    color: white;
    padding: 8px 20px;
    border-radius: 25px;
    display: inline-block;
    font-weight: 500;
    backdrop-filter: blur(10px);
}

.expert-content {
    padding: 40px;
}

.expert-info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 30px;
    margin-bottom: 40px;
}

.expert-info-card {
    background: #f8f9fa;
    padding: 25px;
    border-radius: 15px;
    border-left: 4px solid #2196F3;
    transition: transform 0.3s ease;
}

.expert-info-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 30px rgba(33, 150, 243, 0.1);
}

.expert-info-card h3 {
    color: #2196F3;
    font-size: 1.2rem;
    margin-bottom: 15px;
}

.expert-bio-section {
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    padding: 30px;
    border-radius: 15px;
    margin: 30px 0;
}

.expert-consultation-fee {
    background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
    color: white;
    padding: 15px 25px;
    border-radius: 10px;
    text-align: center;
    font-size: 1.2rem;
    font-weight: bold;
    margin: 20px 0;
}

.expert-detailed-profile {
    background: white;
    padding: 30px;
    border-radius: 15px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.08);
}

.expert-contact-btn {
    background: linear-gradient(135deg, #2196F3 0%, #21CBF3 100%);
    color: white;
    padding: 15px 30px;
    border: none;
    border-radius: 25px;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.3s ease;
    display: inline-block;
    text-decoration: none;
    margin-top: 20px;
}

@media (max-width: 768px) {
    .expert-hero {
        padding: 40px 20px;
    }
    
    .expert-hero h1 {
        font-size: 2rem;
    }
    
    .expert-content {
        padding: 20px;
    }
    
    .expert-info-grid {
        grid-template-columns: 1fr;
        gap: 20px;
    }
}
</style>

<div class="expert-profile">
    <?php while (have_posts()) : the_post(); ?>
        
        <?php
        // Get ACF fields
        $expert_image = get_field("expert_image");
        $expert_title = get_field("expert_title") ?: get_the_title();
        $expertise = get_field("expertise");
        $short_bio = get_field("short_bio");
        $education = get_field("education");
        $achievements = get_field("achievements");
        $consultation_fee = get_field("consultation_fee");
        $detailed_profile = get_field("detailed_profile");
        ?>
        
        <div class="expert-container">
            <!-- Hero Section -->
            <div class="expert-hero">
                <div class="expert-profile-image">
                    <?php if ($expert_image): ?>
                        <img src="<?php echo esc_url($expert_image["url"]); ?>" alt="<?php echo esc_attr($expert_title); ?>">
                    <?php else: ?>
                        <?php 
                        $initials = "";
                        $name_parts = explode(" ", $expert_title);
                        foreach ($name_parts as $part) {
                            $initials .= strtoupper(substr($part, 0, 1));
                        }
                        ?>
                        <div class="expert-default-avatar"><?php echo $initials; ?></div>
                    <?php endif; ?>
                </div>
                <div class="expert-hero-content">
                    <h1><?php echo esc_html($expert_title); ?></h1>
                    <?php if ($expertise): ?>
                        <div class="expert-expertise-badge"><?php echo esc_html($expertise); ?></div>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Content Section -->
            <div class="expert-content">
                <!-- Info Grid -->
                <div class="expert-info-grid">
                    <?php if ($expertise): ?>
                    <div class="expert-info-card">
                        <h3>Expertise</h3>
                        <p><?php echo esc_html($expertise); ?></p>
                    </div>
                    <?php endif; ?>

                    <?php if ($education): ?>
                    <div class="expert-info-card">
                        <h3>Education</h3>
                        <p><?php echo esc_html($education); ?></p>
                    </div>
                    <?php endif; ?>

                    <?php if ($achievements): ?>
                    <div class="expert-info-card">
                        <h3>Achievements</h3>
                        <p><?php echo esc_html($achievements); ?></p>
                    </div>
                    <?php endif; ?>
                </div>

                <!-- Bio Section -->
                <?php if ($short_bio): ?>
                <div class="expert-bio-section">
                    <h3>About</h3>
                    <p><?php echo esc_html($short_bio); ?></p>
                </div>
                <?php endif; ?>

                <!-- Consultation Fee -->
                <?php if ($consultation_fee): ?>
                <div class="expert-consultation-fee">
                    <span>Consultation Fee: ₹<?php echo esc_html($consultation_fee); ?> per hour</span>
                </div>
                <?php endif; ?>

                <!-- Detailed Profile -->
                <?php if ($detailed_profile): ?>
                <div class="expert-detailed-profile">
                    <h3>Detailed Profile</h3>
                    <div><?php echo wp_kses_post($detailed_profile); ?></div>
                </div>
                <?php endif; ?>

                <!-- Contact Button -->
                <a href="#contact" class="expert-contact-btn">Book Consultation</a>
            </div>
        </div>
        
    <?php endwhile; ?>
</div>

<?php get_footer(); ?>
';
}

// ===========================================
// STEP 4: Archive Template for All Experts
// Create: archive-expert.php in your theme folder
// ===========================================

function get_expert_archive_template() {
    return '
<?php get_header(); ?>

<div class="experts-archive" style="padding: 40px 20px; background: #f5f5f5;">
    <div class="container" style="max-width: 1200px; margin: 0 auto;">
        <h1 style="text-align: center; margin-bottom: 40px; color: #333;">Our Experts</h1>
        
        <div class="experts-grid" style="display: grid; grid-template-columns: repeat(auto-fit, minmax(350px, 1fr)); gap: 30px;">
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                
                <?php
                $expert_image = get_field("expert_image");
                $expert_title = get_field("expert_title") ?: get_the_title();
                $expertise = get_field("expertise");
                $consultation_fee = get_field("consultation_fee");
                ?>
                
                <div class="expert-card" style="background: white; border-radius: 15px; padding: 25px; box-shadow: 0 5px 15px rgba(0,0,0,0.1); transition: transform 0.3s ease;">
                    <div class="expert-card-image" style="width: 80px; height: 80px; border-radius: 50%; margin: 0 auto 15px; overflow: hidden;">
                        <?php if ($expert_image): ?>
                            <img src="<?php echo esc_url($expert_image["sizes"]["thumbnail"]); ?>" alt="<?php echo esc_attr($expert_title); ?>" style="width: 100%; height: 100%; object-fit: cover;">
                        <?php else: ?>
                            <?php 
                            $initials = "";
                            $name_parts = explode(" ", $expert_title);
                            foreach ($name_parts as $part) {
                                $initials .= strtoupper(substr($part, 0, 1));
                            }
                            ?>
                            <div style="width: 100%; height: 100%; background: #2196F3; display: flex; align-items: center; justify-content: center; color: white; font-weight: bold; font-size: 24px;"><?php echo $initials; ?></div>
                        <?php endif; ?>
                    </div>
                    
                    <h3 style="text-align: center; color: #333; margin-bottom: 10px;"><?php echo esc_html($expert_title); ?></h3>
                    
                    <?php if ($expertise): ?>
                        <p style="text-align: center; color: #666; margin-bottom: 15px;"><?php echo esc_html($expertise); ?></p>
                    <?php endif; ?>
                    
                    <?php if ($consultation_fee): ?>
                        <p style="text-align: center; color: #4CAF50; font-weight: bold; margin-bottom: 20px;">₹<?php echo esc_html($consultation_fee); ?>/hr</p>
                    <?php endif; ?>
                    
                    <div style="text-align: center;">
                        <a href="<?php the_permalink(); ?>" style="background: #2196F3; color: white; padding: 10px 25px; border-radius: 25px; text-decoration: none; display: inline-block; transition: background 0.3s ease;">View Profile</a>
                    </div>
                </div>
                
            <?php endwhile; endif; ?>
        </div>
    </div>
</div>

<?php get_footer(); ?>
';
}

// ===========================================
// STEP 5: Shortcode for displaying experts anywhere
// ===========================================

function experts_shortcode($atts) {
    $atts = shortcode_atts(array(
        'count' => -1,
        'expertise' => ''
    ), $atts);
    
    $args = array(
        'post_type' => 'expert',
        'posts_per_page' => $atts['count'],
        'post_status' => 'publish'
    );
    
    if (!empty($atts['expertise'])) {
        $args['meta_query'] = array(
            array(
                'key' => 'expertise',
                'value' => $atts['expertise'],
                'compare' => 'LIKE'
            )
        );
    }
    
    $experts = new WP_Query($args);
    
    ob_start();
    
    if ($experts->have_posts()) :
        echo '<div class="experts-shortcode-grid" style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; margin: 20px 0;">';
        
        while ($experts->have_posts()) : $experts->the_post();
            $expert_image = get_field('expert_image');
            $expert_title = get_field('expert_title') ?: get_the_title();
            $expertise = get_field('expertise');
            $consultation_fee = get_field('consultation_fee');
            ?>
            
            <div class="expert-shortcode-card" style="background: white; border-radius: 10px; padding: 20px; box-shadow: 0 3px 10px rgba(0,0,0,0.1); text-align: center;">
                <?php if ($expert_image): ?>
                    <img src="<?php echo esc_url($expert_image['sizes']['thumbnail']); ?>" alt="<?php echo esc_attr($expert_title); ?>" style="width: 60px; height: 60px; border-radius: 50%; margin-bottom: 10px;">
                <?php endif; ?>
                
                <h4 style="margin-bottom: 5px; color: #333;"><?php echo esc_html($expert_title); ?></h4>
                
                <?php if ($expertise): ?>
                    <p style="color: #666; font-size: 14px; margin-bottom: 10px;"><?php echo esc_html($expertise); ?></p>
                <?php endif; ?>
                
                <?php if ($consultation_fee): ?>
                    <p style="color: #4CAF50; font-weight: bold; margin-bottom: 15px;">₹<?php echo esc_html($consultation_fee); ?>/hr</p>
                <?php endif; ?>
                
                <a href="<?php the_permalink(); ?>" style="background: #2196F3; color: white; padding: 8px 20px; border-radius: 20px; text-decoration: none; font-size: 14px;">View Profile</a>
            </div>
            
            <?php
        endwhile;
        
        echo '</div>';
        wp_reset_postdata();
    endif;
    
    return ob_get_clean();
}
add_shortcode('experts', 'experts_shortcode');

// ===========================================
// STEP 6: Admin customization
// ===========================================

// Customize admin columns
function expert_admin_columns($columns) {
    $columns['expert_image'] = 'Image';
    $columns['expertise'] = 'Expertise';
    $columns['consultation_fee'] = 'Fee';
    return $columns;
}
add_filter('manage_expert_posts_columns', 'expert_admin_columns');

function expert_admin_column_content($column, $post_id) {
    switch ($column) {
        case 'expert_image':
            $image = get_field('expert_image', $post_id);
            if ($image) {
                echo '<img src="' . esc_url($image['sizes']['thumbnail']) . '" style="width: 50px; height: 50px; border-radius: 50%;">';
            }
            break;
        case 'expertise':
            echo esc_html(get_field('expertise', $post_id));
            break;
        case 'consultation_fee':
            $fee = get_field('consultation_fee', $post_id);
            if ($fee) {
                echo '₹' . esc_html($fee) . '/hr';
            }
            break;
    }
}
add_action('manage_expert_posts_custom_column', 'expert_admin_column_content', 10, 2);

// Flush rewrite rules on activation
function expert_flush_rewrite_rules() {
    create_expert_post_type();
    flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'expert_flush_rewrite_rules');

?>