<?php

namespace App\Console\Commands;

use App\Models\Files;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class filesBorrador extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'filesBorrador:delete';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Borrado de archivos, si excede tiempo';

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        $hourAgo = Carbon::now()->subHour(); // Obtén la fecha y hora hace una hora
        Log::info('==== INICIA BORRADO DE ARCHIVOS =====');
        Files::borrador()
            ->where('created_at', '<', $hourAgo)
            ->get()
            ->each(function ($item) {
                Log::info(json_encode($item));
                $item->delete();
            });
        Log::info('==== FIN BORRADO DE ARCHIVOS =====');
    }
}
