<?php

namespace App\Http\Controllers;

use Illuminate\Support\Str;
use Inertia\Inertia;

class FrontPagesController extends Controller
{
    public function privacy()
    {
        $markdownPath = resource_path('markdown/privacy.md');
        $markdownContent = file_get_contents($markdownPath);

        // Parse markdown to HTML using Laravel's built-in Str::markdown helper
        $htmlContent = Str::markdown($markdownContent);

        // Style the HTML with Tailwind classes
        $styledContent = $this->styleHtmlContent($htmlContent);

        return Inertia::render('FrontPages/Privacy', [
            'content' => $styledContent,
            'title' => 'Privacy Policy',
        ]);
    }

    public function terms()
    {
        $markdownPath = resource_path('markdown/terms.md');
        $markdownContent = file_get_contents($markdownPath);

        // Parse markdown to HTML using Laravel's built-in Str::markdown helper
        $htmlContent = Str::markdown($markdownContent);

        // Style the HTML with Tailwind classes
        $styledContent = $this->styleHtmlContent($htmlContent);

        return Inertia::render('FrontPages/Terms', [
            'content' => $styledContent,
            'title' => 'Terms & Conditions',
        ]);
    }

    private function styleHtmlContent($html)
    {
        // Add classes to various HTML elements using string replacement for Tailwind 4
        $html = preg_replace('/<h1([^>]*)>/', '<h1$1 class="text-4xl font-bold mb-6 text-gray-900 dark:text-white">', $html);
        $html = preg_replace('/<h2([^>]*)>/', '<h2$1 class="text-3xl font-semibold mb-4 mt-8 text-gray-800 dark:text-gray-100">', $html);
        $html = preg_replace('/<h3([^>]*)>/', '<h3$1 class="text-2xl font-semibold mb-3 mt-6 text-gray-700 dark:text-gray-200">', $html);
        $html = preg_replace('/<h4([^>]*)>/', '<h4$1 class="text-xl font-semibold mb-2 mt-4 text-gray-700 dark:text-gray-200">', $html);
        $html = preg_replace('/<p([^>]*)>/', '<p$1 class="mb-4 text-gray-600 dark:text-gray-300 leading-relaxed">', $html);
        $html = preg_replace('/<ul([^>]*)>/', '<ul$1 class="mb-4 space-y-2 list-disc pl-6">', $html);
        $html = preg_replace('/<ol([^>]*)>/', '<ol$1 class="mb-4 space-y-2 list-decimal pl-6">', $html);
        $html = preg_replace('/<li([^>]*)>/', '<li$1 class="text-gray-600 dark:text-gray-300 leading-relaxed">', $html);
        $html = preg_replace('/<a([^>]*)>/', '<a$1 class="text-blue-600 dark:text-blue-400 hover:text-blue-800 dark:hover:text-blue-300 underline">', $html);
        $html = preg_replace('/<strong([^>]*)>/', '<strong$1 class="font-semibold text-gray-800 dark:text-gray-200">', $html);
        $html = preg_replace('/<em([^>]*)>/', '<em$1 class="italic text-gray-700 dark:text-gray-300">', $html);
        $html = preg_replace('/<blockquote([^>]*)>/', '<blockquote$1 class="border-l-4 border-gray-300 dark:border-gray-600 pl-4 py-2 my-4 bg-gray-50 dark:bg-gray-800">', $html);
        $html = preg_replace('/<code([^>]*)>/', '<code$1 class="bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded text-sm font-mono text-gray-800 dark:text-gray-200">', $html);
        $html = preg_replace('/<pre([^>]*)>/', '<pre$1 class="bg-gray-100 dark:bg-gray-800 p-4 rounded-lg overflow-x-auto mb-4">', $html);

        return $html;
    }
}
