<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Offers\Support;

use Illuminate\Support\Facades\Storage;
use Maestro\Core\Models\MaestroAppSetting;

/**
 * Curated preset imagery plus organisation uploads (Huisstijl › offer backgrounds).
 */
final class OfferDocumentBackgroundCatalog
{
    /**
     * Built-in tech / abstract backgrounds (HTTPS URLs).
     *
     * @return list<array{key: string, label: string, url: string}>
     */
    public static function presets(): array
    {
        return [
            ['key' => 'tech-circuit', 'label' => 'Circuit board', 'url' => 'https://images.unsplash.com/photo-1518770660439-4636190af475?auto=format&fit=crop&w=1920&q=80'],
            ['key' => 'tech-earth', 'label' => 'Earth network', 'url' => 'https://images.unsplash.com/photo-1451187580459-43490279c0fa?auto=format&fit=crop&w=1920&q=80'],
            ['key' => 'tech-server', 'label' => 'Server lights', 'url' => 'https://images.unsplash.com/photo-1558494949-ef010cbdcc31?auto=format&fit=crop&w=1920&q=80'],
            ['key' => 'tech-code', 'label' => 'Code screen', 'url' => 'https://images.unsplash.com/photo-1555949963-ff9fe0c870eb?auto=format&fit=crop&w=1920&q=80'],
            ['key' => 'tech-mesh', 'label' => 'Blue mesh', 'url' => 'https://images.unsplash.com/photo-1557804506-669a67965ba0?auto=format&fit=crop&w=1920&q=80'],
            ['key' => 'tech-abstract', 'label' => 'Abstract gradient', 'url' => 'https://images.unsplash.com/photo-1614850523296-d8c1af93db40?auto=format&fit=crop&w=1920&q=80'],
            ['key' => 'tech-lines', 'label' => 'Diagonal lines', 'url' => 'https://images.unsplash.com/photo-1620641788421-7a1c342ea42e?auto=format&fit=crop&w=1920&q=80'],
            ['key' => 'tech-dark', 'label' => 'Dark geometry', 'url' => 'https://images.unsplash.com/photo-1639322537228-f710d846310a?auto=format&fit=crop&w=1920&q=80'],
            ['key' => 'tech-night', 'label' => 'Night city', 'url' => 'https://images.unsplash.com/photo-1480714378408-67cf0d13bc1b?auto=format&fit=crop&w=1920&q=80'],
            ['key' => 'tech-glass', 'label' => 'Glass facade', 'url' => 'https://images.unsplash.com/photo-1497366216548-37526070297c?auto=format&fit=crop&w=1920&q=80'],
        ];
    }

    /**
     * @return list<string>
     */
    public static function presetUrls(): array
    {
        return array_values(array_unique(array_column(self::presets(), 'url')));
    }

    /**
     * @return list<string>
     */
    public static function customUrls(): array
    {
        $row = MaestroAppSetting::instance();
        $paths = $row->offer_document_background_paths ?? [];
        if (! is_array($paths)) {
            return [];
        }

        $out = [];
        foreach ($paths as $path) {
            if (! is_string($path) || $path === '') {
                continue;
            }
            $out[] = Storage::disk('public')->url($path);
        }

        return $out;
    }

    /**
     * @return list<string>
     */
    public static function allowedUrls(): array
    {
        return array_values(array_unique(array_merge(self::presetUrls(), self::customUrls())));
    }

    public static function isAllowed(?string $url): bool
    {
        if ($url === null || $url === '') {
            return true;
        }

        foreach (self::allowedUrls() as $allowed) {
            if ($allowed === $url) {
                return true;
            }
        }

        return false;
    }

    /**
     * @return list<array{path: string, url: string}>
     */
    public static function customEntries(): array
    {
        $row = MaestroAppSetting::instance();
        $paths = $row->offer_document_background_paths ?? [];
        if (! is_array($paths)) {
            return [];
        }

        $out = [];
        foreach ($paths as $path) {
            if (! is_string($path) || $path === '') {
                continue;
            }
            $out[] = [
                'path' => $path,
                'url' => Storage::disk('public')->url($path),
            ];
        }

        return $out;
    }
}
