<?php

require __DIR__ . '/vendor/autoload.php';

$app = require_once __DIR__ . '/bootstrap/app.php';
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();

use App\Models\User;
use App\Models\Attendance;
use Illuminate\Http\Request;

$user = User::where('email', 'riel.msf28@gmail.com')->first();

// Create a mock request with the user
$request = Request::create('/api/attendance/red-flags', 'GET');
$request->setUserResolver(fn() => $user);

// Call the controller
$controller = new \App\Http\Controllers\Api\AttendanceController(new \App\Services\AttendanceService());
$response = $controller->getRedFlags($request);

// Get response content
$content = json_decode($response->getContent(), true);

echo "=== RED FLAGS API RESPONSE ===\n\n";
echo "Total count: " . ($content['count'] ?? 0) . "\n\n";

if (!empty($content['red_flags'])) {
    foreach ($content['red_flags'] as $flag) {
        echo "---\n";
        echo "Type: {$flag['type']}\n";
        echo "Date: {$flag['date']} ({$flag['date_display']})\n";
        echo "Title: {$flag['title']}\n";
        echo "Description: {$flag['description']}\n";

        if ($flag['type'] === 'wrong_location') {
            if (isset($flag['wrong_checkin_location'])) {
                echo "Wrong Check-in Location:\n";
                echo "  - Lat: {$flag['wrong_checkin_location']['latitude']}\n";
                echo "  - Lng: {$flag['wrong_checkin_location']['longitude']}\n";
                echo "  - Distance: {$flag['wrong_checkin_location']['distance']}m\n";
            }
            if (isset($flag['wrong_checkout_location'])) {
                echo "Wrong Check-out Location:\n";
                echo "  - Lat: {$flag['wrong_checkout_location']['latitude']}\n";
                echo "  - Lng: {$flag['wrong_checkout_location']['longitude']}\n";
                echo "  - Distance: {$flag['wrong_checkout_location']['distance']}m\n";
            }
            if (isset($flag['expected_location'])) {
                echo "Expected Location:\n";
                echo "  - Name: {$flag['expected_location']['name']}\n";
                echo "  - Lat: {$flag['expected_location']['latitude']}\n";
                echo "  - Lng: {$flag['expected_location']['longitude']}\n";
                echo "  - Radius: {$flag['expected_location']['radius']}m\n";
            }
        }
        echo "\n";
    }
} else {
    echo "No red flags found!\n";
}

echo "\n=== SETTINGS ===\n";
print_r($content['settings'] ?? []);
