@page "/commission/dashboard"
@attribute [Authorize]

@using CMSMicroservice.Protobuf.Protos.Commission
@using MudBlazor
@using BackOffice.Pages.AutoComplete

<div>
    <MudText Typo="Typo.h4" Class="mb-4">داشبورد کمیسیون</MudText>

    @* ── G11: راهنمای کمیسیون (Q28) ── *@
    <MudAlert Severity="Severity.Info" Variant="Variant.Text" Dense="true" Class="mb-3" Icon="@Icons.Material.Filled.Info">
        هر پکیج Pool پورسانت مستقل دارد. مبالغ ActivationFee به Pool پکیج مربوطه واریز می‌شود.
    </MudAlert>

    @if (_isLoading)
    {
        <MudProgressCircular Color="Color.Primary" Indeterminate="true" />
    }
    else
    {
        <!-- Pool Summary Section -->
        <MudGrid>
            <MudItem xs="12" sm="6" md="3">
                <MudCard Elevation="2">
                    <MudCardContent>
                        <MudText Typo="Typo.h6" Color="Color.Primary">استخر هفتگی</MudText>
                        <MudText Typo="Typo.h4">@(_poolData?.TotalPoolAmount.ToString("N0") ?? "0") تومان</MudText>
                        <MudText Typo="Typo.body2" Color="Color.Secondary">@_currentWeekDisplayText</MudText>
                    </MudCardContent>
                </MudCard>
            </MudItem>

            <MudItem xs="12" sm="6" md="3">
                <MudCard Elevation="2">
                    <MudCardContent>
                        <MudText Typo="Typo.h6" Color="Color.Success">تعداد تعادلها</MudText>
                        <MudText Typo="Typo.h4">@(_poolData?.TotalBalances.ToString("N0") ?? "0")</MudText>
                        <MudText Typo="Typo.body2" Color="Color.Secondary">مجموع تعادل های فعال</MudText>
                    </MudCardContent>
                </MudCard>
            </MudItem>

            <MudItem xs="12" sm="6" md="3">
                <MudCard Elevation="2">
                    <MudCardContent>
                        <MudText Typo="Typo.h6" Color="Color.Warning">ارزش هر تعادل</MudText>
                        <MudText Typo="Typo.h4">@(_poolData?.ValuePerBalance.ToString("N0") ?? "0") تومان</MudText>
                        <MudText Typo="Typo.body2" Color="Color.Secondary">قیمت واحد تعادل</MudText>
                    </MudCardContent>
                </MudCard>
            </MudItem>

            <MudItem xs="12" sm="6" md="3">
                <MudCard Elevation="2">
                    <MudCardContent>
                        <MudText Typo="Typo.h6" Color="@(_poolData?.IsCalculated == true ? Color.Success : Color.Error)">
                            وضعیت محاسبه
                        </MudText>
                        <MudText Typo="Typo.h4">
                            @(_poolData?.IsCalculated == true ? "محاسبه شده ✓" : "محاسبه نشده ✗")
                        </MudText>
                        <MudText Typo="Typo.body2" Color="Color.Secondary">
                            @if (_poolData?.CalculatedAt != null)
                            {
                                var calculatedDate = _poolData.CalculatedAt.ToDateTime().ToLocalTime();
                                var persianDate = PersianDateTime.ConvertToPersianDateTime(calculatedDate);
                                @($"در تاریخ {persianDate}")
                            }
                            else
                            {
                                @("در انتظار محاسبه")
                            }
                        </MudText>
                    </MudCardContent>
                </MudCard>
            </MudItem>
        </MudGrid>

        <!-- Week Selector -->
        <MudPaper Class="pa-4 mt-4" Elevation="2">
            <MudStack Row="true" AlignItems="AlignItems.Center" Spacing="3">
                <MudText Typo="Typo.h6">انتخاب هفته:</MudText>
                
                @if (_allWeeks.Any())
                {
                    <MudSelect T="long" 
                              Value="_currentWeekDefinitionId"
                              Label="شماره هفته" 
                              Variant="Variant.Outlined" 
                              Margin="Margin.Dense"
                              AnchorOrigin="Origin.BottomCenter"
                              Style="min-width: 400px;"
                              ValueChanged="OnWeekChanged">
                        
                        @if (_availableWeeks?.CurrentWeek != null)
                        {
                            <MudSelectItem T="long" Value="@_availableWeeks.CurrentWeek.WeekDefinitionId">
                                <MudText>
                                    <MudIcon Icon="@Icons.Material.Filled.Today" Size="Size.Small" Class="ml-2" />
                                    @_availableWeeks.CurrentWeek.DisplayText
                                </MudText>
                            </MudSelectItem>
                            <MudDivider />
                        }
                        
                        @if (_availableWeeks?.CalculatedWeeks?.Any() == true)
                        {
                            <MudListSubheader>هفته‌های محاسبه شده</MudListSubheader>
                            @foreach (var week in _availableWeeks.CalculatedWeeks)
                            {
                                <MudSelectItem T="long" Value="@week.WeekDefinitionId">
                                    <MudText>
                                        <MudIcon Icon="@Icons.Material.Filled.CheckCircle" Color="Color.Success" Size="Size.Small" Class="ml-2" />
                                        @week.DisplayText
                                        @if (week.TotalPoolAmount > 0)
                                        {
                                            <MudChip T="string" Size="Size.Small" Color="Color.Success" Class="mr-2">
                                                @week.TotalPoolAmount.ToString("N0") تومان
                                            </MudChip>
                                        }
                                    </MudText>
                                </MudSelectItem>
                            }
                            <MudDivider />
                        }
                        
                        @if (_availableWeeks?.PendingWeeks?.Any() == true)
                        {
                            <MudListSubheader>هفته‌های محاسبه نشده</MudListSubheader>
                            @foreach (var week in _availableWeeks.PendingWeeks)
                            {
                                <MudSelectItem T="long" Value="@week.WeekDefinitionId">
                                    <MudText>
                                        <MudIcon Icon="@Icons.Material.Filled.AccessTime" Color="Color.Warning" Size="Size.Small" Class="ml-2" />
                                        @week.DisplayText
                                    </MudText>
                                </MudSelectItem>
                            }
                            <MudDivider />
                        }
                        
                        @if (_availableWeeks?.FutureWeeks?.Any() == true)
                        {
                            <MudListSubheader>هفته‌های آینده</MudListSubheader>
                            @foreach (var week in _availableWeeks.FutureWeeks)
                            {
                                <MudSelectItem T="long" Value="@week.WeekDefinitionId">
                                    <MudText>
                                        <MudIcon Icon="@Icons.Material.Filled.CalendarMonth" Color="Color.Info" Size="Size.Small" Class="ml-2" />
                                        @week.DisplayText
                                    </MudText>
                                </MudSelectItem>
                            }
                        }
                    </MudSelect>
                }
                else
                {
                    <MudText Color="Color.Warning">لیست هفته‌ها بارگذاری نشده</MudText>
                }
                
                <MudButton Variant="Variant.Filled" 
                          Color="Color.Primary" 
                          OnClick="LoadPoolData"
                          StartIcon="@Icons.Material.Filled.Refresh">
                    بارگذاری
                </MudButton>
            </MudStack>
        </MudPaper>

        <!-- Pool Details -->
        @if (_poolData != null)
        {
            <MudPaper Class="pa-4 mt-4" Elevation="2">
                <MudText Typo="Typo.h6" Class="mb-3">جزئیات استخر</MudText>
                <MudSimpleTable Hover="true" Dense="true">
                    <tbody>
                        <tr>
                            <td><strong>شناسه استخر:</strong></td>
                            <td>@_poolData.Id</td>
                        </tr>
                        <tr>
                            <td><strong>شماره هفته:</strong></td>
                            <td>@_currentWeekDisplayName</td>
                        </tr>
                        <tr>
                            <td><strong>مجموع استخر:</strong></td>
                            <td>@_poolData.TotalPoolAmount.ToString("N0") تومان</td>
                        </tr>
                        <tr>
                            <td><strong>تعداد تعادل:</strong></td>
                            <td>@_poolData.TotalBalances</td>
                        </tr>
                        <tr>
                            <td><strong>ارزش هر تعادل:</strong></td>
                            <td>@_poolData.ValuePerBalance.ToString("N0") تومان</td>
                        </tr>
                        <tr>
                            <td><strong>وضعیت:</strong></td>
                            <td>
                                <MudChip T="string" Color="@(_poolData.IsCalculated ? Color.Success : Color.Error)" Size="Size.Small">
                                    @(_poolData.IsCalculated ? "محاسبه شده" : "محاسبه نشده")
                                </MudChip>
                            </td>
                        </tr>
                        <tr>
                            <td><strong>تاریخ محاسبه:</strong></td>
                            <td>
                                @if (_poolData.CalculatedAt != null)
                                {
                                    @_poolData.CalculatedAt.ToDateTime().ToLocalTime().MiladiToJalaliWithTime()
                                }
                                else
                                {
                                    <MudText Color="Color.Secondary">-</MudText>
                                }
                            </td>
                        </tr>
                        <tr>
                            <td><strong>تاریخ ایجاد:</strong></td>
                            <td>@_poolData.Created.ToDateTime().ToLocalTime().MiladiToJalaliWithTime()</td>
                        </tr>
                    </tbody>
                </MudSimpleTable>
            </MudPaper>
        }

        <!-- Quick Actions -->
        <MudPaper Class="pa-4 mt-4" Elevation="2">
            <MudText Typo="Typo.h6" Class="mb-3">عملیات سریع</MudText>
            <MudStack Row="true" Spacing="2">
                <MudButton Variant="Variant.Filled" 
                          Color="Color.Primary" 
                          StartIcon="@Icons.Material.Filled.Payments"
                          Href="/commission/payouts">
                    مشاهده پرداخت‌ها
                </MudButton>
                <MudButton Variant="Variant.Filled" 
                          Color="Color.Secondary" 
                          StartIcon="@Icons.Material.Filled.AccountBalance"
                          Href="/commission/withdrawals">
                    درخواست‌های برداشت
                </MudButton>
                <MudButton Variant="Variant.Outlined" 
                          Color="Color.Info" 
                          StartIcon="@Icons.Material.Filled.Calculate"
                          Disabled="@(_poolData?.IsCalculated == true)"
                          OnClick="TriggerCalculation">
                    اجرای محاسبه دستی
                </MudButton>
            </MudStack>
        </MudPaper>
    }
</div>
