@inherits LayoutComponentBase
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment WebHostEnvironment
@using System.IO
@using System.Text.Json
@using Microsoft.FluentUI.AspNetCore.Components

<FluentStack Orientation="Orientation.Vertical" Class="h-100 w-100">
    <!-- Header -->
    <FluentHeader>
        <FluentStack Orientation="Orientation.Horizontal" VerticalAlignment="VerticalAlignment.Center"
                     Style="width: 100%; padding: 8px 16px; gap: 16px;">
            <FluentButton OnClick="@(() => navOpen = !navOpen)"
                         Title="Toggle Navigation"
                         Style="background: transparent; border: none; cursor: pointer;">
                ☰
            </FluentButton>
            <h1 style="margin: 0; font-size: 20px; font-weight: 600;">QuantEngine v@appVersion</h1>
        </FluentStack>
    </FluentHeader>

    <!-- Main Content Area -->
    <FluentStack Orientation="Orientation.Horizontal" Class="flex-1" Style="overflow: hidden;">
        <!-- Navigation Sidebar -->
        @if (navOpen)
        {
            <nav style="width: 240px; background: var(--neutral-layer-1); border-right: 1px solid var(--neutral-stroke-1); padding: 12px; overflow-y: auto;">
                <NavMenu />
                <div style="margin-top: auto; padding-top: 12px; border-top: 1px solid var(--neutral-stroke-1); margin-top: 12px; font-size: 11px; color: var(--neutral-foreground-3); line-height: 1.5;">
                    <div style="font-weight: 500; margin-bottom: 2px;">QuantEngine v@appVersion</div>
                    <div style="font-size: 10px; opacity: 0.85;">배포: @buildTime</div>
                </div>
            </nav>
        }

        <!-- Page Content -->
        <FluentStack Orientation="Orientation.Vertical" Class="flex-1" Style="overflow-y: auto; padding: 24px;">
            @Body
        </FluentStack>
    </FluentStack>
</FluentStack>

<div id="blazor-error-ui" data-nosnippet>
    <div class="alert alert-danger" role="alert">
        <p>An unhandled error has occurred.</p>
        <a href="." class="btn btn-primary">Reload</a>
    </div>
</div>

<style>
    .h-100 {
        height: 100%;
    }
    .w-100 {
        width: 100%;
    }
    .flex-1 {
        flex: 1;
        display: flex;
    }
</style>

@code {
    private bool navOpen = true;
    private string appVersion = "Local Debug";
    private string buildTime = "N/A";

    protected override void OnInitialized()
    {
        try
        {
            var versionFilePath = Path.Combine(WebHostEnvironment.WebRootPath, "version.json");
            if (File.Exists(versionFilePath))
            {
                var jsonContent = File.ReadAllText(versionFilePath);
                using var doc = System.Text.Json.JsonDocument.Parse(jsonContent);
                var root = doc.RootElement;

                if (root.TryGetProperty("version", out var versionProp))
                {
                    appVersion = versionProp.GetString() ?? "Local Debug";
                }
                if (root.TryGetProperty("built", out var builtProp))
                {
                    buildTime = builtProp.GetString() ?? "N/A";
                }
            }
        }
        catch
        {
            // Fail-safe default fallback values
        }
    }
}

