<?php

// MINIMAL TEST ROUTE - ONLY SESSION MIDDLEWARE
Route::middleware(['web'])->get('/test-login', function () {
    // Find user
    $user = \App\Models\User::where('name', 'testuser')->first();

    if (!$user) {
        return 'User not found';
    }

    // Manually log in
    \Illuminate\Support\Facades\Auth::login($user);

    return response()->json([
        'logged_in' => \Illuminate\Support\Facades\Auth::check(),
        'user' => $user->name,
        'session_id' => session()->getId(),
    ]);
});

Route::middleware(['web'])->get('/test-check', function () {
    return response()->json([
        'logged_in' => \Illuminate\Support\Facades\Auth::check(),
        'user' => \Illuminate\Support\Facades\Auth::user()?->name,
        'session_id' => session()->getId(),
        'cookie_test' => request()->cookie('test_cookie'),
        'manual_cookie' => request()->cookie('manual_cookie'),
    ]);
});

// Test route that returns plain text to see if middleware applies
Route::middleware(['web'])->get('/test-simple', function () {
    session()->put('simple_test', 'value');
    return 'Session ID: ' . session()->getId();
});

// Test route with minimal JSON
Route::middleware(['web'])->get('/test-json', function () {
    session()->put('json_test', 'value');
    return response()->json(['session_id' => session()->getId()]);
});

// Test route with Auth::login
Route::middleware(['web'])->get('/test-auth', function () {
    $user = \App\Models\User::where('name', 'testuser')->first();
    if ($user) {
        \Illuminate\Support\Facades\Auth::login($user);
    }
    return response()->json([
        'logged_in' => \Illuminate\Support\Facades\Auth::check(),
        'session_id' => session()->getId(),
    ]);
});
