<?php
// tests/HelpersTest.php — PHPUnit tests for pure helper functions.
// Run with:  ./vendor/bin/phpunit   (after `composer require --dev phpunit/phpunit`)
// Or without PHPUnit:  php tests/run.php

use PHPUnit\Framework\TestCase;

final class HelpersTest extends TestCase
{
    public function testSlugifyBasics(): void
    {
        $this->assertSame('oaken-gala-2026', slugify('Oaken Gala 2026'));
        $this->assertSame('a-b-c', slugify('  a  b  c  '));
        $this->assertSame('cafe-noir', slugify('Café Noir!!!'));
        $this->assertSame('', slugify('   '));
    }

    public function testDedupKeyNormalizes(): void
    {
        $this->assertSame(
            dedupKey('John  Doe', 'John@Example.com'),
            dedupKey('john doe', 'john@example.com')
        );
        $this->assertNotSame(
            dedupKey('John Doe', 'a@x.com'),
            dedupKey('John Doe', 'b@x.com')
        );
        $this->assertSame('jane|', dedupKey('Jane', ''));
    }

    public function testGenerateTokenFormat(): void
    {
        $t = generateToken();
        $this->assertSame(32, strlen($t));
        $this->assertMatchesRegularExpression('/^[0-9a-f]{32}$/', $t);
        $this->assertNotSame(generateToken(), generateToken());
    }

    public function testPlanLimits(): void
    {
        $super = planLimits(['role' => 'super_admin']);
        $this->assertNull($super['max_events']);
        $this->assertNull($super['max_guests']);

        $capped = planLimits(['role' => 'admin', 'max_events' => '3', 'max_guests' => '100']);
        $this->assertSame(3, $capped['max_events']);
        $this->assertSame(100, $capped['max_guests']);
    }

    public function testEventTzFallsBackToDefault(): void
    {
        $this->assertSame(DEFAULT_TZ, eventTz(['timezone' => '']));
        $this->assertSame(DEFAULT_TZ, eventTz(['timezone' => 'Not/AZone']));
        $this->assertSame('Europe/London', eventTz(['timezone' => 'Europe/London']));
    }

    public function testFmtEventTimeConverts(): void
    {
        // A fixed instant formatted into two zones should differ by the offset.
        $utc    = fmtEventTime('2026-07-12 12:00:00', ['timezone' => 'UTC'], 'H:i');
        $lagos  = fmtEventTime('2026-07-12 12:00:00', ['timezone' => 'Africa/Lagos'], 'H:i');
        $this->assertNotSame('', $utc);
        $this->assertNotSame('', $lagos);
        $this->assertSame('', fmtEventTime('', ['timezone' => 'UTC']));
    }
}
