<?php

declare(strict_types=1);

namespace App\Framework\View\Loading\Resolvers;

use App\Framework\Core\PathProvider;

final readonly class ComponentResolver implements TemplateResolverStrategy
{
    public function __construct(
        private PathProvider $pathProvider,
        private string $templatePath = '/src/Framework/View/templates'
    ) {
    }

    public function resolve(string $template, ?string $controllerClass = null): ?string
    {
        // Versuche .html Extension für Components
        $htmlPath = $this->pathProvider->resolvePath(
            $this->templatePath . '/components/' . $template . '.html'
        );

        if (file_exists($htmlPath)) {
            return $htmlPath;
        }

        // Fallback: .view.php Extension
        $phpPath = $this->pathProvider->resolvePath(
            $this->templatePath . '/components/' . $template . '.view.php'
        );

        return file_exists($phpPath) ? $phpPath : null;
    }
}
