<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class CleanupTempFiles extends Command
{
    protected $signature   = 'cleanup:temp {--days=3 : Delete files older than this many days}';
    protected $description = 'Remove temporary uploaded files older than N days';

    public function handle(): int
    {
        $days = (int) $this->option('days');
        $this->info("🗑️ {$days} দিনের বেশি পুরনো temp ফাইল মুছে ফেলা হচ্ছে...");

        $deleted = 0;
        $files   = Storage::disk('local')->files('temp');

        foreach ($files as $file) {
            $lastModified = Storage::disk('local')->lastModified($file);
            if (now()->timestamp - $lastModified > $days * 86400) {
                Storage::disk('local')->delete($file);
                $deleted++;
            }
        }

        $this->info("✅ মোট {$deleted} টি temp ফাইল মুছে ফেলা হয়েছে।");
        return self::SUCCESS;
    }
}
