<?php

declare(strict_types=1);

namespace App\Framework\Database\Schema;

use App\Framework\Database\ValueObjects\ColumnName;
use App\Framework\Database\ValueObjects\IndexName;

final class IndexDefinition
{
    public readonly ?IndexName $name;

    /** @var array<ColumnName> */
    public readonly array $columns;

    public readonly IndexType $type;

    public ?string $using = null;

    /**
     * @param string|IndexName|null $name
     * @param array<string|ColumnName> $columns
     */
    public function __construct(string|IndexName|null $name, array $columns, IndexType $type)
    {
        $this->name = $name instanceof IndexName
            ? $name
            : ($name !== null ? IndexName::fromString($name) : null);

        $this->columns = array_map(
            fn($col) => $col instanceof ColumnName ? $col : ColumnName::fromString($col),
            $columns
        );

        $this->type = $type;
    }

    public function using(string $method): self
    {
        $this->using = strtoupper($method);

        return $this;
    }
}
