<?php

// Test Laravel API endpoints
$baseUrl = 'http://127.0.0.1:8000/api';

function makeRequest($url, $method = 'GET', $data = null, $token = null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    
    $headers = [
        'Accept: application/json',
        'Content-Type: application/json'
    ];
    
    if ($token) {
        $headers[] = 'Authorization: Bearer ' . $token;
    }
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    if ($data) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    }
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    return ['code' => $httpCode, 'body' => $response];
}

echo "Testing Laravel API...\n\n";

// Test login
echo "1. Testing login...\n";
$loginData = [
    'email' => 'admin@test.com',
    'password' => 'password'
];

$response = makeRequest($baseUrl . '/auth/login', 'POST', $loginData);
echo "Status: " . $response['code'] . "\n";
echo "Response: " . $response['body'] . "\n\n";

if ($response['code'] == 200) {
    $loginResult = json_decode($response['body'], true);
    if (isset($loginResult['token'])) {
        $token = $loginResult['token'];
        echo "Login successful! Token: " . substr($token, 0, 20) . "...\n\n";
        
        // Test authenticated endpoint
        echo "2. Testing authenticated endpoint (categories)...\n";
        $response = makeRequest($baseUrl . '/menu/categories', 'GET', null, $token);
        echo "Status: " . $response['code'] . "\n";
        echo "Response: " . $response['body'] . "\n\n";
        
    } else {
        echo "Login failed - no token in response\n";
    }
} else {
    echo "Login failed\n";
}