<?php
declare(strict_types=1);

/**
 * Adds the admin company timezone to an existing crm database.
 * Fresh installs already include this in schema.sql.
 * Run: C:\xampp\php\php.exe sql/migrate_timezone.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 = 'tenants' AND COLUMN_NAME = 'timezone'"
)->fetchColumn();

if ($has < 1) {
    $pdo->exec("ALTER TABLE tenants ADD COLUMN timezone VARCHAR(64) NOT NULL DEFAULT 'America/New_York' AFTER smart_tips_on");
    echo "Added tenants.timezone\n";
} else {
    echo "tenants.timezone already present\n";
}

echo "Timezone migrate finished.\n";
