<?php

use Illuminate\Support\Facades\Route;
use Inertia\Inertia;






use Stancl\Tenancy\Middleware\InitializeTenancyByDomainOrSubdomain;

// 登入/登出路由


Route::get('/api/docs', function () {
    $path = resource_path('markdown/manual/api-integration.md');
    
    if (!file_exists($path)) {
        abort(404);
    }
    
    $markdown = file_get_contents($path);
    
    // 動態替換 API Base URL 的租戶網域
    $currentHost = request()->getHost();
    $markdown = str_replace('[租戶網域]', $currentHost, $markdown);
    
    // 解析 Markdown 內容
    $content = Illuminate\Support\Str::markdown($markdown, [
        'html_input' => 'strip',
        'allow_unsafe_links' => false,
    ]);
    
    // 萃取 TOC (簡單的正則表達式抓取 ## 標題)
    preg_match_all('/^##\s+(.+)$/m', $markdown, $matches);
    $toc = collect($matches[1])->map(function ($text) {
        return [
            'id' => Illuminate\Support\Str::slug($text),
            'text' => $text,
        ];
    });

    // 替換內容中的 ## 標題以加入 ID 錨點 (讓左側導覽能跳轉)
    foreach ($toc as $item) {
        $content = str_replace("<h2>{$item['text']}</h2>", "<h2 id=\"{$item['id']}\">{$item['text']}</h2>", $content);
    }

    return view('docs.api', [
        'content' => $content,
        'toc' => $toc,
        'title' => '外部系統 API 對接手冊'
    ]);
});


Route::middleware('auth')->group(function () {
    // 儀表板 - 所有登入使用者皆可存取




















}); // End of auth middleware group
