<?php

namespace App\Console\Commands;

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

class CheckUserRoles extends Command
{
    protected $signature = 'check:user-roles {email}';
    protected $description = 'Check user roles and their status in detail';

    public function handle()
    {
        $email = $this->argument('email');
        
        $user = User::where('email', $email)->first();
        
        if (!$user) {
            $this->error("User with email {$email} not found");
            return;
        }
        
        $this->info("User: {$user->name} (ID: {$user->id})");
        $this->info("User Status: {$user->status}");
        $this->info("isCustomer() result: " . ($user->isCustomer() ? 'true' : 'false'));
        
        // Get detailed role information
        $roles = DB::table('user_roles')
            ->join('roles', 'user_roles.role_id', '=', 'roles.id')
            ->where('user_roles.user_id', $user->id)
            ->select('roles.name', 'user_roles.is_active', 'user_roles.assigned_at', 'user_roles.expires_at')
            ->get();
            
        $this->info("\nRole Details:");
        if ($roles->isEmpty()) {
            $this->info("No roles assigned");
        } else {
            foreach ($roles as $role) {
                $this->info("Role: {$role->name}");
                $this->info("  Active: " . ($role->is_active ? 'Yes' : 'No'));
                $this->info("  Assigned: {$role->assigned_at}");
                $this->info("  Expires: " . ($role->expires_at ?: 'Never'));
                $this->info("");
            }
        }
    }
}
