<?php
namespace App\Services;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class ImageUploadService {
    private array $allowedMimes = ['image/jpeg','image/png','image/webp','image/gif'];
    private int   $maxSizeKb    = 5120; // 5 MB

    public function upload(UploadedFile $file, string $folder = 'uploads'): array {
        if (!in_array($file->getMimeType(), $this->allowedMimes)) {
            throw new \Exception('শুধুমাত্র JPG, PNG, WebP ও GIF ফাইল অনুমোদিত।');
        }
        if ($file->getSize() > $this->maxSizeKb * 1024) {
            throw new \Exception("সর্বোচ্চ {$this->maxSizeKb}KB ফাইল আপলোড করা যাবে।");
        }
        $ext      = $file->getClientOriginalExtension() ?: 'jpg';
        $name     = Str::uuid() . '.' . $ext;
        $path     = $file->storeAs($folder, $name, 'public');
        return [
            'success' => true,
            'url'     => Storage::disk('public')->url($path),
            'path'    => $path,
        ];
    }

    public function delete(string $path): bool {
        return Storage::disk('public')->delete($path);
    }
}
