<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\User;
use App\Models\Driver;

class CreateDriverRecords extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'drivers:create-records';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create driver records for users with driver type';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->info('Creating driver records...');

        $users = User::where('user_type', 'driver')->get();
        $created = 0;
        $skipped = 0;

        foreach ($users as $user) {
            if (!Driver::where('user_id', $user->id)->exists()) {
                Driver::create([
                    'user_id' => $user->id,
                    'is_available' => true,
                    'is_verified' => false,
                    'is_active' => true,
                    'total_trips' => 0,
                    'rating' => 0.0,
                ]);
                $created++;
                $this->info("Created driver record for user: {$user->name} (ID: {$user->id})");
            } else {
                $skipped++;
                $this->info("Driver record already exists for user: {$user->name} (ID: {$user->id})");
            }
        }

        $this->info("Completed! Created: {$created}, Skipped: {$skipped}");

        return 0;
    }
}
