<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {{- $cveIDs := "" -}}
    {{- $seenCVEs := "" -}}
    {{- range .IndexedResults -}}
        {{- if .Result.CVEMatches -}}
            {{- range $cve_id, $cve_data := .Result.CVEMatches -}}
                {{- if eq $cve_data.Result "Yes" -}}
                    {{- if not (contains $seenCVEs $cve_id) -}}
                        {{- if $cveIDs -}}
                            {{- $cveIDs = printf "%s, %s" $cveIDs $cve_id -}}
                        {{- else -}}
                            {{- $cveIDs = $cve_id -}}
                        {{- end -}}
                        {{- if $seenCVEs -}}
                            {{- $seenCVEs = printf "%s,%s" $seenCVEs $cve_id -}}
                        {{- else -}}
                            {{- $seenCVEs = $cve_id -}}
                        {{- end -}}
                    {{- end -}}
                {{- end -}}
            {{- end -}}
        {{- end -}}
    {{- end -}}
    {{- if not $cveIDs -}}
        {{- if (index .Analysis.Meta.Params "cve_ids") -}}
            {{- $cveIDs = (index .Analysis.Meta.Params "cve_ids") -}}
        {{- end -}}
    {{- end -}}
    {{- $productName := "" -}}
    {{- $oldVersion := "" -}}
    {{- $newVersion := "" -}}
    {{- if (index .Analysis.Meta.Params "product") -}}
        {{- $productName = (index .Analysis.Meta.Params "product") -}}
        {{- $oldVersion = (index .Analysis.Meta.Params "old_version") -}}
        {{- $newVersion = (index .Analysis.Meta.Params "new_version") -}}
    {{- else if (index .Analysis.Meta.Params "repo_name") -}}
        {{- $productName = (index .Analysis.Meta.Params "repo_name") -}}
        {{- $oldVersion = (index .Analysis.Meta.Params "old_version") -}}
        {{- $newVersion = (index .Analysis.Meta.Params "new_version") -}}
    {{- else if (index .Analysis.Meta.Params "old_folder") -}}
        {{- $productName = "Folder Analysis" -}}
        {{- $oldVersion = (index .Analysis.Meta.Params "old_folder") -}}
        {{- $newVersion = (index .Analysis.Meta.Params "new_folder") -}}
    {{- end -}}
    {{- if $productName -}}
        {{- if $cveIDs -}}
    <title>PatchLeaks - Analysis Report {{$productName}} {{$oldVersion}} → {{$newVersion}} - {{$cveIDs}}</title>
        {{- else -}}
    <title>PatchLeaks - Analysis Report {{$productName}} {{$oldVersion}} → {{$newVersion}}</title>
        {{- end -}}
    {{- else if $cveIDs -}}
    <title>PatchLeaks - Analysis Report for {{$cveIDs}}</title>
    {{- else -}}
    <title>PatchLeaks - Analysis Report</title>
    {{- end -}}
    
    
    <meta name="description" content="{{if $cveIDs}}Security patch analysis report for {{$cveIDs}} - vulnerability assessment, exploit detection, and patchdiff analysis{{else}}Security patch analysis report - vulnerability assessment, exploit detection, and patchdiff analysis{{end}}">
    <meta name="robots" content="index, follow">
    <meta name="author" content="PatchLeaks Security Analysis">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    
    <meta property="og:title" content="{{if $cveIDs}}PatchLeaks - Analysis Report for {{$cveIDs}}{{else}}PatchLeaks - Security Patch Analysis Report{{end}}">
    <meta property="og:description" content="{{if $cveIDs}}Security patch analysis report for {{$cveIDs}} - vulnerability assessment, exploit detection, and patchdiff analysis{{else}}Security patch analysis report - vulnerability assessment, exploit detection, and patchdiff analysis{{end}}">
    <meta property="og:type" content="website">
    <meta property="og:site_name" content="PatchLeaks">
    
    
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:title" content="{{if $cveIDs}}PatchLeaks - Analysis Report for {{$cveIDs}}{{else}}PatchLeaks - Security Patch Analysis Report{{end}}">
    <meta name="twitter:description" content="{{if $cveIDs}}Security patch analysis report for {{$cveIDs}} - vulnerability assessment, exploit detection, and patchdiff analysis{{else}}Security patch analysis report - vulnerability assessment, exploit detection, and patchdiff analysis{{end}}">
    
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-blue: #4169E1;
            --accent-color: #6c5ce7;
            --code-bg: #0a0e14;
            --section-spacing: 100px;
            --dark-bg: #0a0b10;
            --card-bg: #121520;
            --success-color: #00b894;
            --danger-color: #e74c3c;
            --warning-color: #f39c12;
            --info-color: #3498db;
        }

        body {
            background: linear-gradient(135deg, var(--dark-bg) 0%, #16171d 100%);
            color: #f8f9fa;
            font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            overflow-x: hidden;
        }

        .navbar {
            background: rgba(10, 11, 16, 0.8);
            backdrop-filter: blur(10px);
            border-bottom: 1px solid rgba(255,255,255,0.05);
            padding: 1rem 2rem;
            position: fixed;
            width: 100%;
            top: 0;
            z-index: 1000;
            transition: all 0.3s ease;
        }

        .navbar.scrolled {
            padding: 0.5rem 2rem;
        }

        .nav-link {
            color: rgba(255,255,255,0.7);
            margin: 0 0.5rem;
            position: relative;
            transition: all 0.3s ease;
        }

        .nav-link:hover {
            color: #fff;
        }

        .nav-link::after {
            content: '';
            position: absolute;
            width: 0;
            height: 2px;
            bottom: -4px;
            left: 0;
            background: linear-gradient(90deg, var(--primary-blue), var(--accent-color));
            transition: width 0.3s ease;
        }

        .nav-link:hover::after {
            width: 100%;
        }

        .main-container {
            padding-top: 6rem;
            padding-bottom: 3rem;
        }

        .card {
            background: var(--card-bg);
            border: 1px solid rgba(255,255,255,0.08);
            border-radius: 12px;
            box-shadow: 0 15px 30px -10px rgba(0,0,0,0.5);
            margin-bottom: 1.5rem;
            transition: all 0.3s ease;
        }

        .card:hover {
            box-shadow: 0 20px 40px -15px rgba(0,0,0,0.6);
            transform: translateY(-5px);
            border-color: rgba(65,105,225,0.2);
        }

        .card-header {
            background: rgba(255,255,255,0.03);
            border-bottom: 1px solid rgba(255,255,255,0.05);
            padding: 1.2rem 1.5rem;
        }

        .code-block {
            background: var(--code-bg);
            border: 1px solid rgba(255,255,255,0.05);
            border-radius: 8px;
            padding: 1rem;
            font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
            font-size: 0.875rem;
            overflow: auto;
            white-space: pre-wrap;
            color: #f8f9fa;
        }

        .diff-add {
            color: #25c97c;
            background-color: rgba(37, 201, 124, 0.1);
            display: block;
            padding: 2px 4px;
        }

        .diff-remove {
            color: #ff5757;
            background-color: rgba(255, 87, 87, 0.1);
            display: block;
            padding: 2px 4px;
        }

        .context-line {
            color: #a2abb8;
            display: block;
            padding: 2px 4px;
        }

        .ellipsis {
            color: #8a93a2;
            font-style: italic;
            padding: 2px 4px;
            display: block;
        }

        .collapsible-header {
            cursor: pointer;
            transition: background-color 0.2s;
        }

        .collapsible-header:hover {
            background-color: rgba(255,255,255,0.05);
        }

        .chevron {
            transition: transform 0.2s;
        }

        .collapsed .chevron {
            transform: rotate(180deg);
        }

        .ai-response {
            background: rgba(243, 156, 18, 0.1);
            border-left: 4px solid var(--warning-color);
            margin-top: 1rem;
            padding: 1rem;
            border-radius: 4px;
        }

        .vuln-status-badge {
            font-size: 0.9rem;
            padding: 0.4rem 0.8rem;
            border-radius: 20px;
        }

        .cve-result {
            background-color: rgba(108, 92, 231, 0.1);
            border-left: 4px solid var(--accent-color);
            padding: 1rem;
            margin-top: 1rem;
        }

        details summary {
            cursor: pointer;
            color: #a2abb8;
            transition: color 0.3s;
        }

        details summary:hover {
            color: #fff;
        }

        details[open] summary {
            margin-bottom: 0.5rem;
            color: var(--primary-blue);
        }

        .form-control {
            background: rgba(255,255,255,0.05);
            border: 1px solid rgba(255,255,255,0.1);
            color: #f8f9fa;
            transition: all 0.3s ease;
        }

        .form-control:focus {
            background: rgba(255,255,255,0.08);
            border-color: var(--primary-blue);
            box-shadow: 0 0 0 0.25rem rgba(65,105,225,0.2);
            color: #fff;
        }

        .form-control:read-only {
            background: rgba(255,255,255,0.03);
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--primary-blue), var(--accent-color));
            border: none;
            box-shadow: 0 4px 15px rgba(65,105,225,0.3);
            transition: all 0.3s ease;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(65,105,225,0.4);
        }

        .btn-outline-secondary {
            border: 2px solid rgba(255,255,255,0.2);
            color: rgba(255,255,255,0.7);
            background: transparent;
            transition: all 0.3s ease;
        }

        .btn-outline-secondary:hover {
            border-color: rgba(255,255,255,0.3);
            color: #fff;
            transform: translateY(-2px);
        }

        .btn-outline-secondary.active {
            background: rgba(255,255,255,0.1);
            color: #fff;
            border-color: rgba(255,255,255,0.3);
        }

        .btn-outline-info {
            border: 2px solid var(--info-color);
            color: var(--info-color);
            background: transparent;
            transition: all 0.3s ease;
        }
        
        .btn-outline-info:hover, 
        .btn-outline-info.active {
            background: rgba(52, 152, 219, 0.1);
            color: var(--info-color);
            transform: translateY(-2px);
        }

        .btn-outline-danger {
            border: 2px solid var(--danger-color);
            color: var(--danger-color);
            background: transparent;
            transition: all 0.3s ease;
        }
        
        .btn-outline-danger:hover,
        .btn-outline-danger.active {
            background: rgba(231, 76, 60, 0.1);
            color: var(--danger-color);
            transform: translateY(-2px);
        }

        .btn-warning {
            background: linear-gradient(135deg, var(--warning-color), #e67e22);
            border: none;
            color: #fff;
            box-shadow: 0 4px 15px rgba(243, 156, 18, 0.3);
            transition: all 0.3s ease;
        }

        .btn-warning:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(243, 156, 18, 0.4);
            color: #fff;
        }

        .alert {
            border: none;
            background: rgba(255,255,255,0.05);
            border-left: 4px solid transparent;
        }

        .alert-info {
            border-left-color: var(--info-color);
            background: rgba(52, 152, 219, 0.1);
        }

        .section-bg-accent {
            position: absolute;
            width: 30vw;
            height: 30vw;
            border-radius: 50%;
            background: linear-gradient(45deg, var(--primary-blue), var(--accent-color));
            filter: blur(120px);
            opacity: 0.1;
            z-index: -1;
        }

        .section-bg-accent.left {
            left: -15vw;
            top: 5%;
        }

        .section-bg-accent.right {
            right: -15vw;
            bottom: 5%;
        }

        ::-webkit-scrollbar {
            width: 8px;
            height: 8px;
        }

        ::-webkit-scrollbar-track {
            background: rgba(255,255,255,0.05);
        }

        ::-webkit-scrollbar-thumb {
            background: rgba(255,255,255,0.2);
            border-radius: 4px;
        }

        ::-webkit-scrollbar-thumb:hover {
            background: rgba(65,105,225,0.5);
        }

        .filter-btn.active {
            background: rgba(255,255,255,0.1);
            color: #fff;
        }

        .input-group-text {
            background: rgba(255,255,255,0.03);
            border: 1px solid rgba(255,255,255,0.1);
            color: var(--primary-blue);
        }

        .fw-bold {
            color: #fff;
        }

        .text-muted {
            color: rgba(255, 255, 255, 0.6) !important;
        }

        pre {
            color: #f8f9fa;
        }

        h6 {
            color: #fff;
        }
        
        footer {
            background: linear-gradient(180deg, rgba(18, 21, 32, 0) 0%, rgba(18, 21, 32, 1) 100%);
            position: relative;
            overflow: hidden;
        }

        footer::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 1px;
            background: linear-gradient(90deg, rgba(255,255,255,0), rgba(255,255,255,0.1) 50%, rgba(255,255,255,0));
        }

        .footer-heading {
            color: white;
            font-size: 1rem;
            font-weight: 600;
            margin-bottom: 1.5rem;
            position: relative;
        }

        .footer-links {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .footer-links li {
            margin-bottom: 0.75rem;
        }

        .footer-links a {
            color: rgba(255, 255, 255, 0.6);
            text-decoration: none;
            transition: color 0.2s ease;
            font-size: 0.9rem;
            display: inline-block;
        }

        .footer-links a:hover {
            color: var(--primary-blue);
        }

        .social-link {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 38px;
            height: 38px;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.05);
            color: rgba(255, 255, 255, 0.7);
            transition: all 0.3s ease;
        }

        .social-link:hover {
            background: var(--primary-blue);
            color: white;
            transform: translateY(-3px);
        }

        .footer-newsletter {
            background: rgba(65, 105, 225, 0.05);
            border-radius: 12px;
            border: 1px solid rgba(65, 105, 225, 0.1);
        }

        .footer-bottom {
            font-size: 0.85rem;
        }

        @media (max-width: 767.98px) {
            .footer-heading {
                margin-bottom: 1rem;
                font-size: 0.95rem;
            }
            
            .footer-links li {
                margin-bottom: 0.5rem;
            }
            
            .footer-links a {
                font-size: 0.85rem;
            }
            
            .footer-newsletter {
                padding: 1.5rem !important;
            }
        }
        
        .nav-link.active {
            color: var(--primary-blue) !important;
            font-weight: 500;
        }

        .nav-link.active::after {
            width: 100%;
            background: var(--primary-blue);
        }
        
        .text-muted {
            color: rgba(255, 255, 255, 0.6) !important;
        }

        .fw-bold, .report-title, h1, h2, h3, h4, h5, h6 {
            color: #fff;
        }

        .report-card {
            border-radius: 12px;
            overflow: hidden;
            transition: all 0.3s ease;
            background: rgba(255,255,255,0.03);
            border: 1px solid rgba(255,255,255,0.05);
        }

        .report-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.2);
            background: rgba(255,255,255,0.05);
        }

        .report-header {
            padding: 1.2rem;
            border-bottom: 1px solid rgba(255,255,255,0.05);
            background: rgba(255,255,255,0.02);
        }

        .report-body {
            padding: 1.2rem;
        }

        .report-footer {
            padding: 1rem;
            border-top: 1px solid rgba(255,255,255,0.05);
            background: rgba(255,255,255,0.02);
        }

        .report-title {
            color: #fff;
            font-weight: 600;
            margin-bottom: 0.5rem;
            font-size: 1.1rem;
        }

        .report-meta {
            color: rgba(255,255,255,0.6);
            font-size: 0.85rem;
        }

        .report-stats {
            display: flex;
            gap: 1rem;
            margin-top: 1rem;
        }

        .stat-item {
            background: rgba(255,255,255,0.05);
            padding: 0.5rem 1rem;
            border-radius: 8px;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .stat-item i {
            color: var(--primary-blue);
        }

        .badge-ai {
            background: linear-gradient(135deg, var(--primary-blue), var(--accent-color));
            color: white;
            padding: 0.3rem 0.6rem;
            border-radius: 6px;
            font-size: 0.75rem;
            font-weight: 500;
        }

        .badge-folder {
            background: linear-gradient(135deg, var(--info-color), #45aaf2);
            color: white;
            padding: 0.3rem 0.6rem;
            border-radius: 6px;
            font-size: 0.75rem;
            font-weight: 500;
        }

        .badge-product {
            background: linear-gradient(135deg, var(--success-color), #20bf6b);
            color: white;
            padding: 0.3rem 0.6rem;
            border-radius: 6px;
            font-size: 0.75rem;
            font-weight: 500;
        }

        .search-container {
            position: relative;
            margin-bottom: 2rem;
        }

        .search-container i {
            position: absolute;
            left: 1rem;
            top: 50%;
            transform: translateY(-50%);
            color: rgba(255,255,255,0.5);
        }

        .search-input {
            padding-left: 2.5rem;
            height: 3rem;
            font-size: 1rem;
            border-radius: 8px;
        }

        .filter-btn {
            border: 2px solid rgba(255,255,255,0.2);
            color: rgba(255,255,255,0.7);
            background: transparent;
            transition: all 0.3s ease;
            border-radius: 8px;
            padding: 0.5rem 1rem;
        }

        .filter-btn:hover, .filter-btn.active {
            background: rgba(228, 206, 233, 0.1);
            color: #fff;
            border-color: rgba(255,255,255,0.3);
        }

        .cve-writeup-section {
            border-left: 4px solid var(--danger-color);
            padding-left: 1.5rem;
            margin-bottom: 2rem;
            transition: all 0.3s ease;
        }

        .cve-writeup-section:hover {
            border-left-color: #ff6b6b;
        }

        .writeup-content {
            background: linear-gradient(135deg, rgba(231, 76, 60, 0.08) 0%, rgba(192, 57, 43, 0.05) 100%);
            border: 1px solid rgba(231, 76, 60, 0.2);
            border-radius: 12px;
            margin-top: 1rem;
            box-shadow: 0 4px 15px rgba(231, 76, 60, 0.1);
        }

        .writeup-header {
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            padding-bottom: 0.5rem;
        }

        .writeup-text {
            color: #f8f9fa;
            font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            line-height: 1.8;
            font-size: 0.95rem;
            background: transparent;
            border: none;
        }

        .writeup-text h2 {
            color: #fff;
            font-size: 1.3rem;
            margin-top: 1.5rem;
            margin-bottom: 1rem;
            padding-bottom: 0.5rem;
            border-bottom: 2px solid rgba(231, 76, 60, 0.3);
        }

        .writeup-text h3 {
            color: #f8f9fa;
            font-size: 1.1rem;
            margin-top: 1rem;
            margin-bottom: 0.5rem;
        }

        .writeup-text ul, .writeup-text ol {
            margin-left: 1.5rem;
            margin-bottom: 1rem;
        }

        .writeup-text li {
            margin-bottom: 0.5rem;
        }

        .writeup-text code {
            background: rgba(0, 0, 0, 0.3);
            padding: 0.2rem 0.4rem;
            border-radius: 4px;
            font-family: 'Courier New', monospace;
            color: #ffa07a;
        }

        .writeup-text pre {
            background: rgba(0, 0, 0, 0.4);
            padding: 1rem;
            border-radius: 8px;
            overflow-x: auto;
            margin-bottom: 1rem;
        }

        .writeup-text pre code {
            background: transparent;
            padding: 0;
            color: #f8f9fa;
        }

        .writeup-text strong {
            color: #fff;
            font-weight: 600;
        }

        .writeup-text p {
            margin-bottom: 0.75rem;
        }

        .writeup-text em {
            font-style: italic;
            color: #e0e0e0;
        }

        .writeup-text blockquote {
            border-left: 4px solid rgba(231, 76, 60, 0.5);
            padding-left: 1rem;
            margin-left: 0;
            margin-bottom: 1rem;
            color: rgba(255, 255, 255, 0.8);
        }

        .badge-glow {
            box-shadow: 0 0 20px rgba(231, 76, 60, 0.5);
            animation: pulse-glow 2s infinite;
        }

        @keyframes pulse-glow {
            0%, 100% {
                box-shadow: 0 0 20px rgba(231, 76, 60, 0.5);
            }
            50% {
                box-shadow: 0 0 30px rgba(231, 76, 60, 0.8);
            }
        }

        .cve-writeup-section .btn-danger {
            background: linear-gradient(135deg, var(--danger-color), #c0392b);
            border: none;
            box-shadow: 0 4px 15px rgba(231, 76, 60, 0.3);
            transition: all 0.3s ease;
        }

        .cve-writeup-section .btn-danger:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(231, 76, 60, 0.4);
        }

        .cve-writeup-section .btn-danger .fa-chevron-down {
            transition: transform 0.3s ease;
        }

        .cve-writeup-section .btn-danger[aria-expanded="true"] .fa-chevron-down {
            transform: rotate(180deg);
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg">
        <div class="container">
            <a class="navbar-brand d-flex align-items-center" href="/">
                <i class="fas fa-shield-alt me-2 text-primary"></i>
                <span class="fw-bold text-white">PatchLeaks</span>
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <i class="fas fa-bars text-light"></i>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item"><a class="nav-link" href="/dashboard">Dashboard</a></li>
                    <li class="nav-item"><a class="nav-link" href="/folder">Folder Analysis</a></li>
                    <li class="nav-item"><a class="nav-link" href="/products">Product Analysis</a></li>
                    <li class="nav-item"><a class="nav-link" href="/library">Library</a></li>
                    <li class="nav-item"><a class="nav-link" href="/ai-settings">AI Analysis</a></li>
                    <li class="nav-item"><a class="nav-link active" href="/reports">Reports</a></li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="section-bg-accent left"></div>
    <div class="section-bg-accent right"></div>

    <div class="container main-container pb-5">
        {{if eq .Status "running"}}
        <div class="alert alert-warning d-flex align-items-center mb-4" role="alert">
            <i class="fas fa-spinner fa-spin me-2"></i>
            <div>
                <strong>Analysis is still running...</strong> Please check back later. You can safely close this page and return at any time.
            </div>
        </div>
        <script>
        
        setInterval(function() {
            fetch(window.location.href, {headers: {'X-Requested-With': 'XMLHttpRequest'}})
                .then(resp => resp.text())
                .then(html => {
                    if (!html.includes('Analysis is still running')) {
                        window.location.reload();
                    }
                });
        }, 5000);
        </script>
        {{end}}
        <div class="row mb-4">
            <div class="col-lg-8 col-md-10 mx-auto">
                {{- $cveIDs := "" -}}
                {{- $seenCVEs := "" -}}
                {{- range .IndexedResults -}}
                    {{- if .Result.CVEMatches -}}
                        {{- range $cve_id, $cve_data := .Result.CVEMatches -}}
                            {{- if eq $cve_data.Result "Yes" -}}
                                {{- if not (contains $seenCVEs $cve_id) -}}
                                    {{- if $cveIDs -}}
                                        {{- $cveIDs = printf "%s, %s" $cveIDs $cve_id -}}
                                    {{- else -}}
                                        {{- $cveIDs = $cve_id -}}
                                    {{- end -}}
                                    {{- if $seenCVEs -}}
                                        {{- $seenCVEs = printf "%s,%s" $seenCVEs $cve_id -}}
                                    {{- else -}}
                                        {{- $seenCVEs = $cve_id -}}
                                    {{- end -}}
                                {{- end -}}
                            {{- end -}}
                        {{- end -}}
                    {{- end -}}
                {{- end -}}
                {{- if not $cveIDs -}}
                    {{- if (index .Analysis.Meta.Params "cve_ids") -}}
                        {{- $cveIDs = (index .Analysis.Meta.Params "cve_ids") -}}
                    {{- end -}}
                {{- end -}}
                {{- $productName := "" -}}
                {{- $oldVersion := "" -}}
                {{- $newVersion := "" -}}
                {{- if (index .Analysis.Meta.Params "product") -}}
                    {{- $productName = (index .Analysis.Meta.Params "product") -}}
                    {{- $oldVersion = (index .Analysis.Meta.Params "old_version") -}}
                    {{- $newVersion = (index .Analysis.Meta.Params "new_version") -}}
                {{- else if (index .Analysis.Meta.Params "repo_name") -}}
                    {{- $productName = (index .Analysis.Meta.Params "repo_name") -}}
                    {{- $oldVersion = (index .Analysis.Meta.Params "old_version") -}}
                    {{- $newVersion = (index .Analysis.Meta.Params "new_version") -}}
                {{- else if (index .Analysis.Meta.Params "old_folder") -}}
                    {{- $productName = "Folder Analysis" -}}
                    {{- $oldVersion = (index .Analysis.Meta.Params "old_folder") -}}
                    {{- $newVersion = (index .Analysis.Meta.Params "new_folder") -}}
                {{- end -}}
                {{- if $productName -}}
                    {{- if $cveIDs -}}
                <h2 class="display-5 fw-bold mb-3">Analysis Report {{$productName}} {{$oldVersion}} → {{$newVersion}} - {{$cveIDs}}</h2>
                    {{- else -}}
                <h2 class="display-5 fw-bold mb-3">Analysis Report {{$productName}} {{$oldVersion}} → {{$newVersion}}</h2>
                    {{- end -}}
                {{- else if $cveIDs -}}
                <h2 class="display-5 fw-bold mb-3">Analysis Report for {{$cveIDs}}</h2>
                {{- else -}}
                <h2 class="display-5 fw-bold mb-3">Analysis Report</h2>
                {{- end -}}
                <p class="lead text-muted">Shared security patch analysis results</p>
                {{if .Analysis.Meta.AIEnabled}}
                <span class="badge bg-info text-dark mb-2"><i class="fas fa-robot me-1"></i>AI Used: {{.Analysis.Meta.AIService}}{{if .Analysis.Meta.AIModel}} {{.Analysis.Meta.AIModel}}{{end}}</span>
                {{end}}
            </div>
        </div>

        <div class="card shadow">
            <div class="card-body p-4">
                <div class="alert alert-info mb-4">
                    <div class="d-flex align-items-center mb-2">
                        <i class="fas fa-link me-2 text-info"></i>
                        <h5 class="mb-0">Share this analysis</h5>
                    </div>
                    <div class="input-group mt-2">
                        <span class="input-group-text">
                            <i class="fas fa-share-alt text-primary"></i>
                        </span>
                        <input type="text" class="form-control" 
                               value="{{.AnalysisURL}}" 
                               id="analysisUrl" readonly>
                        <button class="btn btn-outline-primary" onclick="copyLink()">
                            <i class="fas fa-copy me-2"></i>Copy
                        </button>
                    </div>
                </div>

                {{if .Analysis.CVEWriteups}}
                <div class="card mb-4 shadow">
                    <div class="card-header d-flex align-items-center">
                        <i class="fas fa-shield-virus text-danger me-2 fs-5"></i>
                        <h5 class="mb-0">CVE Security Analysis & Writeups</h5>
                    </div>
                    <div class="card-body">
                        <p class="text-muted mb-4">
                            <i class="fas fa-info-circle me-1"></i>
                            Comprehensive security analysis generated by AI for each confirmed CVE match. 
                            Click on a CVE to view the detailed writeup including vulnerability background, technical details, patch analysis, and PoC guide.
                        </p>
                        {{range $cveID, $writeup := .Analysis.CVEWriteups}}
                        <div class="cve-writeup-section mb-4">
                            <div class="d-flex align-items-center justify-content-between mb-2">
                                <div class="d-flex align-items-center">
                                    <span class="badge bg-danger badge-glow me-3 px-3 py-2">
                                        <i class="fas fa-bug me-1"></i>{{$cveID}}
                                    </span>
                                    <button class="btn btn-sm btn-danger" type="button" data-bs-toggle="collapse" 
                                            data-bs-target="#writeup-{{$cveID}}" aria-expanded="false">
                                        <i class="fas fa-file-alt me-2"></i>View Security Analysis
                                        <i class="fas fa-chevron-down ms-2"></i>
                                    </button>
                                </div>
                            </div>
                            <div class="collapse" id="writeup-{{$cveID}}">
                                <div class="writeup-content p-4 mt-3">
                                    <div class="writeup-header mb-3">
                                        <span class="badge bg-info text-dark">
                                            <i class="fas fa-robot me-1"></i>AI-Generated Analysis
                                        </span>
                                    </div>
                                    <textarea style="display:none" id="writeup-template-{{$cveID}}">{{$writeup}}</textarea>
                                    <div class="writeup-text mb-0" id="writeup-content-{{$cveID}}"></div>
                                </div>
                            </div>
                        </div>
                        {{end}}
                    </div>
                </div>
                {{end}}

                {{if .Analysis.Results}}
                <div class="mt-5">
                    <div class="d-flex flex-wrap justify-content-between align-items-center mb-4">
                        <div class="btn-group mb-3">
                            <button type="button" class="btn btn-outline-secondary active filter-btn" data-filter="all">All</button>
                            <button type="button" class="btn btn-outline-info filter-btn" data-filter="cve">CVE Match</button>
                            <button type="button" class="btn btn-outline-danger filter-btn" data-filter="vuln">Vulnerabilities</button>
                        </div>
                        <div class="input-group mb-3" style="max-width: 300px;">
                            <span class="input-group-text">
                                <i class="fas fa-search text-primary"></i>
                            </span>
                            <input type="text" id="aiSearch" class="form-control" placeholder="Search AI responses">
                        </div>
                    </div>
                </div>

                <div class="results-container">
                    
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <div class="pagination-info">
                            <span class="text-muted" id="paginationInfo">
                                Showing 0 to 0 of 0 results
                            </span>
                        </div>
                        <div class="pagination-controls">
                            <nav aria-label="Results pagination">
                                <ul class="pagination pagination-sm mb-0" id="paginationControls">
                                    
                                </ul>
                            </nav>
                        </div>
                    </div>
                    
                    {{range .IndexedResults}}
                    {{- $hasCVE := false -}}
                    {{- if .Result.CVEMatches -}}
                        {{- range $cve_id, $cve_data := .Result.CVEMatches -}}
                            {{- if eq $cve_data.Result "Yes" -}}
                                {{- $hasCVE = true -}}
                            {{- end -}}
                        {{- end -}}
                    {{- end -}}
                    <div class="card mb-4" 
                         data-ai-response="{{if .Result.AIResponse}}{{toLower .Result.AIResponse}}{{end}}"
                         data-vuln-status="{{if .Result.VulnSeverity}}{{.Result.VulnSeverity}}{{else}}no{{end}}"
                         data-cve-match="{{if $hasCVE}}yes{{else}}no{{end}}"
                         data-filename="{{toLower .Filename}}"
                         data-vuln-status-text="{{if .Result.VulnerabilityStatus}}{{toLower .Result.VulnerabilityStatus}}{{end}}"
                         data-cve-ids="{{range $cve_id, $cve_data := .Result.CVEMatches}}{{if eq $cve_data.Result "Yes"}}{{$cve_id}} {{end}}{{end}}"
                         data-cve-descriptions="{{range $cve_id, $cve_data := .Result.CVEMatches}}{{toLower $cve_data.Description}} {{end}}"
                         data-content="{{range .Result.Context}}{{toLower .}} {{end}}">
                        <div class="card-header collapsible-header" data-bs-toggle="collapse" href="#collapse{{.Index}}">
                            <div class="d-flex justify-content-between align-items-center">
                                <div>
                                    <i class="fas fa-file-code me-2 text-primary"></i>
                                    <span class="fw-bold">{{.Filename}}</span>
                                    {{if .Result.VulnerabilityStatus}}
                                    <span class="vuln-status-badge badge ms-2 {{if contains .Result.VulnerabilityStatus "No vulnerabilities"}}bg-success{{else if contains .Result.VulnerabilityStatus "Not Sure"}}bg-warning{{else if contains .Result.VulnerabilityStatus "vulnerabilities"}}bg-danger{{else}}bg-secondary{{end}}">
                                        {{.Result.VulnerabilityStatus}}
                                    </span>
                                    {{end}}
                                    {{if .Result.CVEMatches}}
                                        {{range $cve_id, $cve_data := .Result.CVEMatches}}
                                            {{if eq $cve_data.Result "Yes"}}
                                            <span class="badge bg-danger badge-glow ms-2">
                                                {{$cve_id}}
                                            </span>
                                            {{end}}
                                        {{end}}
                                    {{end}}
                                </div>
                                <i class="fas fa-chevron-down chevron"></i>
                            </div>
                        </div>
                        <div class="collapse" id="collapse{{.Index}}">
                            <div class="card-body p-0">
                                <pre class="code-block p-3 m-0">{{range .Result.Context}}{{if eq . "..."}}<span class="ellipsis">...</span>{{else if hasPrefix . "+"}}<span class="diff-add">{{.}}</span>{{else if hasPrefix . "-"}}<span class="diff-remove">{{.}}</span>{{else}}<span class="context-line">{{.}}</span>{{end}}{{end}}</pre>
                                {{if .Result.AIResponse}}
                                <div class="ai-response p-3">
                                    <div class="d-flex align-items-center mb-3">
                                        <i class="fas fa-robot text-warning me-2"></i>
                                        <h6 class="mb-0">AI Analysis</h6>
                                    </div>
                                    <button class="btn btn-sm btn-warning" type="button" data-bs-toggle="collapse" data-bs-target="#aiAnalysis{{.Index}}">
                                        <i class="fas fa-eye me-2"></i>Show Full AI Analysis
                                    </button>
                                    <div class="collapse mt-3" id="aiAnalysis{{.Index}}">
                                        <div class="card card-body bg-dark">
                                            <pre class="m-0">{{.Result.AIResponse}}</pre>
                                        </div>
                                    </div>
                                    {{if .Result.CVEMatches}}
                                    <div class="cve-result mt-3">
                                        <div class="d-flex align-items-center mb-3">
                                            <i class="fas fa-shield-alt text-primary me-2"></i>
                                            <h6 class="mb-0">CVE Analysis Results:</h6>
                                        </div>
                                        {{range $cve_id, $cve_data := .Result.CVEMatches}}
                                        <div class="mb-3">
                                            <div class="d-flex align-items-center mb-2">
                                                <span class="badge {{if eq $cve_data.Result "Yes"}}bg-danger badge-glow{{else}}bg-secondary{{end}} me-2">
                                                    {{$cve_id}}: {{$cve_data.Result}}
                                                </span>
                                            </div>
                                            <details>
                                                <summary>View CVE Description</summary>
                                                <pre class="mt-2 small text-muted">{{$cve_data.Description}}</pre>
                                            </details>
                                        </div>
                                        {{end}}
                                    </div>
                                    {{end}}
                                </div>
                                {{end}}
                            </div>
                        </div>
                    </div>
                    {{end}}
                </div>
                
                {{if .Pagination}}
                
                <div class="d-flex justify-content-between align-items-center mt-4 pt-4 border-top">
                    <div class="pagination-info">
                        <span class="text-muted">
                            Showing {{.Pagination.StartItem}} to {{.Pagination.EndItem}} of {{.Pagination.TotalItems}} results
                        </span>
                    </div>
                    <nav aria-label="Analysis results pagination">
                        <ul class="pagination pagination-sm mb-0">
                            {{if .Pagination.HasPrev}}
                            <li class="page-item">
                                <a class="page-link" href="?page={{.Pagination.PrevPage}}&per_page={{.Pagination.PerPage}}&filter={{.Pagination.Filter}}&search={{.Pagination.Search}}">
                                    <i class="fas fa-chevron-left"></i> Previous
                                </a>
                            </li>
                            {{else}}
                            <li class="page-item disabled">
                                <span class="page-link">
                                    <i class="fas fa-chevron-left"></i> Previous
                                </span>
                            </li>
                            {{end}}
                            
                            {{range .Pagination.PageNumbers}}
                            <li class="page-item {{if eq . $.Pagination.Page}}active{{end}}">
                                <a class="page-link" href="?page={{.}}&per_page={{$.Pagination.PerPage}}&filter={{$.Pagination.Filter}}&search={{$.Pagination.Search}}">{{.}}</a>
                            </li>
                            {{end}}
                            
                            {{if .Pagination.HasNext}}
                            <li class="page-item">
                                <a class="page-link" href="?page={{.Pagination.NextPage}}&per_page={{.Pagination.PerPage}}&filter={{.Pagination.Filter}}&search={{.Pagination.Search}}">
                                    Next <i class="fas fa-chevron-right"></i>
                                </a>
                            </li>
                            {{else}}
                            <li class="page-item disabled">
                                <span class="page-link">
                                    Next <i class="fas fa-chevron-right"></i>
                                </span>
                            </li>
                            {{end}}
                        </ul>
                    </nav>
                </div>
                {{end}}
                {{end}}
            </div>
        </div>
    </div>

<footer class="py-5 mt-5">
    <div class="container">
        <div class="row g-4">
            <div class="col-lg-4 col-md-6">
                <div class="footer-brand mb-4">
                    <a href="#" class="d-flex align-items-center text-decoration-none">
                        <i class="fas fa-shield-alt fs-3 me-2 text-primary"></i>
                        <span class="fs-4 fw-bold text-white">PatchLeaks</span>
                    </a>
                </div>
                <p class="text-muted mb-4">Advanced security patch analysis system for enterprise applications. Detect vulnerabilities before they become threats.</p>
                <div class="footer-social d-flex gap-3">
                    <a href="#" class="social-link" aria-label="GitHub">
                        <i class="fab fa-github"></i>
                    </a>
                    <a href="#" class="social-link" aria-label="Twitter">
                        <i class="fab fa-twitter"></i>
                    </a>
                    <a href="#" class="social-link" aria-label="LinkedIn">
                        <i class="fab fa-linkedin-in"></i>
                    </a>
                    <a href="#" class="social-link" aria-label="Discord">
                        <i class="fab fa-discord"></i>
                    </a>
                </div>
            </div>
            
            <div class="col-lg-2 col-md-6 col-6">
                <h5 class="footer-heading">Products</h5>
                <ul class="footer-links">
                    <li><a href="#folder-analysis">Folder Analysis</a></li>
                    <li><a href="#product-analysis">Product Analysis</a></li>
                    <li><a href="#ai-analysis">AI Analysis</a></li>
                    <li><a href="#reports">Reports</a></li>
                </ul>
            </div>
            
            <div class="col-lg-2 col-md-6 col-6">
                <h5 class="footer-heading">Resources</h5>
                <ul class="footer-links">
                    <li><a href="#">Documentation</a></li>
                    <li><a href="#">API Reference</a></li>
                    <li><a href="#">Tutorials</a></li>
                    <li><a href="#">Blog</a></li>
                </ul>
            </div>
            
            <div class="col-lg-2 col-md-6 col-6">
                <h5 class="footer-heading">Company</h5>
                <ul class="footer-links">
                    <li><a href="#">About Us</a></li>
                    <li><a href="#">Careers</a></li>
                    <li><a href="#">Contact</a></li>
                    <li><a href="#">Partners</a></li>
                </ul>
            </div>
            
            <div class="col-lg-2 col-md-6 col-6">
                <h5 class="footer-heading">Legal</h5>
                <ul class="footer-links">
                    <li><a href="#">Privacy</a></li>
                    <li><a href="#">Terms</a></li>
                    <li><a href="#">Security</a></li>
                    <li><a href="#">Compliance</a></li>
                </ul>
            </div>
        </div>
        
        <div class="footer-newsletter mt-5 p-4">
            <div class="row align-items-center">
                <div class="col-lg-5 mb-4 mb-lg-0">
                    <h5 class="text-white mb-1">Stay updated</h5>
                    <p class="text-muted mb-0">Get the latest security updates and news</p>
                </div>
                <div class="col-lg-7">
                    <form class="d-flex flex-column flex-sm-row gap-2">
                        <input type="email" class="form-control" placeholder="Enter your email" aria-label="Email">
                        <button type="submit" class="btn btn-primary">Subscribe</button>
                    </form>
                </div>
            </div>
        </div>
        
        <div class="footer-bottom d-flex flex-column flex-md-row justify-content-between align-items-center mt-5 pt-4 border-top border-secondary">
            <p class="mb-3 mb-md-0 text-muted">© 2025 PatchLeaks. All rights reserved.</p>
            <div class="d-flex gap-3 flex-wrap">
                <a href="#" class="text-muted text-decoration-none small">Privacy Policy</a>
                <span class="text-muted d-none d-md-inline">•</span>
                <a href="#" class="text-muted text-decoration-none small">Terms of Service</a>
                <span class="text-muted d-none d-md-inline">•</span>
                <a href="#" class="text-muted text-decoration-none small">Cookie Settings</a>
            </div>
        </div>
    </div>
</footer>

    <script src="/static/js/bootstrap.bundle.min.js"></script>
    <script src="/static/js/notifications.js"></script>
    <script src="/static/js/marked.min.js"></script>
    <script>
    document.addEventListener('DOMContentLoaded', () => {
        
        document.querySelectorAll('textarea[id^="writeup-template-"]').forEach(textarea => {
            const cveId = textarea.id.replace('writeup-template-', '');
            const targetDiv = document.getElementById('writeup-content-' + cveId);
            
            if (targetDiv && typeof marked !== 'undefined') {
                try {
                    const markdownContent = textarea.value;
                    targetDiv.innerHTML = marked.parse(markdownContent);
                } catch (error) {
                    console.error('Error parsing markdown for ' + cveId + ':', error);
                    
                    targetDiv.innerHTML = '<pre>' + textarea.value + '</pre>';
                }
            }
        });

        window.addEventListener('scroll', () => {
            const navbar = document.querySelector('.navbar');
            if (window.scrollY > 50) {
                navbar.classList.add('scrolled');
            } else {
                navbar.classList.remove('scrolled');
            }
        });
    });
    
    function copyLink() {
        const copyText = document.getElementById("analysisUrl");
        copyText.select();
        document.execCommand("copy");
        
        const copyBtn = document.querySelector('.alert-info .btn');
        const originalContent = copyBtn.innerHTML;
        copyBtn.innerHTML = '<i class="fas fa-check me-2"></i>Copied!';
        
        setTimeout(() => {
            copyBtn.innerHTML = originalContent;
        }, 2000);
    }

    document.querySelectorAll('.filter-btn').forEach(btn => {
        btn.addEventListener('click', function() {
            document.querySelectorAll('.filter-btn').forEach(b => b.classList.remove('active'));
            this.classList.add('active');
            updatePagination();
        });
    });

    const aiSearchElement = document.getElementById('aiSearch');
    if (aiSearchElement) {
        aiSearchElement.addEventListener('input', () => {
            updatePagination();
        });
    }

    
    let currentPage = 1;
    const itemsPerPage = 20;
    let allCards = [];
    let filteredCards = [];

    
    function initializePagination() {
        allCards = Array.from(document.querySelectorAll('[data-vuln-status]'));
        updatePagination();
    }

    
    function updatePagination() {
        const activeFilter = document.querySelector('.filter-btn.active');
        const filter = activeFilter ? activeFilter.dataset.filter : 'all';
        const search = document.getElementById('aiSearch').value.toLowerCase();
        
        
        filteredCards = allCards.filter(card => {
            const vulnStatus = card.dataset.vulnStatus;
            const vulnStatusText = card.dataset.vulnStatusText || '';
            const cveMatch = card.dataset.cveMatch;
            const aiResponse = card.dataset.aiResponse || '';
            const filename = card.dataset.filename || '';
            const content = card.dataset.content || '';
            const cveIds = card.dataset.cveIds || '';
            const cveDescriptions = card.dataset.cveDescriptions || '';
            
            
            const searchMatches = search === '' || 
                filename.includes(search) ||
                content.includes(search) ||
                aiResponse.includes(search) ||
                vulnStatusText.includes(search) ||
                cveIds.includes(search) ||
                cveDescriptions.includes(search);
            
            let show = false;
            if(filter === 'all') {
                show = searchMatches;
            } 
            else if(filter === 'cve') {
                show = cveMatch === 'yes' && searchMatches;
            }
            else if(filter === 'vuln') {
                show = vulnStatusText.includes('vulnerabilities') && !vulnStatusText.includes('no vulnerabilities') && searchMatches;
            }
            
            return show;
        });

        
        currentPage = 1;
        displayCurrentPage();
        updatePaginationControls();
    }

    
    function displayCurrentPage() {
        const startIndex = (currentPage - 1) * itemsPerPage;
        const endIndex = startIndex + itemsPerPage;
        
        
        allCards.forEach(card => card.style.display = 'none');
        
        
        filteredCards.slice(startIndex, endIndex).forEach(card => {
            card.style.display = 'block';
        });
        
        
        const totalItems = filteredCards.length;
        const startItem = totalItems > 0 ? startIndex + 1 : 0;
        const endItem = Math.min(endIndex, totalItems);
        
        document.getElementById('paginationInfo').textContent = 
            `Showing ${startItem} to ${endItem} of ${totalItems} results`;
    }

    
    function updatePaginationControls() {
        const totalPages = Math.ceil(filteredCards.length / itemsPerPage);
        const paginationControls = document.getElementById('paginationControls');
        
        if (totalPages <= 1) {
            paginationControls.innerHTML = '';
            return;
        }
        
        let paginationHTML = '';
        
        
        if (currentPage > 1) {
            paginationHTML += `
                <li class="page-item">
                    <a class="page-link" href="#" onclick="goToPage(${currentPage - 1})">
                        <i class="fas fa-chevron-left"></i> Previous
                    </a>
                </li>
            `;
        } else {
            paginationHTML += `
                <li class="page-item disabled">
                    <span class="page-link">
                        <i class="fas fa-chevron-left"></i> Previous
                    </span>
                </li>
            `;
        }
        
        
        const startPage = Math.max(1, currentPage - 2);
        const endPage = Math.min(totalPages, currentPage + 2);
        
        for (let i = startPage; i <= endPage; i++) {
            const activeClass = i === currentPage ? 'active' : '';
            paginationHTML += `
                <li class="page-item ${activeClass}">
                    <a class="page-link" href="#" onclick="goToPage(${i})">${i}</a>
                </li>
            `;
        }
        
        
        if (currentPage < totalPages) {
            paginationHTML += `
                <li class="page-item">
                    <a class="page-link" href="#" onclick="goToPage(${currentPage + 1})">
                        Next <i class="fas fa-chevron-right"></i>
                    </a>
                </li>
            `;
        } else {
            paginationHTML += `
                <li class="page-item disabled">
                    <span class="page-link">
                        Next <i class="fas fa-chevron-right"></i>
                    </span>
                </li>
            `;
        }
        
        paginationControls.innerHTML = paginationHTML;
    }

    
    function goToPage(page) {
        const totalPages = Math.ceil(filteredCards.length / itemsPerPage);
        if (page >= 1 && page <= totalPages) {
            currentPage = page;
            displayCurrentPage();
            updatePaginationControls();
        }
    }

    
    document.addEventListener('DOMContentLoaded', () => {
        initializePagination();
    });
    </script>
</body>
</html>
