<?php

namespace App\Domain\Shared\Branding;

/**
 * Everything a surface needs to render a tenant's brand, resolved once.
 *
 * Before this existed, each layout carried its own `@php` block issuing one
 * SettingsService::get() per field — ~16 SELECTs per admin render, repeated on
 * every Livewire round-trip — and each block chose its own fallback, so
 * primary_color had three different defaults across three files.
 *
 * Every value is a finished value: colours are validated hex, images are URLs
 * (never raw storage paths, which is what components/print/sheet.blade.php was
 * emitting into an <img src> and why the logo was broken on every printed
 * sheet), and each brand colour arrives with a shade ramp and a foreground
 * colour that actually contrasts with it.
 */
final readonly class BrandProfile
{
    /**
     * @param  array<string, ColorRamp>  $ramps  keyed by role: primary, secondary, accent…
     */
    public function __construct(
        public int $academyId,
        public int $version,
        public string $academyName,
        public string $appShortName,
        public ?string $address,
        public ?string $phone,
        public ?string $email,

        public ?string $logoUrl,
        public ?string $logoDarkUrl,
        public ?string $faviconUrl,
        public ?string $signatureUrl,
        public ?string $loginBackgroundUrl,
        public ?string $invoiceHeaderUrl,
        public ?string $appIconUrl,
        public ?string $splashImageUrl,

        public string $primary,
        public string $secondary,
        public string $accent,
        public string $sidebarBg,
        public string $sidebarText,
        public string $sidebarActive,
        public string $headerBg,
        public string $success,
        public string $warning,
        public string $danger,
        public string $themeColor,
        public string $themeMode,

        public string $fontAr,
        public string $fontEn,
        public int $fontSizeBase,

        public string $invoiceFooterText,
        public string $receiptFooterText,
        public string $termsAndConditions,

        public bool $showLogoInSidebar,
        public bool $showLogoInInvoice,
        public bool $showSignatureInInvoice,
        public bool $compactSidebar,

        public array $ramps,
    ) {}

    public function ramp(string $role): ColorRamp
    {
        return $this->ramps[$role] ?? $this->ramps['primary'];
    }

    /**
     * The `:root` custom properties every surface binds to. Emitted server-side
     * once per document; Tailwind's `@theme inline` block reads these at
     * runtime rather than baking a copy at build time.
     *
     * @return array<string, string>
     */
    public function cssVariables(): array
    {
        $vars = [
            '--brand-primary' => $this->primary,
            '--brand-secondary' => $this->secondary,
            '--brand-accent' => $this->accent,
            '--brand-sidebar-bg' => $this->sidebarBg,
            '--brand-sidebar-text' => $this->sidebarText,
            '--brand-sidebar-active' => $this->sidebarActive,
            '--brand-header-bg' => $this->headerBg,
            '--brand-success' => $this->success,
            '--brand-warning' => $this->warning,
            '--brand-danger' => $this->danger,
            '--brand-font-size' => $this->fontSizeBase . 'px',
            '--brand-font-ar' => "'{$this->fontAr}'",
            '--brand-font-en' => "'{$this->fontEn}'",
        ];

        foreach ($this->ramps as $role => $ramp) {
            $prefix = $role === 'primary' ? '--brand' : "--brand-{$role}";

            foreach ($ramp->shades as $step => $value) {
                $vars["{$prefix}-{$step}"] = $value;
            }

            $vars["{$prefix}-fg"] = $ramp->foreground;
        }

        return $vars;
    }

    public function cssVariableBlock(): string
    {
        $lines = [];

        foreach ($this->cssVariables() as $name => $value) {
            $lines[] = "    {$name}: {$value};";
        }

        return implode("\n", $lines);
    }
}
