<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Mtc\MercuryDataModels\NewCar;

class FixNewCarsOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fix:new-cars-order';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Fix order for new cars - replacing null with actual order value';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $currentMaxOrder = NewCar::max('order') ?? 0;

        $newCarsToFix = NewCar::query()
            ->whereNull('order')
            ->orderBy('id', 'asc')
            ->get();

        $newOrder = $currentMaxOrder + 1;

        foreach ($newCarsToFix as $newCar) {
            $newCar->order = $newOrder++;
            $newCar->save();
        }

        $this->info(count($newCarsToFix) . ' new cars updated with new order values.');
    }
}
