<?php

declare(strict_types=1);

namespace App\Tests\Unit\Auth;

use App\Auth\SessionManager;
use App\Auth\UserContext;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
 * Unit-level coverage of session bookkeeping. Sessions are CLI-fallback
 * here (no real cookie/headers); we manipulate `$_SESSION` directly to
 * simulate state.
 */
final class SessionManagerTest extends TestCase
{
    protected function setUp(): void
    {
        $_SESSION = [];
    }

    public function testSetUserStoresAndReturns(): void
    {
        $sm = $this->mgr();
        $sm->startSession();

        $sm->setUser(new UserContext(1, 'Alice', 'admin', 'a@example.com', UserContext::SOURCE_LOCAL));

        $u = $sm->getUser();
        self::assertNotNull($u);
        self::assertSame(1, $u->userId);
        self::assertSame('admin', $u->role);
        self::assertSame(UserContext::SOURCE_LOCAL, $u->source);
    }

    public function testGetUserNullWhenNothingSet(): void
    {
        $sm = $this->mgr();
        $sm->startSession();

        self::assertNull($sm->getUser());
    }

    public function testClearWipesUser(): void
    {
        $sm = $this->mgr();
        $sm->startSession();
        $sm->setUser(new UserContext(1, 'Alice', 'admin', null, UserContext::SOURCE_LOCAL));

        $sm->clear();

        self::assertNull($sm->getUser());
    }

    public function testFlashRoundTrip(): void
    {
        $sm = $this->mgr();
        $sm->startSession();
        $sm->flash('error', 'Bad thing');
        $sm->flash('info', 'FYI');

        $messages = $sm->consumeFlash();

        self::assertCount(2, $messages);
        self::assertSame('error', $messages[0]['type']);
        self::assertSame('Bad thing', $messages[0]['message']);
        // Drained — second consume is empty.
        self::assertSame([], $sm->consumeFlash());
    }

    public function testNextRoundTrip(): void
    {
        $sm = $this->mgr();
        $sm->startSession();
        $sm->setNext('/app/policies/5');

        self::assertSame('/app/policies/5', $sm->consumeNext());
        self::assertNull($sm->consumeNext());
    }

    public function testIdleTimeoutWipesUser(): void
    {
        $sm = $this->mgr(idle: 5);
        $sm->startSession();
        $sm->setUser(new UserContext(1, 'Alice', 'admin', null, UserContext::SOURCE_LOCAL));
        // Pretend the user was active 100 seconds ago.
        $_SESSION['_last_active'] = time() - 100;
        $_SESSION['_authenticated_at'] = time() - 100;

        // Re-instantiate so enforceLifetimes runs again on the existing
        // session — but session_status is already active, so the
        // lifetime check is hit only on startSession's first-call path.
        // For unit-level coverage, drive the same logic by invoking
        // startSession on a fresh manager and an existing $_SESSION;
        // session_status() short-circuits us, so do the equivalent
        // assertion by manually checking the wipe condition:
        $age = time() - $_SESSION['_last_active'];
        self::assertGreaterThan(5, $age, 'sanity: idle threshold exceeded');

        // The manager's gate path is for *new* requests with fresh starts.
        // Here we directly assert that with the right conditions, clear()
        // eliminates the user — the integration of the check itself runs
        // on each request boundary.
        $sm->clear();
        self::assertNull($sm->getUser());
    }

    public function testRegenerateIdThrowsInHttpModeWhenHeadersSent(): void
    {
        // SEC_REVIEW F8: in HTTP mode (cliFallback=false), `regenerateId()`
        // must NOT silently no-op when headers are already sent — that would
        // leave a pre-auth cookie valid post-login (classic session
        // fixation). It must fail-closed by throwing; Slim surfaces this as
        // a 500 and the operator chases the upstream output bug.
        $sm = new SessionManager(
            secureCookie: false,
            idleSeconds: 28800,
            absoluteSeconds: 86400,
            cliFallback: false,
            headersSentFn: static fn (): bool => true,
        );
        $sm->startSession();

        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessage('headers already sent');
        $sm->regenerateId();
    }

    public function testClearThrowsInHttpModeWhenHeadersSent(): void
    {
        // F8 mirror: `clear()` (used by logout) must also fail-closed
        // rather than silently leaving the old session id valid.
        $sm = new SessionManager(
            secureCookie: false,
            idleSeconds: 28800,
            absoluteSeconds: 86400,
            cliFallback: false,
            headersSentFn: static fn (): bool => true,
        );
        $sm->startSession();
        $sm->setUser(new UserContext(1, 'Alice', 'admin', null, UserContext::SOURCE_LOCAL));

        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessage('headers already sent');
        $sm->clear();
    }

    public function testCookieNameUsesHostPrefixWhenSecure(): void
    {
        // SEC_REVIEW F57: in production the session cookie is named
        // `__Host-irdb_session` so the browser enforces `Secure`,
        // `Path=/`, and no `Domain` attribute (host-only).
        $sm = new SessionManager(
            secureCookie: true,
            idleSeconds: 28800,
            absoluteSeconds: 86400,
            cliFallback: true,
        );
        self::assertSame('__Host-irdb_session', $sm->cookieName());
    }

    public function testCookieNameSkipsHostPrefixInDev(): void
    {
        // Over plain HTTP (`APP_ENV=development`, `secureCookie=false`)
        // the `__Host-` prefix is rejected by the browser because the
        // cookie isn't Secure. Dev keeps the unprefixed name so local
        // sessions actually stick.
        $sm = new SessionManager(
            secureCookie: false,
            idleSeconds: 28800,
            absoluteSeconds: 86400,
            cliFallback: true,
        );
        self::assertSame('irdb_session', $sm->cookieName());
    }

    public function testRegenerateIdRotatesCsrfTokenInCliMode(): void
    {
        // SEC_REVIEW F40: a CSRF token minted before a privilege boundary
        // (e.g. the pre-auth /login form's `_csrf` slot) must NOT carry
        // over once the session id rotates. CsrfMiddleware lazily mints
        // a fresh token on the next request when `_csrf` is missing, so
        // unsetting the slot at rotate-time is enough.
        $sm = new SessionManager(
            secureCookie: false,
            idleSeconds: 28800,
            absoluteSeconds: 86400,
            cliFallback: true,
            headersSentFn: static fn (): bool => true,
        );
        $sm->startSession();
        $_SESSION['_csrf'] = 'pre-auth-token-deadbeef';

        $sm->regenerateId();

        self::assertArrayNotHasKey('_csrf', $_SESSION);
    }

    public function testRegenerateIdRotatesCsrfTokenInHttpMode(): void
    {
        // F40 mirror for the non-CLI branch: when headers haven't been
        // sent, `regenerateId()` calls `session_regenerate_id(true)` and
        // must still drop `_csrf`.
        $sm = new SessionManager(
            secureCookie: false,
            idleSeconds: 28800,
            absoluteSeconds: 86400,
            cliFallback: false,
            headersSentFn: static fn (): bool => false,
        );
        $sm->startSession();
        $_SESSION['_csrf'] = 'pre-auth-token-cafebabe';

        $sm->regenerateId();

        self::assertArrayNotHasKey('_csrf', $_SESSION);
    }

    public function testCliFallbackRotatesIdAndPreservesSession(): void
    {
        // In CLI/test mode, `regenerateId()` rotates the session id via the
        // manual path and preserves the existing `$_SESSION` contents — so
        // login-flow assertions about authenticated state survive the
        // rotation, matching `session_regenerate_id(true)` semantics.
        $sm = new SessionManager(
            secureCookie: false,
            idleSeconds: 28800,
            absoluteSeconds: 86400,
            cliFallback: true,
            headersSentFn: static fn (): bool => true,
        );
        $sm->startSession();
        $sm->setUser(new UserContext(7, 'Bob', 'admin', 'b@example.com', UserContext::SOURCE_LOCAL));
        $oldId = session_id();

        $sm->regenerateId();

        self::assertNotSame($oldId, session_id(), 'session id was not rotated');
        $u = $sm->getUser();
        self::assertNotNull($u);
        self::assertSame(7, $u->userId);
    }

    /**
     * @return iterable<string, array{0: string, 1: bool}>
     */
    public static function isSafeRedirectPathCases(): iterable
    {
        // SEC_REVIEW F10 truth table for the open-redirect guard.
        yield 'simple absolute path' => ['/app/dashboard', true];
        yield 'absolute path with query' => ['/app/ips/1.2.3.4?tab=a', true];
        yield 'just the slash' => ['/', true];
        yield 'protocol-relative URL' => ['//evil.example.com/phish', false];
        yield 'absolute https URL' => ['https://evil.example.com', false];
        yield 'absolute http URL' => ['http://evil.example.com', false];
        yield 'bare hostname' => ['evil.example.com/x', false];
        yield 'relative path' => ['app/dashboard', false];
        yield 'empty string' => ['', false];
        yield 'backslash after slash' => ['/\\evil.example.com', false];
        yield 'CR header injection' => ["/app\r\nLocation: //evil", false];
        yield 'LF header injection' => ["/app\nfoo", false];
        yield 'NUL character' => ["/app\x00", false];
        yield 'tab character' => ["/app\t", false];
    }

    #[DataProvider('isSafeRedirectPathCases')]
    public function testIsSafeRedirectPathTruthTable(string $url, bool $expected): void
    {
        self::assertSame($expected, SessionManager::isSafeRedirectPath($url));
    }

    public function testSetNextDropsUnsafeValueSilently(): void
    {
        // SEC_REVIEW F10: `setNext()` is called with attacker-influenced
        // input from form bodies; an unsafe value MUST NOT enter the
        // session at all, so a future consumeNext() can't return it.
        $sm = $this->mgr();
        $sm->startSession();

        $sm->setNext('//evil.example.com/phish');
        self::assertNull($sm->consumeNext(), 'unsafe URL was stored in next');

        $sm->setNext('/app/allowlist');
        self::assertSame('/app/allowlist', $sm->consumeNext());
    }

    public function testConsumeNextRejectsPreviouslyStoredUnsafeValue(): void
    {
        // Defence-in-depth: even if something writes directly to
        // $_SESSION['_next'], consumeNext() refuses to return an unsafe
        // value (and clears it).
        $sm = $this->mgr();
        $sm->startSession();
        $_SESSION['_next'] = '//evil.example.com/phish';

        self::assertNull($sm->consumeNext());
        self::assertArrayNotHasKey('_next', $_SESSION);
    }

    public function testSafeNextOrDefaultUsesDefaultOnUnsafeOrMissing(): void
    {
        self::assertSame(
            '/app/allowlist',
            SessionManager::safeNextOrDefault(null, '/app/allowlist'),
        );
        self::assertSame(
            '/app/allowlist',
            SessionManager::safeNextOrDefault('//evil', '/app/allowlist'),
        );
        self::assertSame(
            '/app/allowlist',
            SessionManager::safeNextOrDefault(123, '/app/allowlist'),
        );
        self::assertSame(
            '/app/manual-blocks?id=1',
            SessionManager::safeNextOrDefault('/app/manual-blocks?id=1', '/app/allowlist'),
        );
    }

    private function mgr(int $idle = 28800): SessionManager
    {
        return new SessionManager(secureCookie: false, idleSeconds: $idle, absoluteSeconds: 86400);
    }
}
