<?php

namespace App\Services\Ai;

/**
 * Hand-designed "design styles" for AI screenshot generation.
 *
 * Template-driven generation: instead of a procedural engine inventing a look,
 * the AI picks the best-fit STYLE for the app and we render it deterministically.
 * That's why it stays consistent + professional — the typography pairings,
 * decoration and alignment were chosen by a human; the AI only personalises copy
 * + colours.
 *
 * A style does NOT define WHERE things sit — the collision-tuned anchor geometry
 * in {@see ThemeRecipes::anchors()} is shared by every style. A style defines what
 * makes a design feel designed: the font pairing, weights, sizing, casing, the
 * decoration motif, and the alignment.
 *
 * Fonts are all in the editor's Google-Fonts library (resources/js/editor/fonts.ts)
 * and are loaded by the export renderer (ensureFontsForLayout), so they render in
 * both the editor and the exported ZIP.
 *
 * Each role's typography tuple:
 *   ['font' => string, 'weight' => int, 'lineHeight' => float,
 *    'min' => int, 'max' => int, 'spacing' => float, 'case' => 'none'|'upper'|'title']
 */
class Styles
{
    public const DEFAULT = 'modern';

    /** @return string[] */
    public static function keys(): array
    {
        return ['editorial', 'modern', 'friendly', 'lookbook'];
    }

    /**
     * A one-line description per style for the AI to choose from, each closing
     * with the creative backgrounds that pair well with it (see {@see backgroundsFor}).
     * The canonical key list + per-key match text is still rendered separately
     * from the shipped library (resources/ai/backgrounds/index.json); this just
     * biases the art director toward on-brand pairings for the chosen style.
     */
    public static function descriptionsForPrompt(): string
    {
        $desc = [
            'editorial' => 'calm, premium, content-led apps (reading, journaling, meditation, wellness, news, luxury). Elegant SERIF headlines, left-aligned, generous whitespace, almost no decoration.',
            'modern'    => 'tech & productivity (SaaS, finance, fintech, developer/AI tools, crypto, dashboards). Bold geometric SANS, centered, a soft spotlight glow behind the device — confident and clean.',
            'friendly'  => 'consumer & lifestyle (social, health & fitness, food, kids, education, shopping, travel). Rounded approachable sans, centered, soft warm glow — energetic and welcoming.',
            'lookbook'  => 'premium lifestyle / fashion / AI-consumer apps that want the polished "App Store feature" look. Elegant serif headline with an italic-accent emphasis word, left-aligned hero with a tilted device + a 5-star trust badge, and subtly varied per-screen backgrounds.',
        ];

        $lines = [];
        foreach ($desc as $key => $text) {
            $bg = self::backgroundsFor($key);
            $hint = $bg !== [] ? ' Backgrounds that pair well: '.implode(', ', $bg).'.' : '';
            $lines[] = "- {$key}: {$text}{$hint}";
        }

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

    /**
     * The creative-background keys (best-first) that suit a style — the art
     * director's pairing guidance. Keys reference the shipped library
     * (resources/ai/backgrounds/index.json); unknown keys are simply ignored by
     * {@see AiController::resolveCreativeBackground}, so a removed asset degrades
     * gracefully rather than erroring.
     *
     * @return string[]
     */
    public static function backgroundsFor(string $key): array
    {
        return self::all()[$key]['bg'] ?? [];
    }

    /** @return array<string, mixed> */
    public static function get(string $key): array
    {
        return self::all()[$key] ?? self::all()[self::DEFAULT];
    }

    /**
     * The layout-composition sequence for a style — this is what makes each style
     * look genuinely different (not the same geometry re-skinned). All entries are
     * device-bearing so every uploaded screenshot is shown.
     *
     * @return string[]
     */
    public static function sequence(string $key, int $n): array
    {
        if ($n <= 0) {
            return [];
        }
        // Lookbook: a designed rhythm — the badge-bearing lb_hero opener, a few
        // varied middles, a CTA closer.
        if ($key === 'lookbook') {
            // No `showcase` — it renders caption-only (a silent screen).
            $mid = ['feature', 'split_right', 'split_left'];
            $seq = ['lb_hero'];
            for ($i = 1; $i < $n - 1; $i++) {
                $seq[] = $mid[($i - 1) % count($mid)];
            }
            if ($n >= 2) {
                $seq[] = 'cta';
            }

            return array_slice($seq, 0, $n);
        }
        // Per-style composition pools (distinct geometry per style). Wider pools =
        // more layout variety across the deck (and between generations). No
        // `showcase`/`highlight` — they render caption/stat-only (a silent screen).
        // All entries use text slots the copy always carries (eyebrow/headline/
        // subhead/body). The rhythm step (dynamic vs uniform) runs on top of this.
        $pool = match ($key) {
            'editorial' => ['editorial_feature'],                                                  // one strong Slow-grade composition, repeated
            'friendly'  => ['hero_device_top', 'feature_device_top', 'split_left', 'feature', 'split_right'], // device-forward, playful
            default     => ['hero', 'feature', 'feature_device_top', 'split_right', 'split_left'],   // modern: centered + edge variants
        };
        $last = match ($key) {
            'editorial' => 'editorial_feature',
            'friendly'  => 'feature_device_top',
            default     => 'cta',
        };

        $seq = [];
        for ($i = 0; $i < $n; $i++) {
            $seq[] = $pool[$i % count($pool)];
        }
        if ($n >= 2) {
            $seq[$n - 1] = $last;
        }

        return $seq;
    }

    public static function isValid(string $key): bool
    {
        return isset(self::all()[$key]);
    }

    /** @return array<string, array<string, mixed>> */
    public static function all(): array
    {
        return [
            // ── Editorial — elegant serif, left-aligned, whitespace-led ────────
            // Editorial — modeled on the Slow kit: oversized Instrument Serif
            // headline, monospace numbered kicker, restrained body. Clean, no deco.
            'editorial' => [
                'name'          => 'Editorial',
                'align'         => 'left',
                'deco'          => ['family' => 'dots', 'density' => 'minimal'],
                // Restrained, lots of negative space — soft fog / band / diagonal wash,
                // plus quiet topographic line-art (contour).
                'bg'            => ['veil', 'horizon', 'corner', 'drape', 'contour'],
                'number_eyebrow' => true,
                'text'  => [
                    'headline' => ['font' => 'Instrument Serif', 'weight' => 400, 'lineHeight' => 1.0, 'min' => 64, 'max' => 230, 'spacing' => -1.0, 'case' => 'none'],
                    'subhead'  => ['font' => 'Inter',            'weight' => 400, 'lineHeight' => 1.4,  'min' => 40, 'max' => 64,  'spacing' => 0.0,  'case' => 'none'],
                    'body'     => ['font' => 'Inter',            'weight' => 400, 'lineHeight' => 1.5,  'min' => 38, 'max' => 60,  'spacing' => 0.0,  'case' => 'none'],
                    'eyebrow'  => ['font' => 'JetBrains Mono',   'weight' => 500, 'lineHeight' => 1.0,  'min' => 30, 'max' => 48,  'spacing' => 2.0,  'case' => 'upper'],
                    'stat'     => ['font' => 'Instrument Serif', 'weight' => 400, 'lineHeight' => 1.0,  'min' => 90, 'max' => 230, 'spacing' => -1.0, 'case' => 'none'],
                    'caption'  => ['font' => 'Inter',            'weight' => 400, 'lineHeight' => 1.3,  'min' => 32, 'max' => 52,  'spacing' => 0.0,  'case' => 'none'],
                ],
            ],

            // ── Modern — bold geometric sans, centered, spotlight ──────────────
            'modern' => [
                'name'  => 'Modern',
                'align' => 'center',
                'deco'  => ['family' => 'blobs', 'density' => 'medium'],
                // Confident, centered, depth — spotlight/aurora glow, bloom, cascade,
                // plus structured geometric line-art (grid/contour) for the tech feel.
                'bg'    => ['spotlight', 'aurora', 'bloom', 'split', 'cascade', 'grid', 'contour'],
                'text'  => [
                    'headline' => ['font' => 'Space Grotesk', 'weight' => 700, 'lineHeight' => 1.1,  'min' => 60, 'max' => 124, 'spacing' => -2.0, 'case' => 'none'],
                    'subhead'  => ['font' => 'Inter',         'weight' => 400, 'lineHeight' => 1.35, 'min' => 26, 'max' => 50,  'spacing' => 0.0,  'case' => 'none'],
                    'body'     => ['font' => 'Inter',         'weight' => 400, 'lineHeight' => 1.5,  'min' => 22, 'max' => 36,  'spacing' => 0.0,  'case' => 'none'],
                    'eyebrow'  => ['font' => 'Space Grotesk', 'weight' => 600, 'lineHeight' => 1.0,  'min' => 20, 'max' => 46,  'spacing' => 2.5,  'case' => 'upper'],
                    'stat'     => ['font' => 'Space Grotesk', 'weight' => 700, 'lineHeight' => 0.96, 'min' => 72, 'max' => 140, 'spacing' => -2.0, 'case' => 'none'],
                    'caption'  => ['font' => 'Space Grotesk', 'weight' => 500, 'lineHeight' => 1.1,  'min' => 32, 'max' => 56,  'spacing' => -0.5, 'case' => 'none'],
                ],
            ],

            // ── Friendly — rounded approachable sans, centered, soft glow ──────
            'friendly' => [
                'name'  => 'Friendly',
                'align' => 'center',
                'deco'  => ['family' => 'blobs', 'density' => 'rich'],
                // Warm, energetic, playful — scattered orbs, mesh, side light, plus
                // an optimistic geometric sunburst (rays).
                'bg'    => ['bokeh', 'aurora', 'dune', 'tide', 'rays'],
                'text'  => [
                    'headline' => ['font' => 'Poppins',      'weight' => 700, 'lineHeight' => 1.05, 'min' => 56, 'max' => 116, 'spacing' => -1.0, 'case' => 'none'],
                    'subhead'  => ['font' => 'Nunito Sans',  'weight' => 400, 'lineHeight' => 1.4,  'min' => 26, 'max' => 48,  'spacing' => 0.0,  'case' => 'none'],
                    'body'     => ['font' => 'Nunito Sans',  'weight' => 400, 'lineHeight' => 1.5,  'min' => 22, 'max' => 36,  'spacing' => 0.0,  'case' => 'none'],
                    'eyebrow'  => ['font' => 'Poppins',      'weight' => 600, 'lineHeight' => 1.0,  'min' => 20, 'max' => 30,  'spacing' => 2.0,  'case' => 'upper'],
                    'stat'     => ['font' => 'Poppins',      'weight' => 800, 'lineHeight' => 0.98, 'min' => 72, 'max' => 138, 'spacing' => -1.0, 'case' => 'none'],
                    'caption'  => ['font' => 'Poppins',      'weight' => 600, 'lineHeight' => 1.1,  'min' => 32, 'max' => 56,  'spacing' => -0.5, 'case' => 'none'],
                ],
            ],

            // ── Lookbook — reference-grade editorial (PROOF). Serif headline with
            //    an italic-accent emphasis word, a left-aligned hero with a tilted
            //    device + trust badge, per-screen background variation. The premium
            //    "App Store feature" look (Wearly / lifestyle apps). ──
            'lookbook' => [
                'name'           => 'Lookbook',
                'align'          => 'left',
                'deco'           => ['family' => 'blobs', 'density' => 'medium'],
                // Polished "App Store feature" depth — mesh, bloom, spotlight, drape.
                'bg'             => ['aurora', 'bloom', 'spotlight', 'drape'],
                'emphasis'       => true,
                'emphasis_italic' => true,
                'vary_bg'        => true,
                'badge'          => true,
                'text'  => [
                    'headline' => ['font' => 'Instrument Serif', 'weight' => 400, 'lineHeight' => 1.0, 'min' => 60, 'max' => 200, 'spacing' => -1.0, 'case' => 'none'],
                    'subhead'  => ['font' => 'Inter',            'weight' => 400, 'lineHeight' => 1.4, 'min' => 34, 'max' => 56,  'spacing' => 0.0,  'case' => 'none'],
                    'body'     => ['font' => 'Inter',            'weight' => 400, 'lineHeight' => 1.5, 'min' => 32, 'max' => 52,  'spacing' => 0.0,  'case' => 'none'],
                    'eyebrow'  => ['font' => 'Inter',            'weight' => 600, 'lineHeight' => 1.0, 'min' => 28, 'max' => 42,  'spacing' => 2.0,  'case' => 'upper'],
                    'stat'     => ['font' => 'Instrument Serif', 'weight' => 400, 'lineHeight' => 1.0, 'min' => 90, 'max' => 200, 'spacing' => -1.0, 'case' => 'none'],
                    'caption'  => ['font' => 'Inter',            'weight' => 500, 'lineHeight' => 1.3, 'min' => 32, 'max' => 50,  'spacing' => 0.0,  'case' => 'none'],
                ],
            ],
        ];
    }
}
