<?php

declare(strict_types=1);

namespace Maestro\Core\Support;

/**
 * Resolved paths inside the maestro/core Composer package root.
 *
 * Use {@see self::packageRoot()} for loading non-PSR paths (e.g. `routes/modules/*.php`) from any
 * service provider; depth-based `dirname(__DIR__, N)` from nested `Modules/* /Providers` breaks if
 * the module tree is reorganized.
 */
final class CorePaths
{
    public static function packageRoot(): string
    {
        return dirname(__DIR__, 2);
    }

    /**
     * Absolute path to a file under the package root (config, routes, stubs, …).
     *
     * @throws \RuntimeException when the installed package tree is incomplete
     */
    public static function packageFile(string $relative): string
    {
        $path = self::packageRoot().'/'.ltrim(str_replace('\\', '/', $relative), '/');

        if (! is_file($path)) {
            throw new \RuntimeException(
                'Maestro CORE package file missing: '.$relative.'. '
                .'Reinstall the package (composer reinstall maestro/core) — '
                .'an incomplete vendor tree often causes this after composer update.',
            );
        }

        return $path;
    }

    public static function brandingAssetPath(string $filename): ?string
    {
        $path = self::packageRoot().'/public/vendor/maestro/branding/'.$filename;

        return is_file($path) ? $path : null;
    }
}
