@page "/dashboard"
@attribute [Authorize]
@rendermode InteractiveServer
@implements IDisposable

@inject ICVCheckService CVCheckService
@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject ILogger<Dashboard> Logger
@inject IJSRuntime JSRuntime
@inject RealCV.Web.Services.IPdfReportService PdfReportService
@inject IAuditService AuditService

<PageTitle>Dashboard - RealCV</PageTitle>

<div class="container py-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="fw-bold mb-1">Dashboard</h1>
            <p class="text-muted mb-0">View and manage your CV verification checks</p>
        </div>
        <div class="d-flex gap-2">
            <button class="btn btn-outline-secondary" @onclick="ExportToPdf" disabled="@(_isExporting || !HasCompletedChecks())">
                @if (_isExporting)
                {
                    <span class="spinner-border spinner-border-sm me-1" role="status"></span>
                }
                else
                {
                    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-file-pdf me-1" viewBox="0 0 16 16">
                        <path d="M4 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H4zm0 1h8a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1z"/>
                        <path d="M4.603 12.087a.81.81 0 0 1-.438-.42c-.195-.388-.13-.776.08-1.102.198-.307.526-.568.897-.787a7.68 7.68 0 0 1 1.482-.645 19.701 19.701 0 0 0 1.062-2.227 7.269 7.269 0 0 1-.43-1.295c-.086-.4-.119-.796-.046-1.136.075-.354.274-.672.65-.823.192-.077.4-.12.602-.077a.7.7 0 0 1 .477.365c.088.164.12.356.127.538.007.187-.012.395-.047.614-.084.51-.27 1.134-.52 1.794a10.954 10.954 0 0 0 .98 1.686 5.753 5.753 0 0 1 1.334.05c.364.065.734.195.96.465.12.144.193.32.2.518.007.192-.047.382-.138.563a1.04 1.04 0 0 1-.354.416.856.856 0 0 1-.51.138c-.331-.014-.654-.196-.933-.417a5.716 5.716 0 0 1-.911-.95 11.642 11.642 0 0 0-1.997.406 11.311 11.311 0 0 1-1.021 1.51c-.29.35-.608.655-.926.787a.793.793 0 0 1-.58.029zm1.379-1.901c-.166.076-.32.156-.459.238-.328.194-.541.383-.647.547-.094.145-.096.25-.04.361.01.022.02.036.026.044a.27.27 0 0 0 .035-.012c.137-.056.355-.235.635-.572a8.18 8.18 0 0 0 .45-.606zm1.64-1.33a12.647 12.647 0 0 1 1.01-.193 11.666 11.666 0 0 1-.51-.858 20.741 20.741 0 0 1-.5 1.05zm2.446.45c.15.162.296.3.435.41.24.19.407.253.498.256a.107.107 0 0 0 .07-.015.307.307 0 0 0 .094-.125.436.436 0 0 0 .059-.2.095.095 0 0 0-.026-.063c-.052-.062-.2-.152-.518-.209a3.881 3.881 0 0 0-.612-.053zM8.078 5.8a6.7 6.7 0 0 0 .2-.828c.031-.188.043-.343.038-.465a.613.613 0 0 0-.032-.198.517.517 0 0 0-.145.04c-.087.035-.158.106-.196.283-.04.192-.03.469.046.822.024.111.054.227.09.346z"/>
                    </svg>
                }
                Export PDF
            </button>
            <a href="/check" class="btn btn-primary">
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-plus-lg me-1" viewBox="0 0 16 16">
                    <path fill-rule="evenodd" d="M8 2a.5.5 0 0 1 .5.5v5h5a.5.5 0 0 1 0 1h-5v5a.5.5 0 0 1-1 0v-5h-5a.5.5 0 0 1 0-1h5v-5A.5.5 0 0 1 8 2Z"/>
                </svg>
                New Check
            </a>
        </div>
    </div>

    @if (_isLoading)
    {
        <div class="text-center py-5">
            <div class="placeholder-glow mb-4">
                <div class="row g-4 mb-4">
                    <div class="col-md-4"><div class="placeholder col-12 rounded-4" style="height: 100px;"></div></div>
                    <div class="col-md-4"><div class="placeholder col-12 rounded-4" style="height: 100px;"></div></div>
                    <div class="col-md-4"><div class="placeholder col-12 rounded-4" style="height: 100px;"></div></div>
                </div>
                <div class="placeholder col-12 rounded-4" style="height: 300px;"></div>
            </div>
            <div class="spinner-border text-primary" role="status" style="width: 2.5rem; height: 2.5rem;">
                <span class="visually-hidden">Loading...</span>
            </div>
            <p class="mt-3 text-muted fw-medium">Loading your checks...</p>
        </div>
    }
    else if (!string.IsNullOrEmpty(_errorMessage))
    {
        <div class="alert alert-danger" role="alert">
            @_errorMessage
        </div>
    }
    else if (_checks.Count == 0)
    {
        <div class="card border-0 shadow-sm">
            <div class="card-body text-center py-5 px-4">
                <div class="empty-state-icon mb-4">
                    <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" fill="currentColor" class="bi bi-file-earmark-plus" viewBox="0 0 16 16">
                        <path d="M8 6.5a.5.5 0 0 1 .5.5v1.5H10a.5.5 0 0 1 0 1H8.5V11a.5.5 0 0 1-1 0V9.5H6a.5.5 0 0 1 0-1h1.5V7a.5.5 0 0 1 .5-.5z"/>
                        <path d="M14 4.5V14a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V2a2 2 0 0 1 2-2h5.5L14 4.5zm-3 0A1.5 1.5 0 0 1 9.5 3V1H4a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V4.5h-2z"/>
                    </svg>
                </div>
                <h4 class="fw-bold mb-2">No CV Checks Yet</h4>
                <p class="text-muted mb-4 mx-auto" style="max-width: 400px;">
                    Upload your first CV to begin verifying employment history against official company records.
                </p>
                <a href="/check" class="btn btn-primary btn-lg">
                    <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" fill="currentColor" class="bi bi-upload me-2" viewBox="0 0 16 16">
                        <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/>
                        <path d="M7.646 1.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 2.707V11.5a.5.5 0 0 1-1 0V2.707L5.354 4.854a.5.5 0 1 1-.708-.708l3-3z"/>
                    </svg>
                    Upload Your First CV
                </a>
            </div>
        </div>
    }
    else
    {
        <!-- Stats Cards -->
        <div class="row mb-4 g-4">
            <div class="col-md-4">
                <div class="card border-0 shadow-sm stat-card h-100">
                    <div class="card-body p-4">
                        <div class="d-flex align-items-center">
                            <div class="stat-icon stat-icon-primary me-3">
                                <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" fill="currentColor" viewBox="0 0 16 16">
                                    <path d="M10.854 7.854a.5.5 0 0 0-.708-.708L7.5 9.793 6.354 8.646a.5.5 0 1 0-.708.708l1.5 1.5a.5.5 0 0 0 .708 0l3-3z"/>
                                    <path d="M14 14V4.5L9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2zM9.5 3A1.5 1.5 0 0 0 11 4.5h2V14a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h5.5v2z"/>
                                </svg>
                            </div>
                            <div>
                                <div class="stat-value">@_checks.Count</div>
                                <div class="stat-label">Total Checks</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card border-0 shadow-sm stat-card h-100">
                    <div class="card-body p-4">
                        <div class="d-flex align-items-center">
                            <div class="stat-icon stat-icon-success me-3">
                                <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" fill="currentColor" viewBox="0 0 16 16">
                                    <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/>
                                    <path d="M10.97 4.97a.235.235 0 0 0-.02.022L7.477 9.417 5.384 7.323a.75.75 0 0 0-1.06 1.06L6.97 11.03a.75.75 0 0 0 1.079-.02l3.992-4.99a.75.75 0 0 0-1.071-1.05z"/>
                                </svg>
                            </div>
                            <div>
                                <div class="stat-value">@_checks.Count(c => c.Status == "Completed")</div>
                                <div class="stat-label">Completed</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card border-0 shadow-sm stat-card h-100">
                    <div class="card-body p-4">
                        <div class="d-flex align-items-center">
                            <div class="stat-icon stat-icon-warning me-3">
                                <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" fill="currentColor" viewBox="0 0 16 16">
                                    <path d="M2.5 15a.5.5 0 1 1 0-1h1v-1a4.5 4.5 0 0 1 2.557-4.06c.29-.139.443-.377.443-.59v-.7c0-.213-.154-.451-.443-.59A4.5 4.5 0 0 1 3.5 3V2h-1a.5.5 0 0 1 0-1h11a.5.5 0 0 1 0 1h-1v1a4.5 4.5 0 0 1-2.557 4.06c-.29.139-.443.377-.443.59v.7c0 .213.154.451.443.59A4.5 4.5 0 0 1 12.5 13v1h1a.5.5 0 0 1 0 1h-11zm2-13v1c0 .537.12 1.045.337 1.5h6.326c.216-.455.337-.963.337-1.5V2h-7zm3 6.35c0 .701-.478 1.236-1.011 1.492A3.5 3.5 0 0 0 4.5 13s.866-1.299 3-1.48V8.35zm1 0v3.17c2.134.181 3 1.48 3 1.48a3.5 3.5 0 0 0-1.989-3.158C8.978 9.586 8.5 9.052 8.5 8.351z"/>
                                </svg>
                            </div>
                            <div>
                                <div class="stat-value">@_checks.Count(c => c.Status is "Pending" or "Processing")</div>
                                <div class="stat-label">In Progress</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Checks List -->
        <div class="card border-0 shadow-sm">
            <div class="card-header py-3 border-bottom" style="background-color: var(--truecv-bg-surface);">
                <div class="d-flex justify-content-between align-items-center">
                    <div class="d-flex align-items-center gap-3">
                        <h5 class="mb-0 fw-bold">Recent CV Checks</h5>
                        @if (_selectedIds.Count > 0)
                        {
                            <span class="badge bg-primary">@_selectedIds.Count selected</span>
                        }
                    </div>
                    <div class="d-flex align-items-center gap-2">
                        @if (_selectedIds.Count > 0)
                        {
                            <button class="btn btn-sm btn-outline-danger" @onclick="ConfirmDeleteSelected">
                                <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="bi bi-trash3 me-1" viewBox="0 0 16 16">
                                    <path d="M6.5 1h3a.5.5 0 0 1 .5.5v1H6v-1a.5.5 0 0 1 .5-.5ZM11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3A1.5 1.5 0 0 0 5 1.5v1H2.506a.58.58 0 0 0-.01 0H1.5a.5.5 0 0 0 0 1h.538l.853 10.66A2 2 0 0 0 4.885 16h6.23a2 2 0 0 0 1.994-1.84l.853-10.66h.538a.5.5 0 0 0 0-1h-.995a.59.59 0 0 0-.01 0H11Zm1.958 1-.846 10.58a1 1 0 0 1-.997.92h-6.23a1 1 0 0 1-.997-.92L3.042 3.5h9.916Zm-7.487 1a.5.5 0 0 1 .528.47l.5 8.5a.5.5 0 0 1-.998.06L5 5.03a.5.5 0 0 1 .47-.53Zm5.058 0a.5.5 0 0 1 .47.53l-.5 8.5a.5.5 0 1 1-.998-.06l.5-8.5a.5.5 0 0 1 .528-.47ZM8 4.5a.5.5 0 0 1 .5.5v8.5a.5.5 0 0 1-1 0V5a.5.5 0 0 1 .5-.5Z"/>
                                </svg>
                                Delete Selected
                            </button>
                        }
                        <span class="badge bg-light text-muted">@_checks.Count total</span>
                    </div>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr style="background-color: var(--truecv-bg-muted);">
                            <th class="border-0 ps-3 py-3" style="width: 40px;">
                                <input type="checkbox" class="form-check-input"
                                       checked="@IsAllSelected()"
                                       @onchange="ToggleSelectAll"
                                       title="Select all" />
                            </th>
                            <th class="border-0 py-3 text-uppercase small fw-semibold text-muted" style="letter-spacing: 0.05em;">Candidate</th>
                            <th class="border-0 py-3 text-uppercase small fw-semibold text-muted" style="letter-spacing: 0.05em;">Uploaded</th>
                            <th class="border-0 py-3 text-uppercase small fw-semibold text-muted text-center" style="letter-spacing: 0.05em;">Status</th>
                            <th class="border-0 py-3 text-uppercase small fw-semibold text-muted text-center" style="letter-spacing: 0.05em;">Score</th>
                            <th class="border-0 py-3 pe-4 text-uppercase small fw-semibold text-muted text-end" style="letter-spacing: 0.05em;">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var check in _checks)
                        {
                            <tr class="@(check.Status == "Completed" ? "cursor-pointer" : "") @(_selectedIds.Contains(check.Id) ? "table-active" : "")"
                                @onclick="() => ViewReport(check)">
                                <td class="ps-3 py-3" @onclick:stopPropagation="true">
                                    <input type="checkbox" class="form-check-input"
                                           checked="@_selectedIds.Contains(check.Id)"
                                           @onchange="() => ToggleSelection(check.Id)" />
                                </td>
                                <td class="py-3">
                                    <div class="d-flex align-items-center">
                                        <div class="file-icon-wrapper me-3">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-file-earmark-person text-primary" viewBox="0 0 16 16">
                                                <path d="M11 8a3 3 0 1 1-6 0 3 3 0 0 1 6 0z"/>
                                                <path d="M14 14V4.5L9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2zM9.5 3A1.5 1.5 0 0 0 11 4.5h2v9.255S12 12 8 12s-5 1.755-5 1.755V2a1 1 0 0 1 1-1h5.5v2z"/>
                                            </svg>
                                        </div>
                                        <div>
                                            <p class="mb-0 fw-semibold text-dark">@Path.GetFileNameWithoutExtension(check.OriginalFileName)</p>
                                            <small class="text-muted">@Path.GetExtension(check.OriginalFileName).ToUpperInvariant()</small>
                                        </div>
                                    </div>
                                </td>
                                <td class="py-3">
                                    <div>
                                        <p class="mb-0 small">@check.CreatedAt.ToString("dd MMM yyyy")</p>
                                        <small class="text-muted">@check.CreatedAt.ToString("HH:mm")</small>
                                    </div>
                                </td>
                                <td class="py-3 text-center">
                                    @switch (check.Status)
                                    {
                                        case "Completed":
                                            <span class="badge rounded-pill bg-success-subtle text-success px-3 py-2">
                                                <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" class="bi bi-check-circle-fill me-1" viewBox="0 0 16 16">
                                                    <path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zm-3.97-3.03a.75.75 0 0 0-1.08.022L7.477 9.417 5.384 7.323a.75.75 0 0 0-1.06 1.06L6.97 11.03a.75.75 0 0 0 1.079-.02l3.992-4.99a.75.75 0 0 0-.01-1.05z"/>
                                                </svg>
                                                Completed
                                            </span>
                                            break;
                                        case "Processing":
                                            <span class="badge rounded-pill bg-primary-subtle text-primary px-3 py-2">
                                                <span class="spinner-border spinner-border-sm me-1" role="status" style="width: 0.75rem; height: 0.75rem;"></span>
                                                @(check.ProcessingStage ?? "Processing")
                                            </span>
                                            break;
                                        case "Pending":
                                            <span class="badge rounded-pill bg-secondary-subtle text-secondary px-3 py-2">
                                                <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" class="bi bi-clock me-1" viewBox="0 0 16 16">
                                                    <path d="M8 3.5a.5.5 0 0 0-1 0V9a.5.5 0 0 0 .252.434l3.5 2a.5.5 0 0 0 .496-.868L8 8.71V3.5z"/>
                                                    <path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zm7-8A7 7 0 1 1 1 8a7 7 0 0 1 14 0z"/>
                                                </svg>
                                                Queued
                                            </span>
                                            break;
                                        case "Failed":
                                            <span class="badge rounded-pill bg-danger-subtle text-danger px-3 py-2">
                                                <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" class="bi bi-x-circle-fill me-1" viewBox="0 0 16 16">
                                                    <path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM5.354 4.646a.5.5 0 1 0-.708.708L7.293 8l-2.647 2.646a.5.5 0 0 0 .708.708L8 8.707l2.646 2.647a.5.5 0 0 0 .708-.708L8.707 8l2.647-2.646a.5.5 0 0 0-.708-.708L8 7.293 5.354 4.646z"/>
                                                </svg>
                                                Failed
                                            </span>
                                            break;
                                        default:
                                            <span class="badge rounded-pill bg-secondary-subtle text-secondary px-3 py-2">@check.Status</span>
                                            break;
                                    }
                                </td>
                                <td class="py-3 text-center">
                                    @if (check.VeracityScore.HasValue)
                                    {
                                        <div class="score-ring-container" title="Veracity Score: @check.VeracityScore%">
                                            <svg class="score-ring" viewBox="0 0 40 40">
                                                <circle class="score-ring-bg" cx="20" cy="20" r="15.9"/>
                                                <circle class="score-ring-progress @GetScoreRingClass(check.VeracityScore.Value)"
                                                        cx="20" cy="20" r="15.9"
                                                        stroke-dasharray="@GetScoreDashArray(check.VeracityScore.Value), 100"/>
                                            </svg>
                                            <span class="score-ring-value @GetScoreTextClass(check.VeracityScore.Value)">@check.VeracityScore</span>
                                        </div>
                                    }
                                    else
                                    {
                                        <span class="text-muted">--</span>
                                    }
                                </td>
                                <td class="py-3 pe-4 text-end">
                                    <div class="d-flex justify-content-end align-items-center gap-2">
                                        @if (check.Status == "Completed")
                                        {
                                            <a href="/report/@check.Id" class="btn btn-sm btn-primary" @onclick:stopPropagation="true">
                                                View Report
                                            </a>
                                        }
                                        else if (check.Status is "Pending" or "Processing")
                                        {
                                            <button class="btn btn-sm btn-outline-secondary" disabled>
                                                Processing...
                                            </button>
                                        }
                                        else
                                        {
                                            <a href="/check" class="btn btn-sm btn-outline-warning" @onclick:stopPropagation="true">
                                                Retry
                                            </a>
                                        }
                                        <button class="btn btn-sm btn-outline-danger" @onclick="() => ConfirmDelete(check)" @onclick:stopPropagation="true" title="Delete">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="bi bi-trash3" viewBox="0 0 16 16">
                                                <path d="M6.5 1h3a.5.5 0 0 1 .5.5v1H6v-1a.5.5 0 0 1 .5-.5ZM11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3A1.5 1.5 0 0 0 5 1.5v1H2.506a.58.58 0 0 0-.01 0H1.5a.5.5 0 0 0 0 1h.538l.853 10.66A2 2 0 0 0 4.885 16h6.23a2 2 0 0 0 1.994-1.84l.853-10.66h.538a.5.5 0 0 0 0-1h-.995a.59.59 0 0 0-.01 0H11Zm1.958 1-.846 10.58a1 1 0 0 1-.997.92h-6.23a1 1 0 0 1-.997-.92L3.042 3.5h9.916Zm-7.487 1a.5.5 0 0 1 .528.47l.5 8.5a.5.5 0 0 1-.998.06L5 5.03a.5.5 0 0 1 .47-.53Zm5.058 0a.5.5 0 0 1 .47.53l-.5 8.5a.5.5 0 1 1-.998-.06l.5-8.5a.5.5 0 0 1 .528-.47ZM8 4.5a.5.5 0 0 1 .5.5v8.5a.5.5 0 0 1-1 0V5a.5.5 0 0 1 .5-.5Z"/>
                                            </svg>
                                        </button>
                                    </div>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    }
</div>

<!-- Delete Confirmation Modal -->
@if (_showDeleteModal)
{
    <div class="modal fade show d-block" tabindex="-1" style="background-color: rgba(0,0,0,0.5);">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header border-0">
                    <h5 class="modal-title fw-bold">
                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-exclamation-triangle text-danger me-2" viewBox="0 0 16 16">
                            <path d="M7.938 2.016A.13.13 0 0 1 8.002 2a.13.13 0 0 1 .063.016.146.146 0 0 1 .054.057l6.857 11.667c.036.06.035.124.002.183a.163.163 0 0 1-.054.06.116.116 0 0 1-.066.017H1.146a.115.115 0 0 1-.066-.017.163.163 0 0 1-.054-.06.176.176 0 0 1 .002-.183L7.884 2.073a.147.147 0 0 1 .054-.057zm1.044-.45a1.13 1.13 0 0 0-1.96 0L.165 13.233c-.457.778.091 1.767.98 1.767h13.713c.889 0 1.438-.99.98-1.767L8.982 1.566z"/>
                            <path d="M7.002 12a1 1 0 1 1 2 0 1 1 0 0 1-2 0zM7.1 5.995a.905.905 0 1 1 1.8 0l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 5.995z"/>
                        </svg>
                        Delete @(_isBulkDelete ? $"{_checksToDelete.Count} CV Checks" : "CV Check")
                    </h5>
                    <button type="button" class="btn-close" @onclick="CancelDelete"></button>
                </div>
                <div class="modal-body">
                    @if (_isBulkDelete)
                    {
                        <p class="mb-2">Are you sure you want to delete <strong>@_checksToDelete.Count</strong> CV checks?</p>
                        <div class="bg-light rounded p-3" style="max-height: 200px; overflow-y: auto;">
                            @foreach (var check in _checksToDelete)
                            {
                                <div class="d-flex justify-content-between align-items-center py-1 @(check != _checksToDelete.Last() ? "border-bottom" : "")">
                                    <span>@Path.GetFileNameWithoutExtension(check.OriginalFileName)</span>
                                    <small class="text-muted">@check.CreatedAt.ToString("dd MMM yyyy")</small>
                                </div>
                            }
                        </div>
                    }
                    else if (_checkToDelete != null)
                    {
                        <p class="mb-2">Are you sure you want to delete this CV check?</p>
                        <div class="bg-light rounded p-3">
                            <strong>@Path.GetFileNameWithoutExtension(_checkToDelete.OriginalFileName)</strong>
                            <br />
                            <small class="text-muted">Uploaded @_checkToDelete.CreatedAt.ToString("dd MMM yyyy 'at' HH:mm")</small>
                        </div>
                    }
                    <p class="text-danger small mt-3 mb-0">
                        <strong>This action cannot be undone.</strong> @(_isBulkDelete ? "All selected CVs and their" : "The CV and all") verification data will be permanently removed.
                    </p>
                </div>
                <div class="modal-footer border-0">
                    <button type="button" class="btn btn-outline-secondary" @onclick="CancelDelete">Cancel</button>
                    <button type="button" class="btn btn-danger" @onclick="ExecuteDelete" disabled="@_isDeleting">
                        @if (_isDeleting)
                        {
                            <span class="spinner-border spinner-border-sm me-1" role="status"></span>
                            <span>Deleting...</span>
                        }
                        else
                        {
                            <span>Delete @(_isBulkDelete ? $"{_checksToDelete.Count} Checks" : "")</span>
                        }
                    </button>
                </div>
            </div>
        </div>
    </div>
}

<style>
    .cursor-pointer {
        cursor: pointer;
        user-select: none;
    }

    .cursor-pointer:hover {
        background-color: rgba(59, 111, 212, 0.04);
    }

    .empty-state-icon {
        width: 100px;
        height: 100px;
        background: linear-gradient(135deg, #e8f1fa 0%, #d4e4f4 100%);
        border-radius: 24px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto;
        color: var(--truecv-primary);
    }

    .file-icon-wrapper {
        width: 44px;
        height: 44px;
        background: linear-gradient(135deg, #e8f1fa 0%, #d4e4f4 100%);
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .score-ring-container {
        position: relative;
        width: 52px;
        height: 52px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .score-ring {
        width: 100%;
        height: 100%;
    }

    .score-ring-bg {
        fill: none;
        stroke: var(--truecv-gray-200);
        stroke-width: 3;
    }

    .score-ring-progress {
        fill: none;
        stroke-width: 3;
        stroke-linecap: round;
        transform-origin: center;
        transform: rotate(-90deg);
    }

    .score-ring-progress.high { stroke: var(--truecv-verified); }
    .score-ring-progress.medium { stroke: var(--truecv-warning); }
    .score-ring-progress.low { stroke: var(--truecv-danger); }

    .score-ring-value {
        position: absolute;
        font-size: 0.875rem;
        font-weight: 700;
        font-family: 'JetBrains Mono', monospace;
    }

    .text-verified { color: var(--truecv-verified); }
    .text-warning-dark { color: var(--truecv-warning-dark); }
    .text-danger { color: var(--truecv-danger); }

    @@media (max-width: 768px) {
        .d-flex.justify-content-between.align-items-center.mb-4 {
            flex-direction: column;
            align-items: stretch !important;
            gap: 1rem;
        }

        .d-flex.gap-2 {
            width: 100%;
            justify-content: stretch;
        }

        .d-flex.gap-2 .btn {
            flex: 1;
        }

        .row.mb-4 .col-md-4 {
            margin-bottom: 0.75rem;
        }

        .score-badge {
            width: 40px;
            height: 40px;
            font-size: 0.875rem;
        }
    }
</style>

@code {
    private List<CVCheckDto> _checks = [];
    private bool _isLoading = true;
    private bool _isExporting;
    private bool _isDeleting;
    private string? _errorMessage;
    private Guid _userId;
    private System.Threading.Timer? _pollingTimer;
    private volatile bool _isPolling;
    private volatile bool _disposed;
    private volatile bool _isOperationInProgress;

    // Delete confirmation modal state
    private bool _showDeleteModal;
    private CVCheckDto? _checkToDelete;
    private List<CVCheckDto> _checksToDelete = [];
    private bool _isBulkDelete;

    // Selection state
    private HashSet<Guid> _selectedIds = [];

    protected override async Task OnInitializedAsync()
    {
        await LoadChecks();
        StartPollingIfNeeded();
    }

    private void StartPollingIfNeeded()
    {
        if (HasProcessingChecks() && !_isPolling && !_disposed)
        {
            _isPolling = true;
            _pollingTimer = new System.Threading.Timer(async _ =>
            {
                if (_disposed) return;

                try
                {
                    await InvokeAsync(async () =>
                    {
                        if (_disposed) return;

                        await LoadChecks();
                        if (!HasProcessingChecks())
                        {
                            StopPolling();
                        }
                        StateHasChanged();
                    });
                }
                catch (ObjectDisposedException)
                {
                    // Component was disposed, ignore
                }
            }, null, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(3));
        }
    }

    private bool HasProcessingChecks()
    {
        foreach (var c in _checks)
        {
            if (c.Status == "Processing" || c.Status == "Pending") return true;
        }
        return false;
    }

    private void StopPolling()
    {
        _isPolling = false;
        _pollingTimer?.Dispose();
        _pollingTimer = null;
    }

    public void Dispose()
    {
        _disposed = true;
        StopPolling();
    }

    private async Task LoadChecks()
    {
        if (_isOperationInProgress) return;

        _isOperationInProgress = true;
        _isLoading = true;
        _errorMessage = null;

        try
        {
            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
            var userIdClaim = authState.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (string.IsNullOrEmpty(userIdClaim) || !Guid.TryParse(userIdClaim, out _userId))
            {
                _errorMessage = "Unable to identify user. Please log in again.";
                return;
            }

            _checks = await CVCheckService.GetUserChecksAsync(_userId) ?? [];
        }
        catch (Exception ex)
        {
            Logger.LogError(ex, "Error loading CV checks");
            _errorMessage = "An error occurred while loading checks. Please try again.";
        }
        finally
        {
            _isLoading = false;
            _isOperationInProgress = false;
        }
    }

    private void ViewReport(CVCheckDto check)
    {
        if (check.Status == "Completed")
        {
            NavigationManager.NavigateTo($"/report/{check.Id}");
        }
    }

    private static string GetScoreBadgeClass(int score)
    {
        return score switch
        {
            > 70 => "bg-success",
            >= 50 => "bg-warning text-dark",
            _ => "bg-danger"
        };
    }

    private static string GetScoreBadgeColorClass(int score)
    {
        return score switch
        {
            > 70 => "score-high",
            >= 50 => "score-medium",
            _ => "score-low"
        };
    }

    private static string GetScoreRingClass(int score) => score > 70 ? "high" : score >= 50 ? "medium" : "low";
    private static string GetScoreTextClass(int score) => score > 70 ? "text-verified" : score >= 50 ? "text-warning-dark" : "text-danger";
    private static string GetScoreDashArray(int score) => score.ToString();

    private async Task ExportToPdf()
    {
        if (_isExporting) return;

        _isExporting = true;
        StateHasChanged();

        try
        {
            var reportDataList = new List<RealCV.Web.Services.PdfReportData>();
            foreach (var check in _checks)
            {
                if (check.Status != "Completed") continue;

                var report = await CVCheckService.GetReportAsync(check.Id, _userId);
                if (report is null) continue;

                int verifiedCount = 0;
                int unverifiedCount = 0;
                foreach (var v in report.EmploymentVerifications)
                {
                    if (v.IsVerified) verifiedCount++;
                    else unverifiedCount++;
                }

                int criticalFlags = 0;
                int warningFlags = 0;
                foreach (var f in report.Flags)
                {
                    if (f.Severity == "Critical") criticalFlags++;
                    else if (f.Severity == "Warning") warningFlags++;
                }

                reportDataList.Add(new RealCV.Web.Services.PdfReportData
                {
                    CandidateName = Path.GetFileNameWithoutExtension(check.OriginalFileName) ?? "Unknown",
                    UploadDate = check.CreatedAt,
                    Score = report.OverallScore,
                    ScoreLabel = report.ScoreLabel,
                    VerifiedEmployers = verifiedCount,
                    UnverifiedEmployers = unverifiedCount,
                    GapMonths = report.TimelineAnalysis.TotalGapMonths,
                    OverlapMonths = report.TimelineAnalysis.TotalOverlapMonths,
                    CriticalFlags = criticalFlags,
                    WarningFlags = warningFlags
                });
            }

            var pdfBytes = PdfReportService.GenerateReport(reportDataList);
            var base64 = Convert.ToBase64String(pdfBytes);
            var fileName = "RealCV_Report_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".pdf";
            await JSRuntime.InvokeVoidAsync("downloadFile", fileName, base64, "application/pdf");

            await AuditService.LogAsync(_userId, AuditActions.ReportExported, null, null, $"Exported {reportDataList.Count} reports to PDF");
        }
        catch (Exception ex)
        {
            Logger.LogError(ex, "Error exporting PDF");
        }
        finally
        {
            _isExporting = false;
            StateHasChanged();
        }
    }

    private bool HasCompletedChecks()
    {
        foreach (var c in _checks)
        {
            if (c.Status == "Completed") return true;
        }
        return false;
    }

    // Selection methods
    private void ToggleSelection(Guid id)
    {
        if (_selectedIds.Contains(id))
            _selectedIds.Remove(id);
        else
            _selectedIds.Add(id);
    }

    private void ToggleSelectAll()
    {
        if (IsAllSelected())
            _selectedIds.Clear();
        else
            _selectedIds = _checks.Select(c => c.Id).ToHashSet();
    }

    private bool IsAllSelected() => _checks.Count > 0 && _selectedIds.Count == _checks.Count;

    // Single delete
    private void ConfirmDelete(CVCheckDto check)
    {
        _checkToDelete = check;
        _checksToDelete = [];
        _isBulkDelete = false;
        _showDeleteModal = true;
    }

    // Bulk delete
    private void ConfirmDeleteSelected()
    {
        _checksToDelete = _checks.Where(c => _selectedIds.Contains(c.Id)).ToList();
        _checkToDelete = null;
        _isBulkDelete = true;
        _showDeleteModal = true;
    }

    private void CancelDelete()
    {
        _showDeleteModal = false;
        _checkToDelete = null;
        _checksToDelete = [];
        _isBulkDelete = false;
    }

    private async Task ExecuteDelete()
    {
        if (_isDeleting) return;

        _isDeleting = true;
        try
        {
            if (_isBulkDelete)
            {
                var failedCount = 0;
                foreach (var check in _checksToDelete)
                {
                    var success = await CVCheckService.DeleteCheckAsync(check.Id, _userId);
                    if (success)
                    {
                        _checks.RemoveAll(c => c.Id == check.Id);
                        _selectedIds.Remove(check.Id);
                    }
                    else
                    {
                        failedCount++;
                    }
                }

                if (failedCount > 0)
                {
                    _errorMessage = $"Failed to delete {failedCount} CV check(s). Please try again.";
                }
            }
            else if (_checkToDelete != null)
            {
                var success = await CVCheckService.DeleteCheckAsync(_checkToDelete.Id, _userId);
                if (success)
                {
                    _checks.RemoveAll(c => c.Id == _checkToDelete.Id);
                    _selectedIds.Remove(_checkToDelete.Id);
                }
                else
                {
                    _errorMessage = "Failed to delete the CV check. Please try again.";
                }
            }
        }
        catch (Exception ex)
        {
            Logger.LogError(ex, "Error deleting CV check(s)");
            _errorMessage = "Failed to delete CV check(s). Please try again.";
        }
        finally
        {
            _isDeleting = false;
            _showDeleteModal = false;
            _checkToDelete = null;
            _checksToDelete = [];
            _isBulkDelete = false;
        }
    }
}
