<?php

namespace App\Console\Commands;

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

class FixAdminUser extends Command
{
    protected $signature = 'admin:fix';
    protected $description = 'Fix or create admin user';

    public function handle()
    {
        $email = 'admin@lindsayleather.com';
        $password = 'admin123';
        
        $this->info('Checking admin user...');
        
        $admin = User::where('email', $email)->first();
        
        if ($admin) {
            $this->info('Admin user found: ' . $admin->name);
            $this->info('Current is_admin status: ' . ($admin->is_admin ? 'YES' : 'NO'));
            
            // Update password and admin status
            $admin->update([
                'password' => Hash::make($password),
                'is_admin' => true,
                'email_verified_at' => now()
            ]);
            
            $this->info('Admin user updated successfully!');
        } else {
            $this->info('Admin user not found, creating...');
            
            User::create([
                'name' => 'Lindsay Admin',
                'email' => $email,
                'password' => Hash::make($password),
                'is_admin' => true,
                'email_verified_at' => now()
            ]);
            
            $this->info('Admin user created successfully!');
        }
        
        $this->info('Login credentials:');
        $this->info('Email: ' . $email);
        $this->info('Password: ' . $password);
        
        return 0;
    }
}