<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\User;
use Illuminate\Support\Facades\Hash;

class TestUserAuth extends Command
{
    protected $signature = 'test:user-auth {email} {password}';
    protected $description = 'Test user authentication with given credentials';

    public function handle()
    {
        $email = $this->argument('email');
        $password = $this->argument('password');

        $user = User::where('email', $email)->first();

        if (!$user) {
            $this->error("User with email {$email} not found!");
            return 1;
        }

        $this->info("User found: {$user->name} ({$user->email})");
        $this->info("User ID: {$user->id}");
        $this->info("User Status: {$user->status}");
        $this->info("Password Hash: " . substr($user->password, 0, 30) . "...");
        
        // Test password
        if (Hash::check($password, $user->password)) {
            $this->info("✅ Password matches!");
        } else {
            $this->error("❌ Password does NOT match!");
        }

        // Check roles
        $roles = $user->roles->pluck('name');
        $this->info("Roles: " . $roles->join(', '));

        // Test if user can login
        if (auth()->attempt(['email' => $email, 'password' => $password])) {
            $this->info("✅ Authentication successful!");
            auth()->logout();
        } else {
            $this->error("❌ Authentication failed!");
        }

        return 0;
    }
}
