<?php

/**
 * Admin Login Test Script
 * 
 * This script tests the admin login functionality
 * Run: php test-admin-login.php
 */

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

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

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;

echo "=== Admin Login Test ===\n\n";

// Test 1: Check if admin user exists
echo "1. Checking admin user...\n";
$admin = User::where('email', 'admin@example.com')->first();

if ($admin) {
    echo "   ✓ Admin user found\n";
    echo "   - Email: {$admin->email}\n";
    echo "   - Role: {$admin->role}\n";
    echo "   - Is Admin: " . ($admin->isAdmin() ? 'Yes' : 'No') . "\n";
    echo "   - Is Admin or Sub-Admin: " . ($admin->isAdminOrSubAdmin() ? 'Yes' : 'No') . "\n";
} else {
    echo "   ✗ Admin user not found!\n";
    echo "   Creating admin user...\n";
    $admin = User::create([
        'name' => 'Admin User',
        'email' => 'admin@example.com',
        'password' => Hash::make('admin123'),
        'role' => 'admin',
        'email_verified_at' => now(),
    ]);
    echo "   ✓ Admin user created\n";
}

echo "\n";

// Test 2: Test password verification
echo "2. Testing password verification...\n";
$testPassword = 'admin123';
if (Hash::check($testPassword, $admin->password)) {
    echo "   ✓ Password verification successful\n";
} else {
    echo "   ✗ Password verification failed!\n";
}

echo "\n";

// Test 3: Test web guard authentication
echo "3. Testing web guard authentication...\n";
$credentials = [
    'email' => 'admin@example.com',
    'password' => 'admin123',
];

if (Auth::guard('web')->attempt($credentials)) {
    echo "   ✓ Web guard authentication successful\n";
    $user = Auth::guard('web')->user();
    echo "   - Authenticated user: {$user->name}\n";
    echo "   - User role: {$user->role}\n";
    echo "   - Is Admin or Sub-Admin: " . ($user->isAdminOrSubAdmin() ? 'Yes' : 'No') . "\n";
    
    Auth::guard('web')->logout();
    echo "   ✓ Logout successful\n";
} else {
    echo "   ✗ Web guard authentication failed!\n";
    echo "   Check credentials and user role.\n";
}

echo "\n";

// Test 4: Check sub-admin user
echo "4. Checking sub-admin user...\n";
$subAdmin = User::where('email', 'subadmin@example.com')->first();

if ($subAdmin) {
    echo "   ✓ Sub-admin user found\n";
    echo "   - Email: {$subAdmin->email}\n";
    echo "   - Role: {$subAdmin->role}\n";
    echo "   - Is Admin: " . ($subAdmin->isAdmin() ? 'Yes' : 'No') . "\n";
    echo "   - Is Sub-Admin: " . ($subAdmin->isSubAdmin() ? 'Yes' : 'No') . "\n";
    echo "   - Is Admin or Sub-Admin: " . ($subAdmin->isAdminOrSubAdmin() ? 'Yes' : 'No') . "\n";
} else {
    echo "   ✗ Sub-admin user not found!\n";
}

echo "\n";

// Test 5: Test routes
echo "5. Testing routes...\n";
$routes = [
    'admin.login' => '/admin/login',
    'admin.dashboard' => '/admin',
];

foreach ($routes as $name => $path) {
    try {
        $url = route($name);
        echo "   ✓ Route '{$name}' exists: {$url}\n";
    } catch (Exception $e) {
        echo "   ✗ Route '{$name}' error: {$e->getMessage()}\n";
    }
}

echo "\n";
echo "=== Test Complete ===\n";
echo "\n";
echo "Login Credentials:\n";
echo "  Admin: admin@example.com / admin123\n";
echo "  Sub-Admin: subadmin@example.com / subadmin123\n";
echo "\n";
echo "Access admin panel at: http://localhost:8000/admin/login\n";
