<?php
declare(strict_types=1);

namespace App\Framework\StaticSite;

use App\Framework\Core\Application;
use App\Framework\Http\Method;
use App\Framework\Http\HttpRequest;
use App\Framework\Http\Request;
use App\Framework\Http\Response;

class StaticSiteGenerator
{
    private Application $app;
    private string $outputDir;
    private array $routesToGenerate;

    public function __construct(Application $app, array $routesToGenerate = [], ?string $outputDir = null)
    {
        $this->app = $app;
        $this->routesToGenerate = $routesToGenerate;
        $this->outputDir = $outputDir ?? dirname(__DIR__, 2) . '/public/static';
    }

    public function generate(): void
    {
        $this->prepareOutputDirectory();

        foreach ($this->routesToGenerate as $route) {
            $this->generateRoute($route);
        }
    }

    private function prepareOutputDirectory(): void
    {
        if (!file_exists($this->outputDir)) {
            mkdir($this->outputDir, 0755, true);
        }
    }

    private function generateRoute(string $route): void
    {
        // Erstelle eine simulierte Anfrage
        $request = new HttpRequest(Method::GET, $route, [], [], []);

        try {
            // Führe die Anfrage aus und erhalte die Antwort
            $response = $this->app->handleRequest($request);

            // Generiere den Pfad für die Datei
            $filePath = $this->getFilePathForRoute($route);

            // Stelle sicher, dass das Verzeichnis existiert
            $directory = dirname($filePath);
            if (!file_exists($directory)) {
                mkdir($directory, 0755, true);
            }

            // Schreibe den Inhalt in die Datei
            file_put_contents($filePath, $response->getBody());

            echo "Statische Seite generiert: {$route} -> {$filePath}\n";
        } catch (\Exception $e) {
            echo "Fehler beim Generieren von {$route}: {$e->getMessage()}\n";
        }
    }

    private function getFilePathForRoute(string $route): string
    {
        // Bereinige Route
        $route = trim($route, '/');

        // Bestimme den Dateinamen
        if (empty($route)) {
            $filePath = $this->outputDir . '/index.html';
        } else {
            // Für /about, /contact, etc.
            if (strpos($route, '.') === false) {
                // Keine Dateiendung vorhanden, erstelle Verzeichnis mit index.html
                $filePath = $this->outputDir . '/' . $route . '/index.html';
            } else {
                // Dateiendung vorhanden (z.B. .json, .xml)
                $filePath = $this->outputDir . '/' . $route;
            }
        }

        return $filePath;
    }

    public function setRoutesToGenerate(array $routes): self
    {
        $this->routesToGenerate = $routes;
        return $this;
    }

    public function addRoute(string $route): self
    {
        $this->routesToGenerate[] = $route;
        return $this;
    }

    public function setOutputDirectory(string $directory): self
    {
        $this->outputDir = $directory;
        return $this;
    }
}
