<?php

namespace App\Modules\Customization\CustomFields\Services;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class CustomFieldPrivateFileStorage
{
    public function store(UploadedFile $file, int $tenantId, int $fieldId): string
    {
        $ext = Str::lower($file->getClientOriginalExtension() ?: 'bin');
        $name = Str::uuid()->toString().'.'.$ext;
        $dir = 'tenants/'.$tenantId.'/custom-fields/'.$fieldId;

        return $file->storeAs($dir, $name, ['disk' => 'local']);
    }

    public function deleteIfExists(?string $relativePath): void
    {
        if ($relativePath === null || $relativePath === '') {
            return;
        }
        Storage::disk('local')->delete($relativePath);
    }
}
