<?php

define('ROOT_PATH', dirname(__DIR__));
require_once ROOT_PATH . '/app/core/Autoloader.php';
\App\Core\Autoloader::register();

$bootstrap = new \App\Core\Bootstrap();
$bootstrap->init();

use App\Modules\Auth\AuthService;

function run_test(string $name, callable $test) {
    try {
        $test();
        echo "✅ PASS: $name\n";
    } catch (\Throwable $e) {
        echo "❌ FAIL: $name\n";
        echo "   Error: " . $e->getMessage() . "\n";
    }
}

$authService = new AuthService();

run_test('Super Admin login via username (admin)', function () use ($authService) {
    $res = $authService->attemptLogin('admin', 'admin', '127.0.0.1');
    if (!$res['success']) {
        throw new Exception("Login failed: " . ($res['error'] ?: 'unknown error'));
    }
    if (strpos($res['redirect'], 'admin/dashboard') === false) {
        throw new Exception("Unexpected redirect target: " . $res['redirect']);
    }
});

run_test('Super Admin login via email (admin@inveetaire.local)', function () use ($authService) {
    $res = $authService->attemptLogin('admin@inveetaire.local', 'admin', '127.0.0.1');
    if (!$res['success']) {
        throw new Exception("Login failed: " . ($res['error'] ?: 'unknown error'));
    }
});

run_test('Crew login via username (crew)', function () use ($authService) {
    $res = $authService->attemptLogin('crew', 'admin', '127.0.0.1');
    if (!$res['success']) {
        throw new Exception("Login failed: " . ($res['error'] ?: 'unknown error'));
    }
    if (strpos($res['redirect'], 'crew/operation/scanner') === false) {
        throw new Exception("Unexpected redirect target: " . $res['redirect']);
    }
});

run_test('Test Couple login via username (test)', function () use ($authService) {
    $res = $authService->attemptLogin('test', 'test123', '127.0.0.1');
    if (!$res['success']) {
        throw new Exception("Login failed: " . ($res['error'] ?: 'unknown error'));
    }
    if (strpos($res['redirect'], 'couple/invitation/edit') === false) {
        throw new Exception("Unexpected redirect target: " . $res['redirect']);
    }
});

run_test('Test Couple login via email (test@inveetaire.local)', function () use ($authService) {
    $res = $authService->attemptLogin('test@inveetaire.local', 'test123', '127.0.0.1');
    if (!$res['success']) {
        throw new Exception("Login failed: " . ($res['error'] ?: 'unknown error'));
    }
});

run_test('Incorrect password returns error', function () use ($authService) {
    $res = $authService->attemptLogin('admin', 'wrong_password', '127.0.0.1');
    if ($res['success']) {
        throw new Exception("Expected login to fail, but it succeeded.");
    }
    if ($res['error'] !== 'Invalid email or password.') {
        throw new Exception("Unexpected error message: " . $res['error']);
    }
});

run_test('Unknown user returns error', function () use ($authService) {
    $res = $authService->attemptLogin('nonexistent_user', 'admin', '127.0.0.1');
    if ($res['success']) {
        throw new Exception("Expected login to fail, but it succeeded.");
    }
    if ($res['error'] !== 'Invalid email or password.') {
        throw new Exception("Unexpected error message: " . $res['error']);
    }
});
