<?php

declare(strict_types=1);

namespace App\Application\Http\Controllers;

use App\Application\Service\QrCodeService;
use App\Domain\QrCode\ValueObject\ErrorCorrectionLevel;
use App\Framework\Attributes\Route;
use App\Framework\Auth\Auth;
use App\Framework\Http\Method;
use App\Framework\Http\Status;
use App\Framework\Http\Response;

final class QrCodeController
{
    public function __construct(
        private readonly QrCodeService $qrCodeService
    ) {
    }

    /**
     * Generiert einen QR-Code als SVG
     */
    #[Auth]
    #[Route(path: '/api/qrcode/svg', method: Method::GET)]
    public function generateSvg(): Response
    {
        $data = $_GET['data'] ?? 'https://example.com';
        $errorLevel = $this->getErrorLevel($_GET['error_level'] ?? 'M');
        $moduleSize = (int) ($_GET['module_size'] ?? 4);
        $margin = (int) ($_GET['margin'] ?? 4);
        $foreground = $_GET['foreground'] ?? '#000000';
        $background = $_GET['background'] ?? '#FFFFFF';

        $config = new QrCodeConfig($moduleSize, $margin, $foreground, $background);

        $svg = $this->qrCodeService->generateSvg($data, $errorLevel, $config);

        return new Response(
            body: $svg,
            status: Status::OK,
            headers: ['Content-Type' => 'image/svg+xml']
        );
    }

    /**
     * Generiert einen QR-Code als PNG
     */
    #[Auth]
    #[Route(path: '/api/qrcode/png', method: Method::GET)]
    public function generatePng(): Response
    {
        $data = $_GET['data'] ?? 'https://example.com';
        $errorLevel = $this->getErrorLevel($_GET['error_level'] ?? 'M');
        $moduleSize = (int) ($_GET['module_size'] ?? 4);
        $margin = (int) ($_GET['margin'] ?? 4);

        $config = new QrCodeConfig($moduleSize, $margin);

        $png = $this->qrCodeService->generatePng($data, $errorLevel, $config);

        return new Response(
            body: $png,
            status: Status::OK,
            headers: ['Content-Type' => 'image/png']
        );
    }

    /**
     * Generiert einen QR-Code als ASCII-Art
     */
    #[Auth]
    #[Route(path: '/api/qrcode/ascii', method: Method::GET)]
    public function generateAscii(): Response
    {
        $data = $_GET['data'] ?? 'https://example.com';
        $errorLevel = $this->getErrorLevel($_GET['error_level'] ?? 'M');

        $ascii = $this->qrCodeService->generateAscii($data, $errorLevel);

        return new Response(
            body: "<pre>$ascii</pre>",
            status: Status::OK,
            headers: ['Content-Type' => 'text/html; charset=utf-8']
        );
    }

    /**
     * Konvertiert einen String in ein ErrorCorrectionLevel-Enum
     */
    private function getErrorLevel(string $level): ErrorCorrectionLevel
    {
        return match (strtoupper($level)) {
            'L' => ErrorCorrectionLevel::L,
            'Q' => ErrorCorrectionLevel::Q,
            'H' => ErrorCorrectionLevel::H,
            default => ErrorCorrectionLevel::M,
        };
    }
}
