<?php

declare(strict_types=1);

namespace App\Framework\Mcp\Core\Services;

use App\Framework\Cache\Cache;
use App\Framework\Logging\Logger;
use App\Framework\Mcp\Core\Interfaces\CacheProvider;
use App\Framework\Mcp\Core\Interfaces\ErrorHandler;
use App\Framework\Mcp\Core\Interfaces\ResultFormatter;
use App\Framework\Mcp\Core\Interfaces\SchemaValidator;

/**
 * MCP Service Factory
 *
 * Zentrale Factory für alle MCP Services mit Dependency Injection
 * Stellt alle benötigten Services für MCP Tools bereit
 */
final readonly class McpServiceFactory
{
    private SchemaValidator $schemaValidator;

    private ResultFormatter $resultFormatter;

    private ErrorHandler $errorHandler;

    private CacheProvider $cacheProvider;

    public function __construct(
        Cache $cache,
        Logger $logger
    ) {
        // Initialize all MCP services
        $this->schemaValidator = new McpSchemaValidator();
        $this->resultFormatter = new McpResultFormatter();
        $this->errorHandler = new McpErrorHandler($logger);
        $this->cacheProvider = new McpCacheProvider($cache);
    }

    /**
     * Erstellt einen vollständig konfigurierten MCP Tool Context
     */
    public function createToolContext(): McpToolContext
    {
        return new McpToolContext(
            schemaValidator: $this->schemaValidator,
            resultFormatter: $this->resultFormatter,
            errorHandler: $this->errorHandler,
            cacheProvider: $this->cacheProvider
        );
    }

    /**
     * Gibt den Schema Validator zurück
     */
    public function getSchemaValidator(): SchemaValidator
    {
        return $this->schemaValidator;
    }

    /**
     * Gibt den Result Formatter zurück
     */
    public function getResultFormatter(): ResultFormatter
    {
        return $this->resultFormatter;
    }

    /**
     * Gibt den Error Handler zurück
     */
    public function getErrorHandler(): ErrorHandler
    {
        return $this->errorHandler;
    }

    /**
     * Gibt den Cache Provider zurück
     */
    public function getCacheProvider(): CacheProvider
    {
        return $this->cacheProvider;
    }

    /**
     * Erstellt Service Factory mit Custom Services (für Testing oder spezielle Konfigurationen)
     */
    public static function withCustomServices(
        SchemaValidator $schemaValidator,
        ResultFormatter $resultFormatter,
        ErrorHandler $errorHandler,
        CacheProvider $cacheProvider
    ): self {
        $factory = new class ($schemaValidator, $resultFormatter, $errorHandler, $cacheProvider) {
            public function __construct(
                private readonly SchemaValidator $schemaValidator,
                private readonly ResultFormatter $resultFormatter,
                private readonly ErrorHandler $errorHandler,
                private readonly CacheProvider $cacheProvider
            ) {
            }

            public function createToolContext(): McpToolContext
            {
                return new McpToolContext(
                    schemaValidator: $this->schemaValidator,
                    resultFormatter: $this->resultFormatter,
                    errorHandler: $this->errorHandler,
                    cacheProvider: $this->cacheProvider
                );
            }

            public function getSchemaValidator(): SchemaValidator
            {
                return $this->schemaValidator;
            }

            public function getResultFormatter(): ResultFormatter
            {
                return $this->resultFormatter;
            }

            public function getErrorHandler(): ErrorHandler
            {
                return $this->errorHandler;
            }

            public function getCacheProvider(): CacheProvider
            {
                return $this->cacheProvider;
            }
        };

        return $factory;
    }
}
