<?php

namespace App\Support;

use Illuminate\Support\Facades\DB;

/**
 * إعدادات الشعار والأيقونة واسم الموقع من جدول settings (قراءة مرة لكل طلب عبر scoped binding).
 */
final class SiteBranding
{
    private const KEYS = ['app_name', 'site_logo', 'site_favicon'];

    /** @var array<string, string|null> */
    private array $rows;

    public function __construct(array $rows)
    {
        $this->rows = $rows;
    }

    public static function fromDatabase(): self
    {
        $rows = DB::table('settings')
            ->whereIn('key', self::KEYS)
            ->pluck('value', 'key')
            ->all();

        return new self($rows);
    }

    public function name(): string
    {
        $n = $this->rows['app_name'] ?? null;

        return is_string($n) && $n !== ''
            ? $n
            : (string) config('app.name', 'Doctor Queue');
    }

    public function logoUrl(): ?string
    {
        $p = $this->rows['site_logo'] ?? null;

        return is_string($p) && $p !== ''
            ? asset('storage/'.$p)
            : null;
    }

    public function faviconUrl(): ?string
    {
        $p = $this->rows['site_favicon'] ?? null;

        return is_string($p) && $p !== ''
            ? asset('storage/'.$p)
            : null;
    }

    public function hasLogo(): bool
    {
        return $this->logoUrl() !== null;
    }

    public function hasFavicon(): bool
    {
        return $this->faviconUrl() !== null;
    }
}
