@page "/dashboard"
@attribute [Authorize]
@using QuantEngine.Core.Infrastructure
@inject HttpClient Http

<PageTitle>Quant Engine - Dashboard</PageTitle>

<MudText Typo="Typo.h4" Class="mb-2">Quant Engine</MudText>
<MudText Typo="Typo.body2" Class="mb-4">운영 진입점입니다. 로그인 후 현재 스냅샷 상태와 리포트 경로만 표시합니다.</MudText>

<MudGrid Spacing="2" Class="mb-4">
    <MudItem xs="12" sm="4">
        <MudPaper Class="pa-4" Elevation="2">
            <MudText Typo="Typo.caption">Operational Report</MudText>
            <MudText Typo="Typo.h6">@ReportStateLabel</MudText>
            <MudText Typo="Typo.body2">@ReportPath</MudText>
        </MudPaper>
    </MudItem>
    <MudItem xs="12" sm="4">
        <MudPaper Class="pa-4" Elevation="2">
            <MudText Typo="Typo.caption">Sections</MudText>
            <MudText Typo="Typo.h6">@SectionCountLabel</MudText>
            <MudText Typo="Typo.body2">Temp/operational_report.json</MudText>
        </MudPaper>
    </MudItem>
    <MudItem xs="12" sm="4">
        <MudPaper Class="pa-4" Elevation="2">
            <MudText Typo="Typo.caption">Primary Route</MudText>
            <MudButton Variant="Variant.Filled" Color="Color.Primary" Href="/operations" Class="mt-2">Open Operations</MudButton>
        </MudPaper>
    </MudItem>
</MudGrid>

<MudGrid Spacing="2" Class="mb-4">
    <MudItem xs="12" md="8">
        <MudPaper Class="pa-4" Elevation="2">
            <MudText Typo="Typo.h6" Class="mb-3">Current State</MudText>
            <MudStack Spacing="1">
                <MudText Typo="Typo.body2">Status: <MudChip T="string" Color="@(ReportChipLabel == "READY" ? Color.Success : Color.Warning)" Variant="Variant.Filled">@ReportChipLabel</MudChip></MudText>
                <MudText Typo="Typo.body2">Generated: @GeneratedAtLabel</MudText>
                <MudText Typo="Typo.body2">Source: @SourceLabel</MudText>
                <MudText Typo="Typo.body2">Decision feed: @DecisionFeedLabel</MudText>
                <MudText Typo="Typo.body2">Factor feed: @FactorFeedLabel</MudText>
                <MudText Typo="Typo.body2">Raw feed: @RawFeedLabel</MudText>
            </MudStack>
        </MudPaper>
    </MudItem>
    <MudItem xs="12" md="4">
        <MudPaper Class="pa-4" Elevation="2">
            <MudText Typo="Typo.h6" Class="mb-3">Routing Notes</MudText>
            <ul style="margin: 0; padding-left: 18px;">
                <li>운영 데이터는 snapshot 우선입니다.</li>
                <li>Excel/GAS 의존 문구는 제거 대상입니다.</li>
                <li>숫자는 provenance 없으면 표시하지 않습니다.</li>
            </ul>
        </MudPaper>
    </MudItem>
</MudGrid>

<MudPaper Class="pa-4" Elevation="2">
    <MudText Typo="Typo.h6" Class="mb-3">Coverage Summary</MudText>
    @if (Sections.Count == 0)
    {
        <MudAlert Severity="Severity.Warning">DATA_MISSING: operational_report.json이 비어 있거나 아직 생성되지 않았습니다.</MudAlert>
    }
    else
    {
        <MudTable Items="@Sections" Dense="true" Hover="true">
            <HeaderContent>
                <MudTh>Name</MudTh>
                <MudTh>Title</MudTh>
                <MudTh>Preview</MudTh>
            </HeaderContent>
            <RowTemplate>
                <MudTd DataLabel="Name">@context.Name</MudTd>
                <MudTd DataLabel="Title">@context.Title</MudTd>
                <MudTd DataLabel="Preview">@context.Preview</MudTd>
            </RowTemplate>
        </MudTable>
    }
</MudPaper>

@code {
    private readonly List<OperationalReportSection> Sections = new();
    private string ReportStateLabel = "DATA_MISSING";
    private string ReportChipLabel = "DATA_MISSING";
    private string SectionCountLabel = "0";
    private string GeneratedAtLabel = "n/a";
    private string SourceLabel = "n/a";
    private string DecisionFeedLabel = "DISCONNECTED";
    private string FactorFeedLabel = "DISCONNECTED";
    private string RawFeedLabel = "DISCONNECTED";
    private string ReportPath = "n/a";

    protected override async Task OnInitializedAsync()
    {
        try
        {
            var report = await Http.GetFromJsonAsync<OperationalReportData>("api/operational-report");
            if (report != null)
            {
                Sections.Clear();
                Sections.AddRange(report.Sections);
                SectionCountLabel = report.SectionCount.ToString();
                GeneratedAtLabel = report.GeneratedAt;
                SourceLabel = report.SourceJson;
                ReportStateLabel = Sections.Count > 0 ? "READY" : "DATA_MISSING";
                ReportChipLabel = Sections.Count > 0 ? "READY" : "DATA_MISSING";
            }
        }
        catch
        {
            ReportStateLabel = "DATA_MISSING";
            ReportChipLabel = "DATA_MISSING";
        }
    }
}
