[Back]
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Str;

class BlockController extends Controller
{
    public function gallery($block)
    {
        $block = strtolower($block);
        abort_unless(in_array($block, ['a','b','c','d']), 404);

        // Titles per block (edit as needed)
        $titlesByBlock = [
            'a' => [
                'Textile Gallery',
                'Crafts Gallery',
                'Historical Gallery',
                'Royalty Gallery',
            ],
            'b' => [
                'Temporary Gallery',
                'Nature Rescources and Economy Gallery',
            ],
            'c' => [
                // 'Islamic Gallery',
                // 'Petroleum Gallery',
            ],
            'd' => [
                // Typo in prompt looked like "Gelari Generai Muda".
                // If you meant "Galeri Generasi Muda", change it here:
                // 'Galeri Generasi Muda',
            ],
        ];

        $titles = $titlesByBlock[$block] ?? [];

        // Build items; prefer slug-named image, else numeric, else placeholder
        $items = collect($titles)->map(function ($title, $index) use ($block) {
            $slug = Str::slug($title);

            $slugImageRel = "assets/img/blocks/{$block}/{$slug}.jpeg";
            $numImageRel  = "assets/img/blocks/{$block}/" . ($index + 1) . ".jpg";
            $fallbackRel  = "assets/img/placeholder.jpg"; // create this if you want

            $imageRel = file_exists(public_path($slugImageRel))
                ? $slugImageRel
                : (file_exists(public_path($numImageRel)) ? $numImageRel : $fallbackRel);

            return [
                'title' => $title,
                'slug'  => $slug,
                'image' => asset($imageRel),
                'desc'  => 'Short description or label for this area/floor.',
            ];
        })->values();

        return view('block.gallery.index', [
            'blockName' => strtoupper($block),
            'items'     => $items,
        ]);
    }

    // public function galleryShow($block, $slug)
    // {
    //     $block = strtolower($block);
    //     abort_unless(in_array($block, ['a','b','c','d']), 404);

    //     // You can reuse the same titles list to derive a pretty title:
    //     $pretty = Str::of($slug)->headline(); // e.g., "Galeri Tekstil"

    //     // Resolve image by slug first, then fallback to 1.jpg, or a placeholder
    //     $slugImageRel = "assets/img/blocks/{$block}/{$slug}.jpeg";
    //     $numImageRel  = "assets/img/blocks/{$block}/1.jpg";
    //     $fallbackRel  = "assets/img/placeholder.jpg";

    //     $imageRel = file_exists(public_path($slugImageRel))
    //         ? $slugImageRel
    //         : (file_exists(public_path($numImageRel)) ? $numImageRel : $fallbackRel);

    //     return view('block.gallery.show', [
    //         'blockName' => strtoupper($block),
    //         'title'     => (string) $pretty,
    //         'image'     => asset($imageRel),
    //         'slug'      => $slug,
    //         // Optional: add $prevSlug / $nextSlug if you want prev/next buttons
    //     ]);
    // }

    public function galleryShow($block, $slug)
    {
        $block = strtolower($block);
        abort_unless(in_array($block, ['a','b','c','d']), 404);

        $pretty = Str::of($slug)->headline();

        $dirPath = public_path("assets/img/blocks/{$block}/{$slug}");
        $images = [];

        if (is_dir($dirPath)) {
            $files = scandir($dirPath);
            foreach ($files as $file) {
                if (preg_match('/\.(jpg|jpeg|png|gif)$/i', $file)) {
                    $images[] = asset("assets/img/blocks/{$block}/{$slug}/{$file}");
                }
            }
        }

        if (empty($images)) {
            $images[] = asset("assets/img/placeholder.jpg");
        }

        // Short descriptions (can expand this)
        $descriptions = [
            'textile-gallery' => 'Introduces the beauty of traditional weaving such as songket and batik of Terengganu — complete with patterns, colors, and inherited techniques.',
            'crafts-gallery' => 'Showcases traditional handicrafts such as weaving, pottery, and woodcarving that reflect the artistry and craftsmanship of Terengganu’s people.',
            'historical-gallery' => 'Tells the historical journey of Terengganu from its early days to the present, including the social, political, and cultural development of its society.',
            'royalty-gallery' => 'Displays the history of Terengganu’s royal institution through personal collections, royal attire, regalia, and stories of the sultans’ reigns.',
            'temporary-gallery' => 'Used for themed or special exhibitions that change from time to time — a space to introduce new materials and ideas to visitors.',
            'nature-rescources-and-economy-gallery' => 'Emphasizes Terengganu’s natural wealth, including marine, forest, and mineral resources, and how they contribute to the state’s economy.',
            
            // 'foyer' => 'Welcome area introducing visitors to the Heritage Museum.',

        ];

        $desc = $descriptions[$slug] ?? 'Explore the cultural richness and artifacts within this gallery.';

        return view('block.gallery.show', [
            'blockName' => strtoupper($block),
            'title' => (string) $pretty,
            'images' => $images,
            'slug' => $slug,
            'desc' => $desc,
        ]);
    }

}