<?php
/**
 * Agent Import Script
 *
 * Run with: wp --allow-root eval-file import-agents.php
 */

// Agents to import from homeprozrealestate.com
$agents = [
    [
        'name' => 'Anna Rahn',
        'title' => 'REALTOR',
        'phone' => '507-383-6399',
        'email' => 'Anna@HomeProzRealEstate.com',
        'short_bio' => 'Dedicated real estate professional serving the Albert Lea and Southern Minnesota area.',
    ],
    [
        'name' => 'Davy Villarreal',
        'title' => 'REALTOR',
        'phone' => '507-318-0247',
        'email' => 'Davy@HomeProzRealEstate.com',
        'short_bio' => 'Experienced agent helping buyers and sellers achieve their real estate goals in Southern Minnesota.',
    ],
    [
        'name' => 'Jordan Mullenbach',
        'title' => 'REALTOR',
        'phone' => '507-402-4188',
        'email' => 'Jordan@HomeProzRealEstate.com',
        'short_bio' => 'Committed to providing exceptional service to clients throughout the Albert Lea area.',
    ],
    [
        'name' => 'Lily Dulitz',
        'title' => 'REALTOR',
        'phone' => '507-383-3781',
        'email' => 'Lily@HomeProzRealEstate.com',
        'short_bio' => 'Passionate about helping families find their perfect home in Southern Minnesota.',
    ],
];

echo "Starting agent import...\n\n";

foreach ($agents as $agent) {
    echo "Importing: {$agent['name']}\n";

    // Check if agent already exists
    $existing = get_posts([
        'post_type' => 'agent',
        'title' => $agent['name'],
        'post_status' => 'any',
        'numberposts' => 1,
    ]);

    if (!empty($existing)) {
        echo "  - Agent already exists (ID: {$existing[0]->ID}), skipping...\n\n";
        continue;
    }

    // Create the agent post
    $post_data = [
        'post_title' => $agent['name'],
        'post_content' => '',
        'post_status' => 'publish',
        'post_type' => 'agent',
    ];

    $post_id = wp_insert_post($post_data);

    if (is_wp_error($post_id)) {
        echo "  - ERROR creating post: " . $post_id->get_error_message() . "\n\n";
        continue;
    }

    echo "  - Created post ID: $post_id\n";

    // Set ACF fields
    update_field('agent_phone', $agent['phone'], $post_id);
    update_field('agent_email', $agent['email'], $post_id);
    update_field('agent_title', $agent['title'], $post_id);
    update_field('agent_short_bio', $agent['short_bio'], $post_id);

    echo "  - ACF fields updated\n";
    echo "  - DONE\n\n";
}

echo "Import complete!\n";
echo "\nNote: Agent photos need to be added manually via WordPress admin.\n";
