<?php

namespace Modules\ClinicSite\Http\Controllers\Concerns;

use Illuminate\Http\UploadedFile;

trait HandlesUploads
{
    /**
     * Store an uploaded image under public/uploads/clinic-site/{businessId}/ and
     * return its path relative to /uploads (matching asset('uploads/'.$path)).
     */
    protected function storeUpload(UploadedFile $file, int $businessId, string $prefix = 'img'): ?string
    {
        if (! $file->isValid()) {
            return null;
        }

        $dir = public_path('uploads/clinic-site/' . $businessId);
        if (! is_dir($dir)) {
            @mkdir($dir, 0755, true);
        }

        $ext  = strtolower($file->getClientOriginalExtension() ?: 'png');
        $name = $prefix . '-' . time() . '-' . substr(md5(uniqid('', true)), 0, 6) . '.' . $ext;
        $file->move($dir, $name);

        return 'clinic-site/' . $businessId . '/' . $name;
    }
}
