@inherits LayoutComponentBase
@inject HttpClient Http
@inject AuthenticationStateProvider AuthStateProvider
@inject NavigationManager NavigationManager

<MudLayout>
    <!-- Top Navigation Bar -->
    <MudAppBar Elevation="1" Dense="false" Color="Color.Surface" Class="mud-appbar-dense">
        <MudHidden Breakpoint="Breakpoint.SmAndUp" Invert="true">
            <MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@(() => navOpen = !navOpen)" />
        </MudHidden>

        <MudText Typo="Typo.h6" Class="ml-2">
            <MudIcon Icon="@Icons.Material.Filled.Dashboard" Class="me-2" />
            QuantEngine
        </MudText>

        <MudSpacer />

        <!-- User Menu -->
        <AuthorizeView>
            <Authorized>
                <MudMenu AnchorOrigin="Origin.BottomRight" TransformOrigin="Origin.TopRight" Class="ml-2">
                    <ActivatorContent>
                        <MudAvatar Color="Color.Primary" Image="@GetUserInitials()" Class="cursor-pointer">
                            @GetFirstLetter(context.User.Identity?.Name)
                        </MudAvatar>
                    </ActivatorContent>
                    <ChildContent>
                        <MudMenuItem>
                            <MudText Typo="Typo.body2">
                                <strong>@context.User.Identity?.Name</strong>
                            </MudText>
                        </MudMenuItem>
                        <MudDivider />
                        <MudMenuItem href="/profile">
                            <MudIcon Icon="@Icons.Material.Filled.Person" Class="mr-2" Size="Size.Small" />
                            프로필
                        </MudMenuItem>
                        <MudMenuItem href="/settings">
                            <MudIcon Icon="@Icons.Material.Filled.Settings" Class="mr-2" Size="Size.Small" />
                            설정
                        </MudMenuItem>
                        <MudDivider />
                        <MudMenuItem OnClick="HandleLogoutAsync">
                            <MudIcon Icon="@Icons.Material.Filled.Logout" Class="mr-2" Size="Size.Small" Color="Color.Error" />
                            <MudText Color="Color.Error">로그아웃</MudText>
                        </MudMenuItem>
                    </ChildContent>
                </MudMenu>
            </Authorized>
        </AuthorizeView>
    </MudAppBar>

    <!-- Sidebar Navigation -->
    <MudDrawer Open="@navOpen" Variant="DrawerVariant.Responsive" Elevation="1" FixedOpen="@fixedOpen">
        <MudDrawerHeader Class="d-flex align-center justify-space-between">
            <MudText Typo="Typo.h6" Class="px-2">메뉴</MudText>
            <MudHidden Breakpoint="Breakpoint.Md" Invert="true">
                <MudIconButton Icon="@Icons.Material.Filled.ChevronLeft"
                              OnClick="ToggleDrawer"
                              Class="mx-1" />
            </MudHidden>
        </MudDrawerHeader>

        <MudNavMenu>
            <NavMenu />
        </MudNavMenu>

        <!-- Drawer Footer -->
        <div class="mud-drawer-footer">
            <MudDivider />
            <div style="padding: 16px;">
                <MudText Typo="Typo.caption">
                    <strong>QuantEngine</strong>
                </MudText>
                <MudText Typo="Typo.caption">
                    v@appVersion
                </MudText>
                <MudText Typo="Typo.caption" Class="mt-2">
                    배포: @buildTime
                </MudText>
            </div>
        </div>
    </MudDrawer>

    <!-- Main Content Area -->
    <MudMainContent Class="mud-main-content-enhanced">
        <MudContainer MaxWidth="MaxWidth.False" Class="pa-6">
            @Body
        </MudContainer>
    </MudMainContent>
</MudLayout>

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

    protected override async Task OnInitializedAsync()
    {
        try
        {
            var versionInfo = await Http.GetFromJsonAsync<VersionInfo>("version.json");
            if (versionInfo != null)
            {
                appVersion = versionInfo.Version ?? "Local Debug";
                buildTime = versionInfo.Built ?? "N/A";
            }
        }
        catch
        {
        }

        await base.OnInitializedAsync();
    }

    private void ToggleDrawer()
    {
        navOpen = !navOpen;
    }

    private async Task HandleLogoutAsync()
    {
        var customProvider = (CustomAuthenticationStateProvider)AuthStateProvider;
        await customProvider.LogoutFromServerAsync();
        NavigationManager.NavigateTo("/login");
    }

    private string GetFirstLetter(string? name)
    {
        return string.IsNullOrEmpty(name) ? "?" : name[0].ToString().ToUpper();
    }

    private string GetUserInitials()
    {
        return string.Empty;
    }

    private class VersionInfo
    {
        public string? Version { get; set; }
        public string? Built { get; set; }
    }
}
