<?php
declare(strict_types=1);

/**
 * Adds the admin smart-tips switch to an existing crm database.
 * Fresh installs already include this in schema.sql.
 * Run: C:\xampp\php\php.exe sql/migrate_phase5.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 = 'smart_tips_on'"
)->fetchColumn();

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

echo "Phase 5 migrate finished.\n";
