<?php
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

function testApi($kernel, $method, $uri, $data = [], $token = null) {
    $server = ['HTTP_ACCEPT' => 'application/json'];
    if ($token) {
        $server['HTTP_AUTHORIZATION'] = 'Bearer ' . $token;
    }
    $request = Illuminate\Http\Request::create($uri, $method, $data, [], [], $server);
    $response = $kernel->handle($request);
    
    $status = $response->getStatusCode();
    $content = json_decode($response->getContent(), true);
    
    if ($status >= 400 && $status !== 422) { // 422 is expected for some negative tests
        echo "FAIL: [$method] $uri returned $status\n";
        echo "Response: " . $response->getContent() . "\n\n";
    } else {
        echo "PASS: [$method] $uri returned $status\n";
    }
    return ['status' => $status, 'data' => $content];
}

echo "--- STARTING API TESTS ---\n";

// 1. LOGIN PEGawai
$res = testApi($kernel, 'POST', '/api/login', ['username' => 'hari_disnaker', 'password' => 'password123']);
$userToken = $res['data']['data']['token'] ?? null;

// 2. LOGIN ADMIN
$res = testApi($kernel, 'POST', '/api/login', ['username' => 'welprida_admin', 'password' => 'password123']);
$adminToken = $res['data']['data']['token'] ?? null;

// 3. LOGIN SUPERADMIN
$res = testApi($kernel, 'POST', '/api/login', ['username' => 'sutowo_superadmin', 'password' => 'password123']);
$superToken = $res['data']['data']['token'] ?? null;

if (!$userToken || !$adminToken || !$superToken) {
    die("Tokens missing, cannot proceed.\n");
}

// --- PEGAWAI TESTS ---
echo "\n--- ROLE: PEGAWAI ---\n";
testApi($kernel, 'GET', '/api/dashboard', [], $userToken);
testApi($kernel, 'GET', '/api/penugasan', [], $userToken);
$createTask = testApi($kernel, 'POST', '/api/penugasan', [
    'kegiatan' => 'Tugas Testing',
    'tanggal_mulai' => '2026-06-01',
    'pegawai_ids' => [4] // hari_disnaker ID is likely 4
], $userToken);
$taskId = $createTask['data']['data']['id'] ?? null;

if ($taskId) {
    testApi($kernel, 'GET', '/api/penugasan/' . $taskId, [], $userToken);
    testApi($kernel, 'PUT', '/api/penugasan/' . $taskId, [
        'kegiatan' => 'Tugas Testing Updated',
        'tanggal_mulai' => '2026-06-01',
        'pegawai_ids' => [4]
    ], $userToken);
    testApi($kernel, 'POST', '/api/penugasan/' . $taskId . '/update-status', ['status' => 'selesai'], $userToken);
}

// --- ADMIN TESTS ---
echo "\n--- ROLE: ADMIN ---\n";
testApi($kernel, 'GET', '/api/dashboard', [], $adminToken);
testApi($kernel, 'GET', '/api/users', [], $adminToken);
testApi($kernel, 'GET', '/api/pegawai', [], $adminToken);

// Create User
$createUser = testApi($kernel, 'POST', '/api/register', [
    'name' => 'User Test',
    'username' => 'usertest_'.time(),
    'email' => 'usertest'.time().'@mail.com',
    'password' => 'password123',
    'role' => 'user'
], $adminToken);
$newUserId = $createUser['data']['user']['id'] ?? null;

if ($newUserId) {
    testApi($kernel, 'PUT', '/api/users/' . $newUserId, ['name' => 'User Test Updated', 'role' => 'user'], $adminToken);
    testApi($kernel, 'DELETE', '/api/users/' . $newUserId, [], $adminToken);
}

// --- SUPER ADMIN TESTS ---
echo "\n--- ROLE: SUPER ADMIN ---\n";
testApi($kernel, 'GET', '/api/dashboard', [], $superToken);
if ($taskId) {
    testApi($kernel, 'POST', '/api/penugasan/' . $taskId . '/update-status', ['status' => 'telah_ditinjau'], $superToken);
    testApi($kernel, 'POST', '/api/penugasan/' . $taskId . '/delete', ['alasan' => 'Dibatalkan karena testing selesai'], $superToken);
}
testApi($kernel, 'GET', '/api/export-penugasan', [], $superToken);

echo "\n--- TESTS COMPLETED ---\n";
