<?php

use App\Models\AppSetting;
use App\Models\Tenant;
use Illuminate\Support\Facades\Storage;

if (! function_exists('appLogoUrl')) {
    /**
     * Get the public URL for the app logo at a given size.
     * Returns null if no logo has been uploaded.
     */
    function appLogoUrl(string $size = 'original'): ?string
    {
        $key   = "app_logo_{$size}";
        $value = AppSetting::getValue($key);
        return $value ? Storage::url($value) : null;
    }
}

if (! function_exists('tenant')) {
    /**
     * Get the current tenant from the app container.
     *
     * @return Tenant
     * @throws RuntimeException
     */
    function tenant(): Tenant
    {
        if (! app()->has('tenant')) {
            throw new RuntimeException('No tenant resolved for this request.');
        }

        return app('tenant');
    }
}
