<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Layout;

final class DesignerLayoutRuntime
{
    private array $operations = [];

    public function createRow(string $rowId, string $template = 'one'): array
    {
        $row = DesignerLayoutTemplate::fromName($template, $rowId);
        $this->operations[] = DesignerLayoutOperation::success('createRow', ['rowId' => $rowId, 'template' => $template, 'totalSpan' => $row->totalSpan()])->toArray();
        return $row->toComponent();
    }

    public function splitColumn(array $rowComponent, int $columnIndex, array $spans): array
    {
        $updated = DesignerGridRow::fromArray($this->rowDataFromComponent($rowComponent))->split($columnIndex, $spans);
        $this->operations[] = DesignerLayoutOperation::success('splitColumn', ['rowId' => $updated->rowId(), 'columnIndex' => $columnIndex, 'spans' => $spans])->toArray();
        return $updated->toComponent();
    }

    public function mergeColumns(array $rowComponent, int $fromIndex, int $toIndex): array
    {
        $updated = DesignerGridRow::fromArray($this->rowDataFromComponent($rowComponent))->merge($fromIndex, $toIndex);
        $this->operations[] = DesignerLayoutOperation::success('mergeColumns', ['rowId' => $updated->rowId(), 'fromIndex' => $fromIndex, 'toIndex' => $toIndex])->toArray();
        return $updated->toComponent();
    }

    public function resizeColumn(array $rowComponent, int $columnIndex, int $span): array
    {
        $updated = DesignerGridRow::fromArray($this->rowDataFromComponent($rowComponent))->resize($columnIndex, $span);
        $this->operations[] = DesignerLayoutOperation::success('resizeColumn', ['rowId' => $updated->rowId(), 'columnIndex' => $columnIndex, 'span' => $span])->toArray();
        return $updated->toComponent();
    }

    public function reorderColumn(array $rowComponent, int $fromIndex, int $toIndex): array
    {
        $updated = DesignerGridRow::fromArray($this->rowDataFromComponent($rowComponent))->reorder($fromIndex, $toIndex);
        $this->operations[] = DesignerLayoutOperation::success('reorderColumn', ['rowId' => $updated->rowId(), 'fromIndex' => $fromIndex, 'toIndex' => $toIndex])->toArray();
        return $updated->toComponent();
    }

    public function addRowToPage(array $page, string $rowId, string $template = 'one', int $position = -1): array
    {
        $row = $this->createRow($rowId, $template);
        $components = array_values((array) ($page['components'] ?? []));
        if ($position < 0 || $position >= count($components)) $components[] = $row; else array_splice($components, $position, 0, [$row]);
        $page['components'] = $components;
        $this->operations[] = DesignerLayoutOperation::success('addRowToPage', ['rowId' => $rowId, 'template' => $template, 'position' => $position])->toArray();
        return $page;
    }

    public function operations(): array { return $this->operations; }
    public function stats(): array { return ['operations' => count($this->operations), 'successfulOperations' => count(array_filter($this->operations, static fn (array $op): bool => ($op['success'] ?? false) === true))]; }

    private function rowDataFromComponent(array $component): array
    {
        return ['rowId' => (string) ($component['id'] ?? $component['rowId'] ?? 'row'), 'columns' => (array) ($component['props']['columns'] ?? $component['props']['cells'] ?? $component['columns'] ?? []), 'settings' => (array) ($component['props']['settings'] ?? $component['settings'] ?? []), 'style' => (array) ($component['style'] ?? [])];
    }
}
