<?php

namespace App\Libraries;

class PublicImageStorage
{
    /**
     * Delete a file stored under public/ when DB holds a relative path (e.g. image/fleet/foo.jpg).
     */
    public static function delete(?string $imgPath): void
    {
        if ($imgPath === null || trim($imgPath) === '') {
            return;
        }

        $relative = ltrim(str_replace('\\', '/', $imgPath), '/');
        if (str_starts_with($relative, 'public/')) {
            $relative = substr($relative, strlen('public/'));
        }

        foreach ([FCPATH . $relative, ROOTPATH . 'public/' . $relative, $imgPath] as $diskPath) {
            if (is_file($diskPath)) {
                @unlink($diskPath);
                return;
            }
        }
    }
}
