<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Models\VehicleDocument;

class CheckVehicleDocuments extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:check-vehicle-documents';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Check vehicle documents for expiry and update status';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $documents = VehicleDocument::all();
        $updated = 0;

        foreach ($documents as $doc) {
            $oldStatus = $doc->status;
            $newStatus = $doc->calculated_status;

            if ($oldStatus !== $newStatus) {
                $doc->update(['status' => $newStatus]);
                $updated++;
            }
        }

        $this->info("Checked " . $documents->count() . " documents. Updated $updated statuses.");
    }
}
