<?php
// tests/run.php — zero-dependency test runner (no PHPUnit required).
// Usage:  php tests/run.php
// Mirrors the assertions in HelpersTest.php for environments without Composer.

require __DIR__ . '/bootstrap.php';

$passed = 0; $failed = 0; $failures = [];

function check(string $label, bool $cond) {
    global $passed, $failed, $failures;
    if ($cond) { $passed++; echo "  ✓ {$label}\n"; }
    else       { $failed++; $failures[] = $label; echo "  ✗ {$label}\n"; }
}

echo "slugify\n";
check('spaces → dashes', slugify('Oaken Gala 2026') === 'oaken-gala-2026');
check('trims + collapses', slugify('  a  b  c  ') === 'a-b-c');
check('empty stays empty', slugify('   ') === '');

echo "dedupKey\n";
check('case/space insensitive', dedupKey('John  Doe', 'John@Example.com') === dedupKey('john doe', 'john@example.com'));
check('different emails differ', dedupKey('J', 'a@x.com') !== dedupKey('J', 'b@x.com'));
check('blank email form', dedupKey('Jane', '') === 'jane|');

echo "generateToken\n";
$t = generateToken();
check('32 hex chars', strlen($t) === 32 && preg_match('/^[0-9a-f]{32}$/', $t) === 1);
check('unique', generateToken() !== generateToken());

echo "planLimits\n";
$s = planLimits(['role' => 'super_admin']);
check('super = unlimited', $s['max_events'] === null && $s['max_guests'] === null);
$c = planLimits(['role' => 'admin', 'max_events' => '3', 'max_guests' => '100']);
check('admin caps cast to int', $c['max_events'] === 3 && $c['max_guests'] === 100);

echo "eventTz\n";
check('blank → default', eventTz(['timezone' => '']) === DEFAULT_TZ);
check('invalid → default', eventTz(['timezone' => 'Not/AZone']) === DEFAULT_TZ);
check('valid kept', eventTz(['timezone' => 'Europe/London']) === 'Europe/London');

echo "fmtEventTime\n";
check('empty in → empty out', fmtEventTime('', ['timezone' => 'UTC']) === '');
check('non-empty format', fmtEventTime('2026-07-12 12:00:00', ['timezone' => 'UTC'], 'H:i') !== '');

echo "\n" . str_repeat('─', 40) . "\n";
echo ($failed === 0 ? "ALL PASSED" : "FAILED") . " — {$passed} passed, {$failed} failed\n";
if ($failures) { echo "Failures:\n"; foreach ($failures as $f) echo "  - {$f}\n"; }
exit($failed === 0 ? 0 : 1);
