<?php
declare(strict_types=1);

/**
 * Adds an optional visit date on service jobs (Customer Portal booking).
 * Fresh installs already include this in schema.sql.
 * Run: C:\xampp\php\php.exe sql/migrate_job_visit_at.php
 */
$base = dirname(__DIR__);
require $base . '/app/init.php';

$db = App\Core\Database::instance();
$pdo = $db->pdo();

$has = (int) $pdo->query(
    "SELECT COUNT(*) FROM information_schema.COLUMNS
     WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'service_jobs' AND COLUMN_NAME = 'visit_at'"
)->fetchColumn();

if ($has < 1) {
    $pdo->exec('ALTER TABLE service_jobs ADD COLUMN visit_at DATETIME NULL AFTER source');
    echo "Added service_jobs.visit_at\n";
} else {
    echo "service_jobs.visit_at already present\n";
}

echo "Job visit date migrate finished.\n";
