<?php
/**
 * CSV Field Analysis Script
 * Analyze CSV_Trainers_Import_1Aug2025.csv and identify missing fields
 */

// Read the CSV file
$csv_file = 'CSV_Trainers_Import_1Aug2025.csv';
if (!file_exists($csv_file)) {
    die("CSV file not found: $csv_file\n");
}

$handle = fopen($csv_file, 'r');
if (!$handle) {
    die("Cannot open CSV file\n");
}

// Get headers
$headers = fgetcsv($handle);
echo "=== CSV HEADERS ANALYSIS ===\n\n";
echo "Total columns: " . count($headers) . "\n\n";

foreach ($headers as $index => $header) {
    echo sprintf("%2d. %-25s", $index + 1, trim($header)) . "\n";
}

// Get first few rows to see data structure
echo "\n=== SAMPLE DATA STRUCTURE ===\n\n";
$sample_rows = 0;
while (($row = fgetcsv($handle)) !== FALSE && $sample_rows < 3) {
    $sample_rows++;
    echo "Row $sample_rows:\n";
    foreach ($headers as $index => $header) {
        $value = isset($row[$index]) ? trim($row[$index]) : '';
        echo sprintf("  %-25s: %s\n", trim($header), $value);
    }
    echo "\n";
}

fclose($handle);

// Compare with current taxonomy fields
echo "=== TAXONOMY FIELD MAPPING ===\n\n";

$taxonomy_fields = [
    'business_type' => 'Business Type',
    'training_audience' => 'Training Audience',
    'training_formats' => 'Not available in CSV',
    'training_locations' => 'Not available in CSV', 
    'training_resources' => 'Not available in CSV'
];

echo "Available taxonomy mappings:\n";
foreach ($taxonomy_fields as $taxonomy => $csv_field) {
    echo sprintf("  %-20s -> %s\n", $taxonomy, $csv_field);
}

echo "\n=== MISSING PROFILE FIELDS ===\n\n";

$profile_fields_in_csv = [
    'Company Name' => 'organization_name / company_name',
    'Role' => 'role / personal_role',
    'Date Certified' => 'date_certified',
    'Certification Type' => 'certification_type', 
    'Certification Status' => 'certification_status',
    'Company Website' => 'company_website / organization_website',
    'Phone Number' => 'phone_number / trainer_phone',
    'Application Details' => 'application_details',
    'User ID' => 'username (for user creation)',
    'Create Venue' => 'create_venue_flag',
    'Create Organizer' => 'create_organizer_flag'
];

echo "Profile fields available in CSV but potentially missing from import:\n";
foreach ($profile_fields_in_csv as $csv_field => $profile_field) {
    echo sprintf("  %-20s -> %s\n", $csv_field, $profile_field);
}

echo "\n=== FIELD ENHANCEMENT RECOMMENDATIONS ===\n\n";

$recommendations = [
    '1. Add Role field to profile import (maps to new role taxonomy)',
    '2. Import Company Website as organization_website',
    '3. Import Phone Number as trainer_phone', 
    '4. Import Application Details to profile content or meta',
    '5. Use User ID column for consistent username creation',
    '6. Process Create Venue/Organizer flags for automatic creation',
    '7. Import Date Certified with proper date parsing',
    '8. Map Business Type to business_type taxonomy',
    '9. Map Training Audience to training_audience taxonomy',
    '10. Handle multiple values in Training Audience field (comma-separated)'
];

foreach ($recommendations as $rec) {
    echo "  $rec\n";
}

echo "\n=== PROPOSED IMPORT SCRIPT ENHANCEMENTS ===\n\n";
echo "The enhanced import script should:\n";
echo "1. Read actual CSV file instead of hardcoded data\n";
echo "2. Parse comma-separated taxonomy values properly\n";
echo "3. Import all available profile fields\n";
echo "4. Handle date fields with proper formatting\n";
echo "5. Create venues and organizers based on flags\n";
echo "6. Use proper taxonomy assignment for new taxonomy system\n";

?>