<?php

namespace App\Console\Commands;

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

class TestLogin extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:login';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Test admin login functionality';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->info('Testing admin login...');

        // البحث عن المستخدم المدير
        $adminUser = User::where('user_type', 'admin')->first();

        if (!$adminUser) {
            $this->error('No admin user found!');
            return 1;
        }

        $this->info("Found admin user: {$adminUser->name} ({$adminUser->email})");

        // محاولة تسجيل الدخول
        if (Auth::attempt(['email' => $adminUser->email, 'password' => '123321001'])) {
            $this->info('Login successful!');
            $this->info("User ID: " . Auth::id());
            $this->info("User type: " . Auth::user()->user_type);
            $this->info("User status: " . Auth::user()->status);

            // تسجيل الخروج
            Auth::logout();
            $this->info('Logged out successfully');
        } else {
            $this->error('Login failed!');
            return 1;
        }

        return 0;
    }
}
