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

<PageTitle>QuantEngine - Operations</PageTitle>

<MudText Typo="Typo.h4" Class="mb-2">Operational Report</MudText>
<MudText Typo="Typo.body2" Class="mb-4">Temp/operational_report.json만 읽는 운영 고정 화면입니다.</MudText>

<MudGrid Spacing="2" Class="mb-4">
    <MudItem xs="12" sm="3">
        <MudPaper Class="pa-4" Elevation="2">
            <MudText Typo="Typo.caption">Schema</MudText>
            <MudText Typo="Typo.h6">@SchemaVersion</MudText>
        </MudPaper>
    </MudItem>
    <MudItem xs="12" sm="3">
        <MudPaper Class="pa-4" Elevation="2">
            <MudText Typo="Typo.caption">Sections</MudText>
            <MudText Typo="Typo.h6">@SectionCountLabel</MudText>
        </MudPaper>
    </MudItem>
    <MudItem xs="12" sm="3">
        <MudPaper Class="pa-4" Elevation="2">
            <MudText Typo="Typo.caption">Source</MudText>
            <MudText Typo="Typo.h6">@SourceJson</MudText>
        </MudPaper>
    </MudItem>
    <MudItem xs="12" sm="3">
        <MudPaper Class="pa-4" Elevation="2">
            <MudText Typo="Typo.caption">Generated</MudText>
            <MudText Typo="Typo.h6">@GeneratedAt</MudText>
        </MudPaper>
    </MudItem>
</MudGrid>

<MudGrid Spacing="2" Class="mb-4">
    @foreach (var section in HighlightSections)
    {
        <MudItem xs="12" sm="6" md="3" @key="section.Name">
            <MudPaper Class="pa-4" Elevation="2">
                <MudText Typo="Typo.caption">@(section.Name)</MudText>
                <MudText Typo="Typo.h6">@(section.Title)</MudText>
                <MudText Typo="Typo.body2">@(section.Preview)</MudText>
            </MudPaper>
        </MudItem>
    }
</MudGrid>

<MudPaper Class="pa-4 mb-4" Elevation="2">
    <MudText Typo="Typo.h6" Class="mb-3">Report Health</MudText>
    <MudStack Spacing="1">
        <MudText Typo="Typo.body2">Status: <MudChip T="string" Color="@(HealthLabel == "PASS" ? Color.Success : Color.Warning)" Variant="Variant.Filled">@HealthLabel</MudChip></MudText>
        <MudText Typo="Typo.body2">Path: @ReportPath</MudText>
        <MudText Typo="Typo.body2">Sections rendered: @RenderedSectionCountLabel</MudText>
    </MudStack>
</MudPaper>

<MudPaper Class="pa-4" Elevation="2">
    <MudText Typo="Typo.h6" Class="mb-3">Sections</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 readonly List<OperationalReportSection> HighlightSections = new();
    private string SchemaVersion = "n/a";
    private string SourceJson = "n/a";
    private string GeneratedAt = "n/a";
    private string SectionCountLabel = "0";
    private string RenderedSectionCountLabel = "0";
    private string HealthLabel = "DATA_MISSING";
    private string ReportPath = "n/a";

    protected override async Task OnInitializedAsync()
    {
        try
        {
            var report = await Http.GetFromJsonAsync<OperationalReportData>("api/operational-report");
            if (report != null)
            {
                SchemaVersion = report.SchemaVersion;
                SourceJson = report.SourceJson;
                GeneratedAt = report.GeneratedAt;

                Sections.Clear();
                Sections.AddRange(report.Sections);

                HighlightSections.Clear();
                HighlightSections.AddRange(Sections.Take(4));

                SectionCountLabel = report.SectionCount.ToString();
                RenderedSectionCountLabel = Sections.Count.ToString();
                HealthLabel = Sections.Count > 0 ? "PASS" : "DATA_MISSING";
            }
        }
        catch
        {
            HealthLabel = "DATA_MISSING";
        }
    }
}
