<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Layouts;

use PpPhp\Core\Designer\Layouts\Layouts\SingleColumnLayout;
use PpPhp\Core\Designer\Layouts\Layouts\TwoColumnLayout;
use PpPhp\Core\Designer\Layouts\Layouts\KPIGridLayout;
use PpPhp\Core\Designer\Layouts\Layouts\AnalyticsGridLayout;

class DesignerLayoutRegistry
{
    /**
     * @return array<class-string<DesignerLayoutInterface>>
     */
    public static function all(): array
    {
        return [
            SingleColumnLayout::class,
            TwoColumnLayout::class,
            KPIGridLayout::class,
            AnalyticsGridLayout::class,
        ];
    }

    /**
     * @return array<class-string<DesignerLayoutInterface>>
     */
    public static function byCategory(string $category): array
    {
        return array_values(array_filter(
            self::all(),
            fn(string $layout): bool => $layout::category() === $category
        ));
    }

    public static function find(string $id): ?string
    {
        foreach (self::all() as $layout) {
            if ($layout::id() === $id) {
                return $layout;
            }
        }

        return null;
    }

    public static function metadata(): array
    {
        return array_map(
            fn(string $layout): array => [
                'id' => $layout::id(),
                'name' => $layout::name(),
                'description' => $layout::description(),
                'icon' => $layout::icon(),
                'category' => $layout::category(),
                'columns' => $layout::columns(),
            ],
            self::all()
        );
    }
}
