<?php

declare(strict_types=1);

namespace App\Application\Admin;

use App\Domain\Media\Image;
use App\Domain\Media\ImageRepository;
use App\Framework\Attributes\Route;
use App\Framework\Auth\Auth;

final readonly class Images
{
    #[Auth]
    #[Route('/admin/images')]
    public function showAll(ImageRepository $imageRepository): never
    {
        $images = $imageRepository->findAll();

        echo "<div style='background-color: #222; display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); grid-gap: 16px; padding: 16px;'>";

        /** @var Image $image */
        foreach ($images as $image) {
            echo sprintf("<a href='/images/%s'>", $image->filename);

            echo "<img src='/images/" . $image->filename . "' style='width: 400px; aspect-ratio: 1; object-fit: cover; border-radius: 16px;'/>";

            echo "</a>";

            // Check if variants are loaded to avoid uninitialized property error
            try {
                $variantInfo = $image->variants[0]->filename ?? 'no variant';
                echo "<small>Variant: " . htmlspecialchars($variantInfo) . "</small>";
            } catch (\Error $e) {
                echo "<small>Variants not loaded yet</small>";
            }
        }

        echo "</div>";

        debug($images);
        die();
    }
}
