<?php

namespace Modules\Vrc\Services\Adapters\Erp;

use App\Media;
use Modules\Vrc\Services\Contracts\MediaServiceContract;

/**
 * ERP adapter: stores VRC attachments through App\Media (polymorphic media
 * table, public/uploads/media), reusing the ERP upload pipeline. When the
 * SmartStorage module is active the underlying disk is swapped transparently.
 */
class ErpMediaService implements MediaServiceContract
{
    public function upload(int $businessId, $model, $request, string $inputName, bool $isSingle = false): void
    {
        Media::uploadMedia($businessId, $model, $request, $inputName, $isSingle);
    }

    public function storeBase64(string $base64): ?string
    {
        // Accept a data-URI ("data:image/png;base64,....") or a raw payload.
        if (str_contains($base64, ',')) {
            $base64 = substr($base64, strpos($base64, ',') + 1);
        }

        return Media::uploadBase64Image($base64) ?: null;
    }
}
