<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Dashboard;

final class DesignerDashboardLayout
{
    public function __construct(
        private readonly string $type = 'grid12',
        private readonly array $spans = [4, 4, 4],
        private readonly int $gap = 16,
        private readonly array $breakpoints = []
    ) {}

    public static function preset(string $preset): self
    {
        return match ($preset) {
            '6+6' => new self('grid12', [6, 6]),
            '3+3+3+3' => new self('grid12', [3, 3, 3, 3]),
            '8+4' => new self('grid12', [8, 4]),
            '9+3' => new self('grid12', [9, 3]),
            default => new self('grid12', [4, 4, 4]),
        };
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (string) ($data['type'] ?? 'grid12'),
            (array) ($data['spans'] ?? [4, 4, 4]),
            (int) ($data['gap'] ?? 16),
            (array) ($data['breakpoints'] ?? ['xs' => 1, 'sm' => 1, 'md' => 2, 'lg' => 3])
        );
    }

    public function spans(): array { return $this->spans; }

    public function toArray(): array
    {
        return [
            'type' => $this->type,
            'spans' => $this->spans,
            'gap' => $this->gap,
            'breakpoints' => $this->breakpoints ?: ['xs' => 1, 'sm' => 1, 'md' => 2, 'lg' => 3],
            'supportedSpans' => [3, 4, 6, 8, 9, 12],
        ];
    }
}
