<?php

namespace App\Console\Commands;

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

class CleanTempCertificates extends Command
{
    protected $signature = 'certificates:clean-temp';

    protected $description = 'Delete generated certificate PDFs from temp directory';

    public function handle()
    {
        $tempPath = storage_path('app/temp');

        if (!File::exists($tempPath)) {
            $this->info('Temp directory does not exist.');
            return Command::SUCCESS;
        }

        $files = File::files($tempPath);

        $deleted = 0;

        foreach ($files as $file) {
            if ($file->getExtension() === 'pdf') {
                File::delete($file->getPathname());
                $deleted++;
            }
        }

        $this->info("Deleted {$deleted} temporary certificate files.");

        return Command::SUCCESS;
    }
}
