@page "/dashboard"
@rendermode InteractiveWebAssembly

@using QuantEngine.Core.Infrastructure
@using Microsoft.AspNetCore.Components.Authorization
@inject HttpClient Http
@inject AuthenticationStateProvider AuthStateProvider
@inject NavigationManager NavManager

<PageTitle>QuantEngine - Admin Dashboard</PageTitle>



<!-- Page Header -->
<div class="mb-6">
    <MudText Typo="Typo.h4" Class="mb-2">관리자 대시보드</MudText>
    <MudText Typo="Typo.body1" Class="text-muted">시스템 현황 및 데이터 수집 모니터링</MudText>
</div>

<!-- KPI Cards -->
<MudGrid Spacing="3" Class="mb-6">
    <!-- Total Runs -->
    <MudItem xs="12" sm="6" md="3">
        <MudPaper Class="pa-4 mud-card-kpi" Elevation="0" Style="border: 1px solid var(--mud-palette-divider);">
            <div class="d-flex justify-content-between align-items-start">
                <div>
                    <MudText Typo="Typo.caption" Class="text-muted mb-1">총 수집 실행</MudText>
                    <MudText Typo="Typo.h5" Class="text-primary">@TotalRuns</MudText>
                    <MudText Typo="Typo.body2" Class="text-muted mt-2">
                        <MudIcon Icon="@Icons.Material.Filled.TrendingUp" Size="Size.Small" Style="color: #4caf50;" />
                        이번 주 +@WeeklyRuns
                    </MudText>
                </div>
                <MudIcon Icon="@Icons.Material.Filled.PlayCircleOutline" Size="Size.Large" Class="text-primary" Style="opacity: 0.3;" />
            </div>
        </MudPaper>
    </MudItem>

    <!-- Success Rate -->
    <MudItem xs="12" sm="6" md="3">
        <MudPaper Class="pa-4 mud-card-kpi" Elevation="0" Style="border: 1px solid var(--mud-palette-divider);">
            <div class="d-flex justify-content-between align-items-start">
                <div>
                    <MudText Typo="Typo.caption" Class="text-muted mb-1">성공률</MudText>
                    <MudText Typo="Typo.h5" Class="text-success">@SuccessRate%</MudText>
                    <MudText Typo="Typo.body2" Class="text-muted mt-2">
                        <MudIcon Icon="@Icons.Material.Filled.CheckCircle" Size="Size.Small" Style="color: #4caf50;" />
                        최근 30일
                    </MudText>
                </div>
                <MudIcon Icon="@Icons.Material.Filled.Assessment" Size="Size.Large" Class="text-success" Style="opacity: 0.3;" />
            </div>
        </MudPaper>
    </MudItem>

    <!-- Recent Errors -->
    <MudItem xs="12" sm="6" md="3">
        <MudPaper Class="pa-4 mud-card-kpi" Elevation="0" Style="border: 1px solid var(--mud-palette-divider);">
            <div class="d-flex justify-content-between align-items-start">
                <div>
                    <MudText Typo="Typo.caption" Class="text-muted mb-1">최근 에러</MudText>
                    <MudText Typo="Typo.h5" Class="text-error">@RecentErrors</MudText>
                    <MudText Typo="Typo.body2" Class="text-muted mt-2">
                        <MudIcon Icon="@Icons.Material.Filled.ErrorOutline" Size="Size.Small" Style="color: #f44336;" />
                        지난 7일
                    </MudText>
                </div>
                <MudIcon Icon="@Icons.Material.Filled.WarningAmber" Size="Size.Large" Class="text-error" Style="opacity: 0.3;" />
            </div>
        </MudPaper>
    </MudItem>

    <!-- Last Sync -->
    <MudItem xs="12" sm="6" md="3">
        <MudPaper Class="pa-4 mud-card-kpi" Elevation="0" Style="border: 1px solid var(--mud-palette-divider);">
            <div class="d-flex justify-content-between align-items-start">
                <div>
                    <MudText Typo="Typo.caption" Class="text-muted mb-1">마지막 동기화</MudText>
                    <MudText Typo="Typo.h5">@LastSyncTime</MudText>
                    <MudText Typo="Typo.body2" Class="text-muted mt-2">
                        <MudChip T="string" Label="true" Size="Size.Small"
                                 Color="@(IsLastSyncSuccess ? Color.Success : Color.Warning)"
                                 Variant="Variant.Filled">
                            @(IsLastSyncSuccess ? "성공" : "경고")
                        </MudChip>
                    </MudText>
                </div>
                <MudIcon Icon="@Icons.Material.Filled.Schedule" Size="Size.Large" Class="text-secondary" Style="opacity: 0.3;" />
            </div>
        </MudPaper>
    </MudItem>
</MudGrid>

<!-- Main Content Grid -->
<MudGrid Spacing="3" Class="mb-6">
    <!-- Recent Activity Feed -->
    <MudItem xs="12" md="8">
        <MudPaper Class="pa-4" Elevation="1">
            <MudText Typo="Typo.h6" Class="mb-4">최근 활동</MudText>

            @if (RecentActivities.Count == 0)
            {
                <MudAlert Severity="Severity.Info">활동 기록이 없습니다.</MudAlert>
            }
            else
            {
                <MudStack Spacing="2">
                    @foreach (var activity in RecentActivities)
                    {
                        <div class="d-flex gap-3 pa-2" style="border-left: 3px solid @GetActivityColor(activity.Type); padding-left: 12px;">
                            <MudIcon Icon="@GetActivityIcon(activity.Type)" Size="Size.Medium" Color="@GetActivityColorEnum(activity.Type)" />
                            <div style="flex: 1;">
                                <MudText Typo="Typo.body2" Class="font-weight-500">@activity.Title</MudText>
                                <MudText Typo="Typo.caption" Class="text-muted">@activity.Timestamp.ToString("yyyy-MM-dd HH:mm:ss")</MudText>
                                <MudText Typo="Typo.body2" Class="mt-1">@activity.Description</MudText>
                            </div>
                        </div>
                    }
                </MudStack>
            }
        </MudPaper>
    </MudItem>

    <!-- System Status -->
    <MudItem xs="12" md="4">
        <MudPaper Class="pa-4" Elevation="1">
            <MudText Typo="Typo.h6" Class="mb-4">시스템 상태</MudText>

            <MudStack Spacing="2">
                <div class="d-flex justify-content-between align-items-center">
                    <MudText Typo="Typo.body2">API 서버</MudText>
                    <MudChip T="string" Label="true" Size="Size.Small" Color="Color.Success" Variant="Variant.Filled">온라인</MudChip>
                </div>
                <div class="d-flex justify-content-between align-items-center">
                    <MudText Typo="Typo.body2">데이터베이스</MudText>
                    <MudChip T="string" Label="true" Size="Size.Small" Color="Color.Success" Variant="Variant.Filled">연결됨</MudChip>
                </div>
                <div class="d-flex justify-content-between align-items-center">
                    <MudText Typo="Typo.body2">KIS API</MudText>
                    <MudChip T="string" Label="true" Size="Size.Small" Color="@(KisApiStatus ? Color.Success : Color.Warning)" Variant="Variant.Filled">
                        @(KisApiStatus ? "활성" : "비활성")
                    </MudChip>
                </div>
                <MudDivider Class="my-2" />
                <MudText Typo="Typo.caption" Class="text-muted">마지막 점검: @SystemCheckTime</MudText>
            </MudStack>
        </MudPaper>
    </MudItem>
</MudGrid>

<!-- Collections Table -->
<MudPaper Class="pa-4" Elevation="1">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <MudText Typo="Typo.h6">최근 데이터 수집 실행</MudText>
        <MudButton Variant="Variant.Filled" Color="Color.Primary" Size="Size.Small" OnClick="RefreshData">
            <MudIcon Icon="@Icons.Material.Filled.Refresh" Size="Size.Small" Class="mr-2" />
            새로고침
        </MudButton>
    </div>

    @if (Sections.Count == 0)
    {
        <MudAlert Severity="Severity.Info">데이터 수집 기록이 없습니다.</MudAlert>
    }
    else
    {
        <MudTable Items="@Sections" Dense="true" Hover="true" Striped="true">
            <HeaderContent>
                <MudTh>이름</MudTh>
                <MudTh>상태</MudTh>
                <MudTh>시작 시간</MudTh>
                <MudTh>작업</MudTh>
            </HeaderContent>
            <RowTemplate>
                <MudTd DataLabel="Name">
                    <MudText Typo="Typo.body2">@context.Name</MudText>
                </MudTd>
                <MudTd DataLabel="Status">
                    <MudChip T="string" Label="true" Size="Size.Small" Color="Color.Primary" Variant="Variant.Filled">
                        @context.Title
                    </MudChip>
                </MudTd>
                <MudTd DataLabel="Timestamp">
                    <MudText Typo="Typo.body2">@context.Preview</MudText>
                </MudTd>
                <MudTd DataLabel="Actions">
                    <MudButton Variant="Variant.Text" Size="Size.Small" Color="Color.Primary">상세</MudButton>
                </MudTd>
            </RowTemplate>
        </MudTable>
    }
</MudPaper>

<style>
    .mud-card-kpi {
        border-radius: 8px !important;
        transition: all 0.3s ease;
    }

    .mud-card-kpi:hover {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1) !important;
        transform: translateY(-2px);
    }

    .text-primary {
        color: var(--mud-palette-primary) !important;
    }

    .text-success {
        color: var(--mud-palette-success) !important;
    }

    .text-error {
        color: var(--mud-palette-error) !important;
    }

    .text-muted {
        color: var(--mud-palette-text-secondary) !important;
    }

    .font-weight-500 {
        font-weight: 500;
    }

    .gap-3 {
        gap: 1rem;
    }
</style>

@code {
    private readonly List<OperationalReportSection> Sections = new();
    private readonly List<ActivityLog> RecentActivities = new();

    // KPI values
    private int TotalRuns = 47;
    private int WeeklyRuns = 12;
    private int SuccessRate = 94;
    private int RecentErrors = 3;
    private string LastSyncTime = "2분 전";
    private bool IsLastSyncSuccess = true;
    private bool KisApiStatus = true;
    private string SystemCheckTime = DateTime.Now.ToString("HH:mm:ss");

    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthStateProvider.GetAuthenticationStateAsync();
        if (!(authState.User.Identity?.IsAuthenticated ?? false))
        {
            NavManager.NavigateTo("/Account/Login", forceLoad: true);
            return;
        }

        try
        {
            var report = await Http.GetFromJsonAsync<OperationalReportData>("api/operational-report");
            if (report != null)
            {
                Sections.Clear();
                Sections.AddRange(report.Sections);
            }
        }
        catch
        {
            // Handle error silently
        }

        LoadRecentActivities();
    }

    private void LoadRecentActivities()
    {
        RecentActivities.Clear();
        RecentActivities.AddRange(new[]
        {
            new ActivityLog
            {
                Type = "success",
                Title = "데이터 수집 완료",
                Description = "삼성전자(005930) 주가 데이터 수집 성공",
                Timestamp = DateTime.Now.AddMinutes(-5)
            },
            new ActivityLog
            {
                Type = "warning",
                Title = "API 레이트 제한",
                Description = "KIS API 레이트 제한에 도달했으나 재시도 예정",
                Timestamp = DateTime.Now.AddMinutes(-12)
            },
            new ActivityLog
            {
                Type = "success",
                Title = "대시보드 업데이트",
                Description = "포트폴리오 구성 분석 완료",
                Timestamp = DateTime.Now.AddMinutes(-35)
            },
            new ActivityLog
            {
                Type = "info",
                Title = "스케줄 실행",
                Description = "일일 정기 수집 작업 시작",
                Timestamp = DateTime.Now.AddHours(-1)
            }
        });
    }

    private async Task RefreshData()
    {
        await OnInitializedAsync();
    }

    private string GetActivityIcon(string type) => type switch
    {
        "success" => Icons.Material.Filled.CheckCircle,
        "warning" => Icons.Material.Filled.WarningAmber,
        "error" => Icons.Material.Filled.Error,
        _ => Icons.Material.Filled.Info
    };

    private string GetActivityColor(string type) => type switch
    {
        "success" => "#4caf50",
        "warning" => "#ff9800",
        "error" => "#f44336",
        _ => "#2196f3"
    };

    private Color GetActivityColorEnum(string type) => type switch
    {
        "success" => Color.Success,
        "warning" => Color.Warning,
        "error" => Color.Error,
        _ => Color.Info
    };

    private class ActivityLog
    {
        public string Type { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public DateTime Timestamp { get; set; }
    }
}
