<?php

declare(strict_types=1);

namespace App\Framework\Database;

use App\Framework\Database\Middleware\MiddlewarePipeline;
use App\Framework\Database\Middleware\QueryContext;

final class MiddlewareConnection implements ConnectionInterface
{
    public function __construct(
        private ConnectionInterface $baseConnection,
        private MiddlewarePipeline $pipeline
    ) {}

    public function execute(string $sql, array $parameters = []): int
    {
        $context = new QueryContext('execute', $sql, $parameters, $this->baseConnection);

        return $this->pipeline->process($context, function(QueryContext $ctx): int {
            return $ctx->connection->execute($ctx->sql, $ctx->parameters);
        });
    }

    public function query(string $sql, array $parameters = []): ResultInterface
    {
        $context = new QueryContext('query', $sql, $parameters, $this->baseConnection);

        return $this->pipeline->process($context, function(QueryContext $ctx): ResultInterface {
            return $ctx->connection->query($ctx->sql, $ctx->parameters);
        });
    }

    public function queryOne(string $sql, array $parameters = []): ?array
    {
        $context = new QueryContext('queryOne', $sql, $parameters, $this->baseConnection);

        return $this->pipeline->process($context, function(QueryContext $ctx): ?array {
            return $ctx->connection->queryOne($ctx->sql, $ctx->parameters);
        });
    }

    public function queryColumn(string $sql, array $parameters = []): array
    {
        $context = new QueryContext('queryColumn', $sql, $parameters, $this->baseConnection);

        return $this->pipeline->process($context, function(QueryContext $ctx): array {
            return $ctx->connection->queryColumn($ctx->sql, $ctx->parameters);
        });
    }

    public function queryScalar(string $sql, array $parameters = []): mixed
    {
        $context = new QueryContext('queryScalar', $sql, $parameters, $this->baseConnection);

        return $this->pipeline->process($context, function(QueryContext $ctx): mixed {
            return $ctx->connection->queryScalar($ctx->sql, $ctx->parameters);
        });
    }

    public function beginTransaction(): void
    {
        $context = new QueryContext('beginTransaction', '', [], $this->baseConnection);

        $this->pipeline->process($context, function(QueryContext $ctx): void {
            $ctx->connection->beginTransaction();
        });
    }

    public function commit(): void
    {
        $context = new QueryContext('commit', '', [], $this->baseConnection);

        $this->pipeline->process($context, function(QueryContext $ctx): void {
            $ctx->connection->commit();
        });
    }

    public function rollback(): void
    {
        $context = new QueryContext('rollback', '', [], $this->baseConnection);

        $this->pipeline->process($context, function(QueryContext $ctx): void {
            $ctx->connection->rollback();
        });
    }

    public function inTransaction(): bool
    {
        return $this->baseConnection->inTransaction();
    }

    public function lastInsertId(): string
    {
        return $this->baseConnection->lastInsertId();
    }

    public function getPdo(): \PDO
    {
        return $this->baseConnection->getPdo();
    }

    public function getPipeline(): MiddlewarePipeline
    {
        return $this->pipeline;
    }

    public function getBaseConnection(): ConnectionInterface
    {
        return $this->baseConnection;
    }
}
