@page
@model QuantEngine.Web.Pages.Admin.Monitoring.IndexModel
@{
    ViewData["Title"] = "모니터링 - QuantEngine";
}

<div class="page-header d-print-none">
    <div class="row align-items-center">
        <div class="col">
            <h2 class="page-title">실시간 모니터링</h2>
        </div>
        <div class="col-auto">
            <a href="javascript:location.reload()" class="btn btn-secondary">새로고침</a>
        </div>
    </div>
</div>

<div class="page-body">
    <div class="row row-deck row-cards">
        <!-- 진행 중인 작업 -->
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">진행 중인 작업</h3>
                </div>
                <div class="table-responsive">
                    <table class="table table-vcenter card-table">
                        <thead>
                            <tr>
                                <th>작업 ID</th>
                                <th>상태</th>
                                <th>시작 시간</th>
                                <th>진행률</th>
                            </tr>
                        </thead>
                        <tbody>
                            @if (Model.OngoingRuns?.Any() == true)
                            {
                                @foreach (var run in Model.OngoingRuns)
                                {
                                    <tr>
                                        <td><code>@run.RunId</code></td>
                                        <td><span class="badge bg-warning">진행 중</span></td>
                                        <td>@(DateTime.TryParse(run.StartedAt?.ToString(), out var dt) ? dt.ToString("yyyy-MM-dd HH:mm:ss") : (run.StartedAt?.ToString() ?? "-"))</td>
                                        <td>
                                            @if (run.TotalSnapshots > 0)
                                            {
                                                var progressPercent = (int)((run.TotalSnapshots * 100) / (run.TotalSnapshots + (run.TotalErrors ?? 0) + 1));
                                                <div class="progress progress-sm">
                                                    <div class="progress-bar bg-info" style="width: @progressPercent%"></div>
                                                </div>
                                            }
                                            else
                                            {
                                                <span class="text-muted">-</span>
                                            }
                                        </td>
                                    </tr>
                                }
                            }
                            else
                            {
                                <tr>
                                    <td colspan="4" class="text-center text-muted">진행 중인 작업이 없습니다</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <!-- 최근 실행 통계 -->
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">최근 24시간 통계</h3>
                </div>
                <div class="card-body">
                    <div class="row row-sm">
                        <div class="col-auto">
                            <div class="text-muted">전체 실행</div>
                            <div class="h2">@Model.TotalRuns24h</div>
                        </div>
                        <div class="col-auto">
                            <div class="text-muted">성공</div>
                            <div class="h2 text-success">@Model.SuccessRuns24h</div>
                        </div>
                        <div class="col-auto">
                            <div class="text-muted">실패</div>
                            <div class="h2 text-danger">@Model.FailedRuns24h</div>
                        </div>
                        <div class="col-auto">
                            <div class="text-muted">성공률</div>
                            <div class="h2">@(Model.TotalRuns24h > 0 ? ((Model.SuccessRuns24h * 100) / Model.TotalRuns24h).ToString("F0") : 0)%</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 시스템 상태 -->
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">시스템 상태</h3>
                </div>
                <div class="card-body">
                    <div class="list-group list-group-flush">
                        <div class="list-group-item">
                            <div class="row align-items-center">
                                <div class="col">
                                    <strong>데이터베이스</strong>
                                </div>
                                <div class="col-auto">
                                    <span class="badge bg-success">연결 정상</span>
                                </div>
                            </div>
                        </div>
                        <div class="list-group-item">
                            <div class="row align-items-center">
                                <div class="col">
                                    <strong>API 서버</strong>
                                </div>
                                <div class="col-auto">
                                    <span class="badge bg-success">운영 중</span>
                                </div>
                            </div>
                        </div>
                        <div class="list-group-item">
                            <div class="row align-items-center">
                                <div class="col">
                                    <strong>마지막 갱신</strong>
                                </div>
                                <div class="col-auto">
                                    <span class="text-muted">@(Model.LastRefreshTime?.ToString("HH:mm:ss") ?? "-")</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 최근 에러 -->
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">최근 에러 (상위 10개)</h3>
                </div>
                <div class="table-responsive">
                    <table class="table table-sm table-vcenter card-table">
                        <thead>
                            <tr>
                                <th>실행 ID</th>
                                <th>에러 종류</th>
                                <th>메시지</th>
                                <th>발생 시간</th>
                            </tr>
                        </thead>
                        <tbody>
                            @if (Model.RecentErrors?.Any() == true)
                            {
                                @foreach (var error in Model.RecentErrors)
                                {
                                    <tr>
                                        <td><small><code>@error.RunId</code></small></td>
                                        <td><span class="badge bg-danger">@error.ErrorKind</span></td>
                                        <td class="text-muted">@(error.ErrorMessage?.Length > 50 ? error.ErrorMessage.Substring(0, 50) + "..." : error.ErrorMessage ?? "-")</td>
                                        <td><small>@(DateTime.TryParse(error.CreatedAt?.ToString(), out var dt) ? dt.ToString("HH:mm:ss") : (error.CreatedAt?.ToString() ?? "-"))</small></td>
                                    </tr>
                                }
                            }
                            else
                            {
                                <tr>
                                    <td colspan="4" class="text-center text-muted">최근 에러가 없습니다</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
