<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\DragDrop;

final class DesignerDropTarget
{
    public function __construct(
        private readonly string $targetType,
        private readonly array $path = [],
        private readonly string $position = 'inside',
        private readonly ?int $index = null,
        private readonly array $meta = []
    ) {}

    public static function page(?int $index = null): self
    {
        return new self('page', ['components'], 'inside', $index);
    }

    public static function container(array $path, string $position = 'inside', ?int $index = null, array $meta = []): self
    {
        return new self('container', $path, $position, $index, $meta);
    }

    public static function column(array $path, ?int $index = null, array $meta = []): self
    {
        return new self('column', $path, 'inside', $index, $meta);
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (string)($data['targetType'] ?? 'page'),
            (array)($data['path'] ?? ['components']),
            (string)($data['position'] ?? 'inside'),
            isset($data['index']) ? (int)$data['index'] : null,
            (array)($data['meta'] ?? [])
        );
    }

    public function targetType(): string { return $this->targetType; }
    public function path(): array { return $this->path; }
    public function position(): string { return $this->position; }
    public function index(): ?int { return $this->index; }

    public function toArray(): array
    {
        return [
            'targetType' => $this->targetType,
            'path' => $this->path,
            'position' => $this->position,
            'index' => $this->index,
            'meta' => $this->meta,
        ];
    }
}
