<?php
require_once __DIR__ . '/config/database.php';

// Multi-branch workshops that need city suffix to avoid UNIQUE name conflict
$branches = [
    ['COMBINES SYSTEM LTD (IBADAN)', 'MOSHOOD ABIOLA WAY', 'IBADAN'],
    ['COMBINES SYSTEM LTD (OSHOGBO)', 'CENTRAL', 'OSHOGBO'],
    ['KAURA MOTORS (KADUNA)', 'AB2, Namidi Azikwe Way, Express bypass, P.O. Box 974', 'KADUNA'],
    ['CARBON CAR CARE AND TYRES (Jakande)', 'BY CIRCLE MALL, JAKANDE', 'LAGOS'],
    ['CARBON CAR CARE AND TYRES (Lekki)', 'IKATE, LEKKI-AJAH EXP. WAY', 'LEKKI'],
    ['AFFORDABLE CARS (Lekki)', 'LEKKI', 'LAGOS'],
    ['AUTOPOINTE QUICK FIX (Ojodu)', 'OGUNNUSI RD, OJODU, BESIDE STANBIC BANK, OJODU', 'OJODU, LAGOS'],
    ['AUTOPOINTE QUICK FIX (Oshodi)', 'BY VIO SITE, BOLADE, OSHODI', 'OSHODI, LAGOS'],
    ['T & T AUTOS (Umuahia)', '115, ABA ROAD, OPPOSITE APRICOT HOTEL', 'UMUAHIA'],
];

$pdo->beginTransaction();
try {
    $stmtCheck = $pdo->prepare("SELECT id FROM workshops WHERE name = ? AND deleted_at IS NULL");
    $stmtInsert = $pdo->prepare("INSERT INTO workshops (name, address, city, vendor_type, status) VALUES (?, ?, ?, 'Workshop', 'active')");
    $imported = 0;

    foreach ($branches as $b) {
        $stmtCheck->execute([$b[0]]);
        if ($stmtCheck->fetchColumn()) {
            echo "  Found: {$b[0]}\n";
            continue;
        }
        $stmtInsert->execute($b);
        $imported++;
        echo "  Created: {$b[0]}\n";
    }

    $pdo->commit();
    echo "\n=== Summary ===\nAdded: {$imported} branch(es)\n";
} catch (Throwable $e) {
    $pdo->rollBack();
    echo "ERROR: {$e->getMessage()}\n";
    exit(1);
}
