<?php
/**
 * @package WordPress
 * @subpackage Jobster
 */

// [list_all_meta_keys]
add_shortcode('list_all_meta_keys', function () {
    global $wpdb;

    $output = '';
    $post_types = ['candidate', 'company','membership'];

    foreach ($post_types as $post_type) {
        $output .= "<h3>Meta Keys for Post Type: <strong>{$post_type}</strong></h3><ul>";

        $meta_keys = $wpdb->get_col($wpdb->prepare("
            SELECT DISTINCT meta_key
            FROM {$wpdb->postmeta} pm
            INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id
            WHERE p.post_type = %s
              AND meta_key NOT LIKE '\_%'
            ORDER BY meta_key ASC
        ", $post_type));

        if (!empty($meta_keys)) {
            foreach ($meta_keys as $key) {
                $output .= "<li><code>{$key}</code></li>";
            }
        } else {
            $output .= "<li><em>No meta keys found.</em></li>";
        }

        $output .= '</ul>';
    }

    return '<div class="meta-key-list">' . $output . '</div>';
});
// [list_candidate_user] — Shortcode to list company_user meta for all companies
function list_candidate_user_shortcode() {
    $args = array(
        'post_type'      => 'candidate',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
    );

    $query = new WP_Query($args);
    if (!$query->have_posts()) {
        return '<p>No candidate found.</p>';
    }

    $output = '<ul>';
    while ($query->have_posts()) {
        $query->the_post();
        $company_id   = get_the_ID();
        $company_name = get_the_title($company_id);
        $user_id      = get_post_meta($company_id, 'candidate_user', true);

        $user_info = $user_id ? get_userdata($user_id) : null;
        $user_name = $user_info ? esc_html($user_info->display_name) : 'N/A';

        $output .= '<li><strong>' . esc_html($company_name) . '</strong> — User: ' . $user_name . ' (ID: ' . esc_html($user_id) . ')</li>';
    }
    wp_reset_postdata();
    $output .= '</ul>';

    return $output;
}
add_shortcode('list_candidate_user', 'list_candidate_user_shortcode');

// [list_company_user] — Shortcode to list company_user meta for all companies
function list_company_user_shortcode() {
    $args = array(
        'post_type'      => 'company',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
    );

    $query = new WP_Query($args);
    if (!$query->have_posts()) {
        return '<p>No companies found.</p>';
    }

    $output = '<ul>';
    while ($query->have_posts()) {
        $query->the_post();
        $company_id   = get_the_ID();
        $company_name = get_the_title($company_id);
        $user_id      = get_post_meta($company_id, 'company_user', true);

        $user_info = $user_id ? get_userdata($user_id) : null;
        $user_name = $user_info ? esc_html($user_info->display_name) : 'N/A';

        $output .= '<li><strong>' . esc_html($company_name) . '</strong> — User: ' . $user_name . ' (ID: ' . esc_html($user_id) . ')</li>';
    }
    wp_reset_postdata();
    $output .= '</ul>';

    return $output;
}
add_shortcode('list_company_user', 'list_company_user_shortcode');
// [list_datares_company_user] — Show only companies titled "DataRes" and their user info
function list_datares_company_user_shortcode() {
    $args = array(
        'post_type'      => 'company',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
        's'              => 'CoderBotics', // Search by title
    );

    $query = new WP_Query($args);
    if (!$query->have_posts()) {
        return '<p>No companies named "DataRes" found.</p>';
    }

    $output = '<ul>';
    while ($query->have_posts()) {
        $query->the_post();
        $company_id   = get_the_ID();
        $company_name = get_the_title($company_id);
        $user_id      = get_post_meta($company_id, 'company_user', true);

        $user_info = $user_id ? get_userdata($user_id) : null;
        $user_name = $user_info ? esc_html($user_info->display_name) : 'N/A';

        $output .= '<li><strong>' . esc_html($company_name) . '</strong> — User: ' . $user_name . ' (ID: ' . esc_html($user_id) . ')</li>';
    }
    wp_reset_postdata();
    $output .= '</ul>';

    return $output;
}
add_shortcode('list_datares_company_user', 'list_datares_company_user_shortcode');
// [list_user_meta user_id="2"]
function list_user_meta_shortcode($atts) {
    $atts = shortcode_atts([
        'user_id' => 0,
    ], $atts);

    $user_id = intval($atts['user_id']);
    if (!$user_id) {
        return '<p>User ID not provided.</p>';
    }

    $user_meta = get_user_meta($user_id);
    if (empty($user_meta)) {
        return '<p>No meta data found for user ID ' . esc_html($user_id) . '.</p>';
    }

    $output = '<h3>User Meta for ID ' . esc_html($user_id) . '</h3><ul>';
    foreach ($user_meta as $key => $value) {
        $formatted_value = is_array($value) ? implode(', ', array_map('esc_html', $value)) : esc_html($value);
        $output .= '<li><strong>' . esc_html($key) . '</strong>: ' . $formatted_value . '</li>';
    }
    $output .= '</ul>';

    return $output;
}
add_shortcode('list_user_meta', 'list_user_meta_shortcode');
// [company_meta_by_user user_id="2"]
function company_meta_by_user_shortcode($atts) {
    $atts = shortcode_atts([
        'user_id' => 0,
    ], $atts);

    $user_id = intval($atts['user_id']);
    if (!$user_id) return '<p>Missing user ID.</p>';

    // Query the company post linked to this user
    $args = [
        'post_type'  => 'company',
        'meta_key'   => 'company_user',
        'meta_value' => $user_id,
        'posts_per_page' => -1,
    ];
    $query = new WP_Query($args);

    if (!$query->have_posts()) return '<p>No company found for this user.</p>';

    $output = '';
    foreach ($query->posts as $post) {
        $meta = get_post_meta($post->ID);
        $output .= '<h3>' . esc_html($post->post_title) . ' (Company ID: ' . $post->ID . ')</h3><ul>';
        foreach ($meta as $key => $value) {
            $val = is_array($value) ? implode(', ', array_map('esc_html', $value)) : esc_html($value);
            $output .= '<li><strong>' . esc_html($key) . '</strong>: ' . $val . '</li>';
        }
        $output .= '</ul>';
    }

    return $output;
}
add_shortcode('company_meta_by_user', 'company_meta_by_user_shortcode');
function candidate_meta_by_user_shortcode($atts) {
    $atts = shortcode_atts([
        'user_id' => 0,
    ], $atts);

    $user_id = intval($atts['user_id']);
    if (!$user_id) return '<p>Missing user ID.</p>';

    // Query the company post linked to this user
    $args = [
        'post_type'  => 'candidate',
        'meta_key'   => 'candidate_user',
        'meta_value' => $user_id,
        'posts_per_page' => -1,
    ];
    $query = new WP_Query($args);

    if (!$query->have_posts()) return '<p>No candidate found for this user.</p>';

    $output = '';
    foreach ($query->posts as $post) {
        $meta = get_post_meta($post->ID);
        $output .= '<h3>' . esc_html($post->post_title) . ' (candidate ID: ' . $post->ID . ')</h3><ul>';
        foreach ($meta as $key => $value) {
            $val = is_array($value) ? implode(', ', array_map('esc_html', $value)) : esc_html($value);
            $output .= '<li><strong>' . esc_html($key) . '</strong>: ' . $val . '</li>';
        }
        $output .= '</ul>';
    }

    return $output;
}
add_shortcode('candidate_meta_by_user', 'candidate_meta_by_user_shortcode');

add_action('wp_enqueue_scripts', 'jobster_enqueue_styles');
function jobster_enqueue_styles() {
    $parenthandle = 'jobster-style';
    $theme = wp_get_theme();

    $css_arr = array(
        'font-awesome',
        'pxp-base-font',
        'bootstrap',
        'owl-carousel',
        'owl-theme',
        'animate'
    );

    if (is_singular('candidate') || is_singular('company')) {
        $css_arr[] = 'photoswipe';
        $css_arr[] = 'photoswipe-skin';
    }

    wp_enqueue_style(
        $parenthandle,
        get_template_directory_uri() . '/style.css', 
        $css_arr, 
        $theme->parent()->get('Version')
    );

    wp_enqueue_style('child-style', get_stylesheet_uri(),
        array($parenthandle),
        $theme->get('Version')
    );
}
?>