<?php
require_once __DIR__ . '/../config.php';

echo "<h2>Test Credentials</h2>";

$test_credentials = [
    ['Widia', 'qwerty5*'],  // Credentials yang diberikan user
    ['admin', 'admin'],
    ['admin', 'password'],
    ['admin', '123456'],
    ['user', 'user'],
    ['user', 'password'],
    ['test', 'test'],
    ['demo', 'demo'],
    ['bij', 'bij'],
    ['karyawan', 'karyawan'],
    ['', '']
];

foreach ($test_credentials as $cred) {
    $username = $cred[0];
    $password = $cred[1];

    echo "<h3>Testing: $username / $password</h3>";

    $result = api_login($username, $password);

    echo "Success: " . ($result['success'] ? 'YES' : 'NO') . "<br>";
    echo "HTTP Code: " . $result['http_code'] . "<br>";
    echo "Response: " . json_encode($result['data'], JSON_PRETTY_PRINT) . "<br>";

    if ($result['success']) {
        echo "<strong style='color: green;'>LOGIN BERHASIL!</strong><br>";
        if (isset($result['data']['token'])) {
            echo "Token: " . substr($result['data']['token'], 0, 20) . "...<br>";
        }
        break; // Stop testing if successful
    }

    echo "<hr>";
}

echo "<h3>Test dengan format data yang berbeda</h3>";

// Test dengan format data yang mungkin berbeda
$test_data_formats = [
    ['username' => 'admin', 'password' => 'admin'],
    ['user' => 'admin', 'pass' => 'admin'],
    ['login' => 'admin', 'pwd' => 'admin'],
    ['uname' => 'admin', 'pword' => 'admin']
];

foreach ($test_data_formats as $format) {
    echo "<h4>Format: " . json_encode($format) . "</h4>";

    $ch = curl_init(API_BASE . 'login');
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
        CURLOPT_TIMEOUT => 10,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_POSTFIELDS => json_encode($format)
    ]);

    $res = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    $data = json_decode($res, true);
    echo "HTTP Code: $httpCode<br>";
    echo "Response: " . json_encode($data, JSON_PRETTY_PRINT) . "<br><br>";
}
