@inherits LayoutComponentBase

<PageTitle>کارا بازار سلامت</PageTitle>

<MudRTLProvider RightToLeft="true">
    <MudThemeProvider @ref="_mudThemeProvider" Theme="CustomMudTheme.CustomMudBlazorTheme" @bind-IsDarkMode="_isDark"/>
    <MudPopoverProvider/>
    <MudDialogProvider/>
    <MudSnackbarProvider/>
    <MudLayout>
        <!-- AppBar -->
        <MudAppBar Elevation="1" Color="Color.Surface" Dense="true" Class="py-2" id="top">
            <MudContainer MaxWidth="MaxWidth.Large" Class="d-flex align-center justify-space-between w-100">

                <MudHidden Breakpoint="Breakpoint.MdAndUp">
                    <MudIconButton Class="d-inline"
                                   Icon="@Icons.Material.Filled.Menu"
                                   OnClick="@ToggleDrawer"/>
                </MudHidden>

                <div class="d-flex align-center gap-2">
                    <MudLink Href="@(_isAuthenticated ? RouteConstants.Profile.Index : RouteConstants.Main.MainPage)" Underline="Underline.None">
                        <MudStack Row="true" Spacing="3" AlignItems="AlignItems.Center">
                            <MudHidden Breakpoint="Breakpoint.MdAndUp" Invert="true">
                                <MudImage ObjectFit="ObjectFit.Cover"
                                          ObjectPosition="ObjectPosition.Center"
                                          Width="32"
                                          Height="32"
                                          Class="rounded-circle"
                                          Src="favicon.png"/>
                            </MudHidden>
                            <MudText Typo="Typo.h6">کارا بازار سلامت</MudText>
                        </MudStack>
                    </MudLink>

                    @* ── Store context badge (desktop) ── *@
                    @if (IsInRegularStore)
                    {
                        <MudChip T="string" Color="Color.Success" Variant="Variant.Filled" Size="Size.Small"
                                 Class="d-none d-md-flex" Icon="@Icons.Material.Filled.Storefront">فروشگاه</MudChip>
                    }
                    else if (IsInDiscountStore)
                    {
                        <MudChip T="string" Color="Color.Error" Variant="Variant.Filled" Size="Size.Small"
                                 Class="d-none d-md-flex" Icon="@Icons.Material.Filled.Loyalty">فروشگاه اعتباری</MudChip>
                    }
                </div>

                <div class="d-none d-md-flex align-center gap-10">
                    @if (IsInRegularStore && _isAuthenticated)
                    {
                        @* ── Regular store nav ── *@
                        <MudLink Href="@(RouteConstants.Store.Products)" Typo="Typo.subtitle1" Class="mud-link">محصولات</MudLink>
                        <MudLink Href="@(RouteConstants.Store.Cart)" Typo="Typo.subtitle1" Class="mud-link">سبد خرید</MudLink>
                        <MudLink Href="@(RouteConstants.Store.Orders)" Typo="Typo.subtitle1" Class="mud-link">سفارشات من</MudLink>
                    }
                    else if (IsInDiscountStore && _isAuthenticated)
                    {
                        @* ── Discount store nav ── *@
                        <MudLink Href="@(RouteConstants.DiscountStore.Products)" Typo="Typo.subtitle1" Class="mud-link">محصولات اعتباری</MudLink>
                        <MudLink Href="@(RouteConstants.DiscountStore.Cart)" Typo="Typo.subtitle1" Class="mud-link">سبد خرید</MudLink>
                        <MudLink Href="@(RouteConstants.DiscountStore.Orders)" Typo="Typo.subtitle1" Class="mud-link">سفارشات من</MudLink>
                    }
                    else
                    {
                        @* ── Default global nav (not inside any store) ── *@
                        @if (_isAuthenticated)
                        {
                            <MudLink Href="@(RouteConstants.Gateway.StoreChooser)" Typo="Typo.subtitle1" Class="mud-link">فروشگاه‌ها</MudLink>
                        }
                    }
                    <MudLink Href="@(RouteConstants.FAQ.Index)" Typo="Typo.subtitle1" Class="mud-link">سوالات متداول</MudLink>
                    <MudLink Href="@(RouteConstants.Blog.Index)" Typo="Typo.subtitle1" Class="mud-link">بلاگ</MudLink>
                    <MudLink Href="@(RouteConstants.Contact.Index)" Typo="Typo.subtitle1" Class="mud-link">ارتباط با ما</MudLink>
                    <MudLink Href="@(RouteConstants.About.Index)" Typo="Typo.subtitle1" Class="mud-link">درباره ما</MudLink>
                    <MudLink Href="@(RouteConstants.Licenses.Index)" Typo="Typo.subtitle1" Class="mud-link">مجوزها</MudLink>
                </div>

                <div class="d-flex align-center gap-2">
                    @if (_isAuthenticated)
                    {
                        @* ── Cart icon: only visible when inside a store ── *@
                        @if (IsInRegularStore)
                        {
                            <MudIconButton Icon="@Icons.Material.Filled.ShoppingCart"
                                           Color="@(_cartCount > 0 ? Color.Success : Color.Inherit)"
                                           Href="@(RouteConstants.Store.Cart)" />
                        }
                        else if (IsInDiscountStore)
                        {
                            <MudIconButton Icon="@Icons.Material.Filled.ShoppingCart"
                                           Color="@(_discountCartCount > 0 ? Color.Error : Color.Inherit)"
                                           Href="@(RouteConstants.DiscountStore.Cart)" />
                        }

                        @* دکمه بروزرسانی *@
                        @if (_hasUpdate)
                        {
                            <MudBadge Dot="true" Color="Color.Error" Overlap="true" Bordered="true">
                                <MudIconButton Icon="@Icons.Material.Filled.SystemUpdateAlt" 
                                               Color="Color.Warning" 
                                               OnClick="OpenUpdateDialog"
                                               Title="بروزرسانی موجود است"/>
                            </MudBadge>
                        }
                        <MudMenu Icon="@Icons.Material.Filled.Person" Color="Color.Inherit" Size="Size.Medium">
                            <MudMenuItem OnClick="NavigateToProfile" Disabled="@(!_isCompleteRegister)" Icon="@Icons.Material.Outlined.Dashboard">
                                داشبورد
                            </MudMenuItem>
                            <MudMenuItem OnClick="NavigateToHub" Disabled="@(!_isCompleteRegister)" Icon="@Icons.Material.Outlined.Person">
                                پروفایل
                            </MudMenuItem>
                            <MudDivider/>
                            @if (_hasUpdate)
                            {
                                <MudMenuItem OnClick="OpenUpdateDialog" IconColor="Color.Warning" Icon="@Icons.Material.Filled.SystemUpdateAlt">
                                    <MudText Color="Color.Warning">بروزرسانی (@_newVersion)</MudText>
                                </MudMenuItem>
                                <MudDivider/>
                            }
                            <MudMenuItem OnClick="ToggleTheme" Icon="@(_isDark ? Icons.Material.Filled.LightMode : Icons.Material.Filled.DarkMode)">
                                @(_isDark ? "حالت روشن" : "حالت تاریک")
                            </MudMenuItem>
                            <MudDivider/>
                            <MudMenuItem OnClick="Logout">خروج از حساب</MudMenuItem>
                        </MudMenu>
                    }
                    else
                    {
                        <MudIconButton Icon="@Icons.Material.Filled.Login" Color="Color.Inherit"
                                       OnClick="OpenAuthDialog"/>
                        <MudIconButton OnClick="@ToggleTheme"
                                       Icon="@(_isDark ? Icons.Material.Filled.LightMode : Icons.Material.Filled.DarkMode)"
                                       Title="@(_isDark ? "حالت روشن" : "حالت تاریک")"/>
                    }
                </div>
            </MudContainer>
        </MudAppBar>

        <!-- Mobile Drawer -->
        <MudDrawer @bind-Open="_drawerOpen" Anchor="Anchor.Right" Elevation="1" Variant="DrawerVariant.Temporary"
                   Class="pa-4">
            <MudStack Spacing="2">
                @if (_isAuthenticated)
                {
                    @if (IsInRegularStore)
                    {
                        <MudText Typo="Typo.overline" Class="mud-text-secondary">فروشگاه</MudText>
                        <MudLink Href="@(RouteConstants.Store.Products)" Typo="Typo.subtitle1"
                                 OnClick="() => _drawerOpen=false">محصولات</MudLink>
                        <MudLink Href="@(RouteConstants.Store.Cart)" Typo="Typo.subtitle1"
                                 OnClick="() => _drawerOpen=false">سبد خرید</MudLink>
                        <MudLink Href="@(RouteConstants.Store.Orders)" Typo="Typo.subtitle1"
                                 OnClick="() => _drawerOpen=false">سفارشات من</MudLink>
                        <MudDivider Class="my-1" />
                        <MudLink Href="@(RouteConstants.Gateway.StoreChooser)" Typo="Typo.subtitle1"
                                 OnClick="() => _drawerOpen=false" Class="mud-text-secondary">
                            ← بازگشت به فروشگاه‌ها
                        </MudLink>
                    }
                    else if (IsInDiscountStore)
                    {
                        <MudText Typo="Typo.overline" Style="color:var(--mud-palette-error);">فروشگاه اعتباری</MudText>
                        <MudLink Href="@(RouteConstants.DiscountStore.Products)" Typo="Typo.subtitle1"
                                 OnClick="() => _drawerOpen=false">محصولات اعتباری</MudLink>
                        <MudLink Href="@(RouteConstants.DiscountStore.Cart)" Typo="Typo.subtitle1"
                                 OnClick="() => _drawerOpen=false">سبد خرید</MudLink>
                        <MudLink Href="@(RouteConstants.DiscountStore.Orders)" Typo="Typo.subtitle1"
                                 OnClick="() => _drawerOpen=false">سفارشات من</MudLink>
                        <MudDivider Class="my-1" />
                        <MudLink Href="@(RouteConstants.Gateway.StoreChooser)" Typo="Typo.subtitle1"
                                 OnClick="() => _drawerOpen=false" Class="mud-text-secondary">
                            ← بازگشت به فروشگاه‌ها
                        </MudLink>
                    }
                    else
                    {
                        <MudLink Href="@(RouteConstants.Gateway.StoreChooser)" Typo="Typo.subtitle1"
                                 OnClick="() => _drawerOpen=false">فروشگاه‌ها</MudLink>
                    }
                    <MudDivider Class="my-1" />
                }
                <MudLink Href="@(RouteConstants.FAQ.Index)" Typo="Typo.subtitle1" OnClick="() => _drawerOpen=false">
                    سوالات متداول
                </MudLink>
                <MudLink Href="@(RouteConstants.Blog.Index)" Typo="Typo.subtitle1" OnClick="() => _drawerOpen=false">
                    بلاگ
                </MudLink>
                <MudLink Href="@(RouteConstants.About.Index)" Typo="Typo.subtitle1" OnClick="() => _drawerOpen=false">
                    درباره ما
                </MudLink>
                <MudLink Href="@(RouteConstants.Contact.Index)" Typo="Typo.subtitle1" OnClick="() => _drawerOpen=false">
                    ارتباط با ما
                </MudLink>
                <MudLink Href="@(RouteConstants.Licenses.Index)" Typo="Typo.subtitle1" OnClick="() => _drawerOpen=false">
                    مجوزها
                </MudLink>

                <MudDivider Class="my-2"/>
                @if (_isAuthenticated)
                {
                    <MudButton Href="/profile" Color="Color.Primary" OnClick="() => _drawerOpen=false">پروفایل
                    </MudButton>
                    @if (_hasUpdate)
                    {
                        <MudButton Variant="Variant.Filled" Color="Color.Warning" 
                                   StartIcon="@Icons.Material.Filled.SystemUpdateAlt"
                                   OnClick="() => { _drawerOpen=false; OpenUpdateDialog(); }">
                            بروزرسانی (@_newVersion)
                        </MudButton>
                    }
                    <MudButton Variant="Variant.Text" 
                               StartIcon="@(_isDark ? Icons.Material.Filled.LightMode : Icons.Material.Filled.DarkMode)"
                               OnClick="ToggleTheme">
                        @(_isDark ? "حالت روشن" : "حالت تاریک")
                    </MudButton>
                    <MudButton Variant="Variant.Text" Color="Color.Error"
                               OnClick="() => { _drawerOpen=false; Logout(); }">خروج از حساب
                    </MudButton>
                }
                else
                {
                    <MudButton Color="Color.Primary" OnClick="() => { _drawerOpen=false; OpenAuthDialog(); }">ورود
                    </MudButton>
                    <MudButton Variant="Variant.Text" 
                               StartIcon="@(_isDark ? Icons.Material.Filled.LightMode : Icons.Material.Filled.DarkMode)"
                               OnClick="ToggleTheme">
                        @(_isDark ? "حالت روشن" : "حالت تاریک")
                    </MudButton>
                }
            </MudStack>
        </MudDrawer>

        <MudMainContent Class="main-content-wrapper">
            @Body
            <MudHidden Breakpoint="Breakpoint.SmAndDown">
                <Footer/>
            </MudHidden>
        </MudMainContent>

        <!-- Mobile Bottom Navigation -->
        <MudHidden Breakpoint="Breakpoint.MdAndUp">
            <MudPaper Class="bottom-nav" Elevation="8">
                <MudStack Row="true" Justify="Justify.SpaceAround" AlignItems="AlignItems.Center" Class="py-1">
                    @if (IsInRegularStore && _isAuthenticated)
                    {
                        @* ── Regular store bottom nav ── *@
                        <MudLink Href="@(RouteConstants.Store.Products)" Class="bottom-nav-item">
                            <MudIcon Icon="@Icons.Material.Outlined.Storefront" Size="Size.Medium" />
                            <MudText Typo="Typo.caption">محصولات</MudText>
                        </MudLink>
                        <MudLink Href="@(RouteConstants.Store.Cart)" Class="bottom-nav-item">
                            <MudIcon Icon="@Icons.Material.Outlined.ShoppingCart" Size="Size.Medium" Color="@(_cartCount > 0 ? Color.Success : Color.Default)" />
                            <MudText Typo="Typo.caption">سبد خرید</MudText>
                        </MudLink>
                        <MudLink Href="@(RouteConstants.Store.Orders)" Class="bottom-nav-item">
                            <MudIcon Icon="@Icons.Material.Outlined.Receipt" Size="Size.Medium" />
                            <MudText Typo="Typo.caption">سفارشات</MudText>
                        </MudLink>
                        <MudLink Href="@(RouteConstants.Profile.Index)" Class="bottom-nav-item">
                            <MudIcon Icon="@Icons.Material.Outlined.Home" Size="Size.Medium" />
                            <MudText Typo="Typo.caption">داشبورد</MudText>
                        </MudLink>
                    }
                    else if (IsInDiscountStore && _isAuthenticated)
                    {
                        @* ── Discount store bottom nav ── *@
                        <MudLink Href="@(RouteConstants.DiscountStore.Products)" Class="bottom-nav-item">
                            <MudIcon Icon="@Icons.Material.Outlined.Loyalty" Size="Size.Medium" Color="Color.Error" />
                            <MudText Typo="Typo.caption">محصولات</MudText>
                        </MudLink>
                        <MudLink Href="@(RouteConstants.DiscountStore.Cart)" Class="bottom-nav-item">
                            <MudIcon Icon="@Icons.Material.Outlined.ShoppingCart" Size="Size.Medium" Color="@(_discountCartCount > 0 ? Color.Error : Color.Default)" />
                            <MudText Typo="Typo.caption">سبد خرید</MudText>
                        </MudLink>
                        <MudLink Href="@(RouteConstants.DiscountStore.Orders)" Class="bottom-nav-item">
                            <MudIcon Icon="@Icons.Material.Outlined.Receipt" Size="Size.Medium" />
                            <MudText Typo="Typo.caption">سفارشات</MudText>
                        </MudLink>
                        <MudLink Href="@(RouteConstants.Profile.Index)" Class="bottom-nav-item">
                            <MudIcon Icon="@Icons.Material.Outlined.Home" Size="Size.Medium" />
                            <MudText Typo="Typo.caption">داشبورد</MudText>
                        </MudLink>
                    }
                    else
                    {
                        @* ── Default bottom nav (outside stores) ── *@
                        <MudLink Href="@(_isAuthenticated ? RouteConstants.Profile.Index : RouteConstants.Main.MainPage)" Class="bottom-nav-item">
                            <MudIcon Icon="@Icons.Material.Outlined.Home" Size="Size.Medium" />
                            <MudText Typo="Typo.caption">@(_isAuthenticated ? "داشبورد" : "خانه")</MudText>
                        </MudLink>
                        @if (_isAuthenticated)
                        {
                            <MudLink Href="@(RouteConstants.Gateway.StoreChooser)" Class="bottom-nav-item">
                                <MudIcon Icon="@Icons.Material.Outlined.Store" Size="Size.Medium" />
                                <MudText Typo="Typo.caption">فروشگاه‌ها</MudText>
                            </MudLink>
                        }
                        <MudLink Href="@(RouteConstants.Blog.Index)" Class="bottom-nav-item">
                            <MudIcon Icon="@Icons.Material.Outlined.Article" Size="Size.Medium" />
                            <MudText Typo="Typo.caption">بلاگ</MudText>
                        </MudLink>
                        @if (_isAuthenticated)
                        {
                            <MudLink Href="@(RouteConstants.Profile.Hub)" Class="bottom-nav-item">
                                <MudIcon Icon="@Icons.Material.Outlined.Person" Size="Size.Medium" />
                                <MudText Typo="Typo.caption">پروفایل</MudText>
                            </MudLink>
                        }
                        else
                        {
                            <MudLink Class="bottom-nav-item" @onclick="OpenAuthDialog">
                                <MudIcon Icon="@Icons.Material.Outlined.Login" Size="Size.Medium" />
                                <MudText Typo="Typo.caption">ورود</MudText>
                            </MudLink>
                        }
                    }
                </MudStack>
            </MudPaper>
        </MudHidden>

    </MudLayout>
</MudRTLProvider>
