<?php

namespace App\Console\Commands;

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

class DeleteTempProductFiles extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:delete-temp-product-files';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Delete temporary product images and cover images';

    /**
     * Execute the console command.
     */

    public function handle()
    {
        $tempImagePath = public_path('uploads/tamp_product_image');
        $tempCoverPath = public_path('uploads/tamp_product_cover_image');
        $tempdocumentsPath = public_path('uploads/tamp_product_documents');

        $this->deleteFilesInDirectory($tempImagePath);
        $this->deleteFilesInDirectory($tempCoverPath);
        $this->deleteFilesInDirectory($tempdocumentsPath);

        $this->info('Temporary product files deleted successfully.');
    }

    protected function deleteFilesInDirectory($directory)
    {
        if (File::exists($directory)) {
            $files = File::files($directory);
            foreach ($files as $file) {
                File::delete($file);
            }
        }
    }
}
