<?php

namespace App\Support;

/**
 * Generates inline SVG placeholder illustrations for Marketplace packages
 * that have no uploaded thumbnail/screenshot yet.
 *
 * Usage:
 *   echo MarketplacePlaceholder::render('blog', '#2563eb', '#8b5cf6');
 *
 * Six categories (matching marketplace-api.md):
 *   blog | shop | portfolio | dashboard | widget | content
 */
class MarketplacePlaceholder
{
    private const CATEGORIES = ['blog', 'shop', 'portfolio', 'dashboard', 'widget', 'content'];

    private const LABELS = [
        'blog'      => 'Blog / News',
        'shop'      => 'Shop / E-Commerce',
        'portfolio' => 'Portfolio / Creative',
        'dashboard' => 'Dashboard / Admin',
        'widget'    => 'UI-Widget',
        'content'   => 'Content / Editor',
    ];

    /**
     * Returns the full placeholder HTML (wrapper div + SVG + label).
     *
     * @param string $category   One of the 6 category slugs. Falls back to 'content'.
     * @param string $primary    Hex colour for the SVG stroke (e.g. '#2563eb').
     * @param string $accent     Hex colour for the radial gradient highlight.
     * @param string $packageName Optional package name shown in the placeholder (Hero-detail fallback).
     * @param string $slideLabel  Optional badge text top-right (e.g. 'Blog-Screenshot fehlt').
     */
    public static function render(
        string $category    = 'content',
        string $primary     = '#2563eb',
        string $accent      = '#8b5cf6',
        string $packageName = '',
        string $slideLabel  = ''
    ): string {
        if (!in_array($category, self::CATEGORIES, true)) {
            $category = 'content';
        }

        $accentSoft  = self::hexToRgba($accent,  0.12);
        $primarySoft = self::hexToRgba($primary, 0.10);
        $label       = self::LABELS[$category];
        $svg         = self::svg($category);

        $style = implode(';', [
            "--ph-stroke:{$primary}",
            "--ph-accent-soft:{$accentSoft}",
            "--ph-primary-soft:{$primarySoft}",
        ]);

        $slideBadge = $slideLabel !== ''
            ? '<span class="mp-placeholder__slide-label">' . e($slideLabel) . '</span>'
            : '';

        $nameBadge = $packageName !== ''
            ? '<span class="mp-placeholder__name">' . e($packageName) . '</span>'
            : '';

        return '<div class="mp-placeholder" style="' . $style . '">'
            . $svg
            . '<span class="mp-placeholder__label">' . e($label) . '</span>'
            . $slideBadge
            . $nameBadge
            . '</div>';
    }

    /**
     * Returns just the raw SVG string for a given category.
     */
    public static function svg(string $category): string
    {
        return match ($category) {
            'blog'      => self::svgBlog(),
            'shop'      => self::svgShop(),
            'portfolio' => self::svgPortfolio(),
            'dashboard' => self::svgDashboard(),
            'widget'    => self::svgWidget(),
            default     => self::svgContent(),
        };
    }

    // ─────────────────────────────────────────────────────────────────
    // SVG definitions — Line-Art, 200×200 viewBox, currentColor stroke
    // ─────────────────────────────────────────────────────────────────

    private static function svgBlog(): string
    {
        return '<svg viewBox="0 0 200 200" fill="none" stroke="currentColor" stroke-width="4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">'
            . '<rect x="30" y="36" width="140" height="128" rx="6"/>'
            . '<line x1="30" y1="72" x2="170" y2="72"/>'
            . '<line x1="48" y1="52" x2="110" y2="52"/>'
            . '<rect x="48" y="86" width="48" height="38" rx="3" fill="currentColor" fill-opacity="0.14" stroke="none"/>'
            . '<line x1="104" y1="90" x2="152" y2="90"/>'
            . '<line x1="104" y1="102" x2="152" y2="102"/>'
            . '<line x1="104" y1="114" x2="138" y2="114"/>'
            . '<line x1="48" y1="138" x2="152" y2="138"/>'
            . '<line x1="48" y1="150" x2="128" y2="150"/>'
            . '</svg>';
    }

    private static function svgShop(): string
    {
        return '<svg viewBox="0 0 200 200" fill="none" stroke="currentColor" stroke-width="4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">'
            . '<path d="M46 62 L58 46 H142 L154 62"/>'
            . '<path d="M40 62 H160 L150 160 A6 6 0 0 1 144 166 H56 A6 6 0 0 1 50 160 Z"/>'
            . '<path d="M78 62 V82 A22 22 0 0 0 122 82 V62"/>'
            . '<circle cx="82" cy="128" r="4" fill="currentColor" stroke="none"/>'
            . '<circle cx="118" cy="128" r="4" fill="currentColor" stroke="none"/>'
            . '<path d="M74 100 H126" stroke-opacity="0.35"/>'
            . '</svg>';
    }

    private static function svgPortfolio(): string
    {
        return '<svg viewBox="0 0 200 200" fill="none" stroke="currentColor" stroke-width="4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">'
            . '<rect x="30" y="52" width="72" height="52" rx="4" fill="currentColor" fill-opacity="0.14" stroke="none"/>'
            . '<rect x="30" y="52" width="72" height="52" rx="4"/>'
            . '<rect x="114" y="40" width="56" height="80" rx="4"/>'
            . '<rect x="30" y="116" width="52" height="48" rx="4"/>'
            . '<rect x="94" y="132" width="76" height="32" rx="4" fill="currentColor" fill-opacity="0.14" stroke="none"/>'
            . '<rect x="94" y="132" width="76" height="32" rx="4"/>'
            . '<circle cx="56" cy="74" r="7"/>'
            . '<path d="M38 98 L58 84 L78 98" stroke-opacity="0.6"/>'
            . '<path d="M126 112 L140 100 L154 112" stroke-opacity="0.6"/>'
            . '</svg>';
    }

    private static function svgDashboard(): string
    {
        return '<svg viewBox="0 0 200 200" fill="none" stroke="currentColor" stroke-width="4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">'
            . '<rect x="28" y="36" width="144" height="128" rx="8"/>'
            . '<line x1="28" y1="64" x2="172" y2="64"/>'
            . '<circle cx="44" cy="50" r="3" fill="currentColor" stroke="none"/>'
            . '<circle cx="56" cy="50" r="3" fill="currentColor" stroke="none"/>'
            . '<path d="M44 140 V112 M62 140 V96 M80 140 V124 M98 140 V84 M116 140 V108 M134 140 V92 M152 140 V118" stroke-opacity="0.55"/>'
            . '<path d="M44 108 L62 92 L80 120 L98 80 L116 104 L134 88 L152 114"/>'
            . '<circle cx="98" cy="80" r="5" fill="currentColor" stroke="none"/>'
            . '</svg>';
    }

    private static function svgWidget(): string
    {
        return '<svg viewBox="0 0 200 200" fill="none" stroke="currentColor" stroke-width="4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">'
            . '<rect x="30" y="30" width="64" height="64" rx="6" fill="currentColor" fill-opacity="0.14" stroke="none"/>'
            . '<rect x="30" y="30" width="64" height="64" rx="6"/>'
            . '<rect x="106" y="30" width="64" height="40" rx="6"/>'
            . '<rect x="106" y="82" width="64" height="44" rx="6" fill="currentColor" fill-opacity="0.22" stroke="none"/>'
            . '<rect x="106" y="82" width="64" height="44" rx="6"/>'
            . '<rect x="30" y="106" width="40" height="60" rx="6"/>'
            . '<rect x="82" y="138" width="88" height="28" rx="6"/>'
            . '<circle cx="62" cy="62" r="10" stroke-opacity="0.5"/>'
            . '</svg>';
    }

    private static function svgContent(): string
    {
        return '<svg viewBox="0 0 200 200" fill="none" stroke="currentColor" stroke-width="4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">'
            . '<path d="M40 158 L40 128 L128 40 A14 14 0 0 1 148 40 L160 52 A14 14 0 0 1 160 72 L72 160 L40 160 Z"/>'
            . '<line x1="122" y1="48" x2="152" y2="78"/>'
            . '<line x1="48" y1="146" x2="62" y2="146" stroke-opacity="0.5"/>'
            . '<line x1="48" y1="134" x2="72" y2="134" stroke-opacity="0.35"/>'
            . '</svg>';
    }

    // ─────────────────────────────────────────────────────────────────
    // Helpers
    // ─────────────────────────────────────────────────────────────────

    private static function hexToRgba(string $hex, float $alpha): string
    {
        $hex = ltrim($hex, '#');
        if (strlen($hex) === 3) {
            $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];
        }
        if (strlen($hex) !== 6) {
            return "rgba(37,99,235,{$alpha})";
        }
        $r = hexdec(substr($hex, 0, 2));
        $g = hexdec(substr($hex, 2, 2));
        $b = hexdec(substr($hex, 4, 2));
        return "rgba({$r},{$g},{$b},{$alpha})";
    }
}
