<?php

declare(strict_types=1);

namespace App\Framework\Database\Middleware;

use App\Framework\Database\ConnectionInterface;
use App\Framework\Database\ValueObjects\SqlQuery;

final class QueryContext
{
    public function __construct(
        public readonly string $operation,
        public readonly SqlQuery $query,
        public readonly ConnectionInterface $connection,
        public array $metadata = []
    ) {
    }

    public function withMetadata(string $key, mixed $value): self
    {
        $new = clone $this;
        $new->metadata[$key] = $value;

        return $new;
    }

    public function getMetadata(string $key, mixed $default = null): mixed
    {
        return $this->metadata[$key] ?? $default;
    }

    public function hasMetadata(string $key): bool
    {
        return array_key_exists($key, $this->metadata);
    }
}
