<?php

declare(strict_types=1);

namespace App\Application\Api;

use App\Framework\Attributes\Route;
use App\Framework\Http\HttpResponse;
use App\Framework\Http\Method;
use App\Framework\Meta\StaticPageMetaResolver;
use App\Framework\Router\ApiRoutes;
use App\Framework\Router\Result\ViewResult;

/**
 * Simple controller to test Markdown rendering without complex dependencies
 */
final readonly class SimpleMarkdownController
{
    #[Route(path: '/api/docs/simple', method: Method::GET, name: ApiRoutes::DOCS_SIMPLE)]
    public function simpleTest(): ViewResult
    {
        return new ViewResult(
            'simple-test',
            new StaticPageMetaResolver(
                'Simple Test',
                'Testing the template system'
            )(),
            [
                'title' => 'Simple Test',
                'heading' => 'Template System Test',
                'message' => 'This tests if the template system works correctly with variables.',
                'currentTime' => date('Y-m-d H:i:s'),
            ]
        );
    }

    #[Route(path: '/api/docs/converter-test', method: Method::GET, name: ApiRoutes::DOCS_CONVERTER_TEST)]
    public function converterTest(): ViewResult
    {
        try {
            $converter = new \App\Framework\Markdown\MarkdownConverter();
            $markdown = "# Test\n\nThis is **bold** and *italic*.\n\n- Item 1\n- Item 2";
            $html = $converter->toHtml($markdown);

            return new ViewResult(
                'simple-test',
                new StaticPageMetaResolver(
                    'Converter Test',
                    'Test the markdown converter'
                )(),
                [
                    'title' => 'Converter Test',
                    'heading' => 'Markdown Converter Test',
                    'message' => 'Original: ' . $markdown . ' | Converted: ' . $html,
                    'currentTime' => date('Y-m-d H:i:s'),
                ]
            );
        } catch (\Exception $e) {
            return new ViewResult(
                'simple-test',
                new StaticPageMetaResolver(
                    'Error',
                    'Error testing converter'
                )(),
                [
                    'title' => 'Error',
                    'heading' => 'Error',
                    'message' => $e->getMessage(),
                    'currentTime' => date('Y-m-d H:i:s'),
                ]
            );
        }
    }

    #[Route(path: '/api/docs/markdown-simple', method: Method::GET, name: ApiRoutes::DOCS_MARKDOWN_SIMPLE)]
    public function markdownSimple(): ViewResult
    {
        try {
            // Teste nur den Converter erst
            $converter = new \App\Framework\Markdown\MarkdownConverter();
            $testMarkdown = "# API Documentation\n\nThis is a **test**.";
            $html = $converter->toHtml($testMarkdown);

            return new ViewResult(
                'simple-test',
                new StaticPageMetaResolver(
                    'Markdown Debug',
                    'Debug markdown step by step'
                )(),
                [
                    'title' => 'Markdown Debug',
                    'heading' => 'Step 1: Converter only',
                    'message' => 'Markdown: ' . $testMarkdown . ' | HTML: ' . $html,
                    'currentTime' => date('Y-m-d H:i:s'),
                ]
            );
        } catch (\Exception $e) {
            return new ViewResult(
                'simple-test',
                new StaticPageMetaResolver(
                    'Error',
                    'Error in markdown rendering'
                )(),
                [
                    'title' => 'Error',
                    'heading' => 'Markdown Rendering Error',
                    'message' => $e->getMessage() . ' | Trace: ' . $e->getTraceAsString(),
                    'currentTime' => date('Y-m-d H:i:s'),
                ]
            );
        }
    }

    #[Route(path: '/api/docs/markdown-renderer', method: Method::GET, name: ApiRoutes::DOCS_MARKDOWN_RENDERER)]
    public function markdownRenderer(): ViewResult
    {
        try {
            // Teste den Renderer
            $converter = new \App\Framework\Markdown\MarkdownConverter();
            $renderer = new \App\Framework\Markdown\MarkdownRenderer($converter);

            $testMarkdown = "# API Documentation\n\nThis is a **test**.";
            $html = $renderer->render($testMarkdown, 'api', [
                'title' => 'Test',
                'syntaxHighlighting' => false,
            ]);

            return new ViewResult(
                'simple-test',
                new StaticPageMetaResolver(
                    'Renderer Debug',
                    'Debug renderer step by step'
                )(),
                [
                    'title' => 'Renderer Debug',
                    'heading' => 'Step 2: Renderer test',
                    'message' => 'HTML length: ' . strlen($html) . ' | First 200 chars: ' . substr($html, 0, 200),
                    'currentTime' => date('Y-m-d H:i:s'),
                ]
            );
        } catch (\Exception $e) {
            return new ViewResult(
                'simple-test',
                new StaticPageMetaResolver(
                    'Error',
                    'Error in renderer'
                )(),
                [
                    'title' => 'Error',
                    'heading' => 'Renderer Error',
                    'message' => $e->getMessage() . ' | File: ' . $e->getFile() . ' | Line: ' . $e->getLine(),
                    'currentTime' => date('Y-m-d H:i:s'),
                ]
            );
        }
    }

    #[Route(path: '/api/docs/markdown-full', method: Method::GET, name: ApiRoutes::DOCS_MARKDOWN_FULL)]
    public function markdownFull(): ViewResult
    {
        try {
            // Teste mit HttpResponse statt ViewResult
            $converter = new \App\Framework\Markdown\MarkdownConverter();
            $renderer = new \App\Framework\Markdown\MarkdownRenderer($converter);

            $testMarkdown = "# API Documentation\n\n## Test Endpoint\n\n**GET** `/api/test`\n\nThis is a test endpoint.\n\n### Parameters\n\n- `id` (required) - The item ID\n- `format` (optional) - Response format";

            $html = $renderer->render($testMarkdown, 'api', [
                'title' => 'Full API Documentation',
                'syntaxHighlighting' => false,
            ]);

            return new ViewResult(
                'markdown-test',
                new StaticPageMetaResolver(
                    'Full API Documentation',
                    'Test complete markdown rendering'
                )(),
                [
                    'content' => $html,
                ]
            );
        } catch (\Exception $e) {
            return new ViewResult(
                'simple-test',
                new StaticPageMetaResolver(
                    'Error',
                    'Error in full markdown'
                )(),
                [
                    'title' => 'Error',
                    'heading' => 'Full Markdown Error',
                    'message' => $e->getMessage() . ' | File: ' . $e->getFile() . ' | Line: ' . $e->getLine(),
                    'currentTime' => date('Y-m-d H:i:s'),
                ]
            );
        }
    }

    #[Route(path: '/api/docs/viewresult-debug', method: Method::GET, name: ApiRoutes::DOCS_VIEWRESULT_DEBUG)]
    public function viewResultDebug(): ViewResult
    {
        // Teste das ViewResult ohne Markdown
        return new ViewResult(
            'simple-test',
            new StaticPageMetaResolver(
                'ViewResult Debug',
                'Test ViewResult without markdown'
            )(),
            [
                'title' => 'ViewResult Debug',
                'heading' => 'Testing ViewResult',
                'message' => 'This should work if ViewResult is OK',
                'currentTime' => date('Y-m-d H:i:s'),
            ]
        );
    }
}
