<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RepliMap Drift Report</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        .code-block {
            font-family: 'SF Mono', 'Monaco', 'Consolas', monospace;
            font-size: 12px;
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen">

    <!-- Header -->
    <header class="bg-gradient-to-r from-orange-500 to-red-600 text-white py-8">
        <div class="max-w-6xl mx-auto px-4">
            <div class="flex items-center justify-between">
                <div>
                    <h1 class="text-3xl font-bold">
                        {% if report.has_drift %}
                        Drift Detected
                        {% else %}
                        No Drift
                        {% endif %}
                    </h1>
                    <p class="text-orange-100 mt-2">Infrastructure Drift Report</p>
                </div>
                <div class="text-right text-sm text-orange-100">
                    <div>Region: {{ report.region }}</div>
                    <div>{{ generated_at[:19] }}</div>
                </div>
            </div>
        </div>
    </header>

    <main class="max-w-6xl mx-auto px-4 py-8">

        <!-- Summary Cards -->
        <section class="grid grid-cols-2 md:grid-cols-5 gap-4 mb-8">
            <div class="bg-white rounded-lg shadow p-4 text-center">
                <div class="text-3xl font-bold text-gray-800">{{ report.total_resources }}</div>
                <div class="text-sm text-gray-500">Total Resources</div>
            </div>
            <div class="bg-white rounded-lg shadow p-4 text-center border-l-4 border-red-500">
                <div class="text-3xl font-bold text-red-600">{{ report.drifted_resources }}</div>
                <div class="text-sm text-gray-500">Drifted</div>
            </div>
            <div class="bg-white rounded-lg shadow p-4 text-center">
                <div class="text-3xl font-bold text-green-600">{{ report.added_resources }}</div>
                <div class="text-sm text-gray-500">Added</div>
            </div>
            <div class="bg-white rounded-lg shadow p-4 text-center">
                <div class="text-3xl font-bold text-red-600">{{ report.removed_resources }}</div>
                <div class="text-sm text-gray-500">Removed</div>
            </div>
            <div class="bg-white rounded-lg shadow p-4 text-center">
                <div class="text-3xl font-bold text-yellow-600">{{ report.modified_resources }}</div>
                <div class="text-sm text-gray-500">Modified</div>
            </div>
        </section>

        <!-- Critical/High Priority Drifts -->
        {% set critical_high = report.critical_drifts + report.high_drifts %}
        {% if critical_high %}
        <section class="bg-white rounded-lg shadow-lg p-6 mb-8 border-l-4 border-red-500">
            <h2 class="text-xl font-bold text-red-700 mb-4">
                High Priority Drifts ({{ critical_high|length }})
            </h2>
            <p class="text-gray-600 mb-4">These changes may impact security or production stability.</p>

            <div class="space-y-4">
                {% for drift in critical_high %}
                <div class="border rounded-lg overflow-hidden">
                    <div class="bg-red-50 px-4 py-3 flex items-center justify-between">
                        <div class="flex items-center gap-3">
                            <span class="text-xl">
                                {% if drift.drift_type == DriftType.ADDED %}+
                                {% elif drift.drift_type == DriftType.REMOVED %}-
                                {% else %}~{% endif %}
                            </span>
                            <div>
                                <span class="font-mono text-sm bg-gray-200 px-2 py-1 rounded">
                                    {{ drift.resource_type }}
                                </span>
                                <span class="ml-2 font-medium">{{ drift.resource_id }}</span>
                            </div>
                        </div>
                        <span class="px-2 py-1 text-xs font-bold rounded
                            {% if drift.severity == DriftSeverity.CRITICAL %}bg-purple-100 text-purple-800
                            {% else %}bg-red-100 text-red-800{% endif %}">
                            {{ drift.severity.value|upper }}
                        </span>
                    </div>

                    {% if drift.tf_address %}
                    <div class="px-4 py-2 bg-gray-50 text-sm text-gray-600">
                        TF Address: <code>{{ drift.tf_address }}</code>
                    </div>
                    {% endif %}

                    {% if drift.diffs %}
                    <div class="p-4">
                        <table class="w-full text-sm">
                            <thead class="text-left text-gray-500">
                                <tr>
                                    <th class="pb-2">Attribute</th>
                                    <th class="pb-2">Expected (TF)</th>
                                    <th class="pb-2">Actual (AWS)</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y">
                                {% for diff in drift.diffs %}
                                <tr>
                                    <td class="py-2 font-medium">{{ diff.attribute }}</td>
                                    <td class="py-2">
                                        <code class="bg-red-50 text-red-700 px-1 rounded code-block">
                                            {{ diff.expected | truncate(50) }}
                                        </code>
                                    </td>
                                    <td class="py-2">
                                        <code class="bg-green-50 text-green-700 px-1 rounded code-block">
                                            {{ diff.actual | truncate(50) }}
                                        </code>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% endif %}
                </div>
                {% endfor %}
            </div>
        </section>
        {% endif %}

        <!-- All Drifts -->
        <section class="bg-white rounded-lg shadow-lg p-6 mb-8">
            <h2 class="text-xl font-bold text-gray-800 mb-4">
                All Drifts ({{ report.drifted_resources }})
            </h2>

            {% if not report.has_drift %}
            <div class="text-center py-8">
                <span class="text-6xl">OK</span>
                <p class="mt-4 text-gray-600">No infrastructure drift detected!</p>
                <p class="text-sm text-gray-500">Your AWS resources match your Terraform state.</p>
            </div>
            {% else %}
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-2 text-left">Type</th>
                            <th class="px-4 py-2 text-left">Resource</th>
                            <th class="px-4 py-2 text-left">Drift</th>
                            <th class="px-4 py-2 text-left">Severity</th>
                            <th class="px-4 py-2 text-left">Changes</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y">
                        {% for drift in report.drifts %}
                        {% if drift.is_drifted %}
                        <tr class="hover:bg-gray-50">
                            <td class="px-4 py-2 font-mono text-xs">{{ drift.resource_type }}</td>
                            <td class="px-4 py-2">
                                <div class="font-medium">{{ drift.resource_id }}</div>
                                {% if drift.tf_address %}
                                <div class="text-xs text-gray-500">{{ drift.tf_address }}</div>
                                {% endif %}
                            </td>
                            <td class="px-4 py-2">
                                <span class="px-2 py-1 rounded text-xs font-medium
                                    {% if drift.drift_type == DriftType.ADDED %}bg-green-100 text-green-800
                                    {% elif drift.drift_type == DriftType.REMOVED %}bg-red-100 text-red-800
                                    {% else %}bg-yellow-100 text-yellow-800{% endif %}">
                                    {{ drift.drift_type.value|upper }}
                                </span>
                            </td>
                            <td class="px-4 py-2">
                                <span class="px-2 py-1 rounded text-xs font-medium
                                    {% if drift.severity == DriftSeverity.CRITICAL %}bg-purple-100 text-purple-800
                                    {% elif drift.severity == DriftSeverity.HIGH %}bg-red-100 text-red-800
                                    {% elif drift.severity == DriftSeverity.MEDIUM %}bg-yellow-100 text-yellow-800
                                    {% else %}bg-blue-100 text-blue-800{% endif %}">
                                    {{ drift.severity.value|upper }}
                                </span>
                            </td>
                            <td class="px-4 py-2 text-gray-600">
                                {{ drift.diff_count }} change(s)
                            </td>
                        </tr>
                        {% endif %}
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% endif %}
        </section>

        <!-- Remediation Suggestions -->
        {% if report.has_drift %}
        <section class="bg-indigo-50 rounded-lg p-6 mb-8">
            <h2 class="text-xl font-bold text-indigo-800 mb-4">Remediation Options</h2>
            <div class="space-y-3 text-indigo-900">
                <div class="flex items-start gap-3">
                    <span class="text-indigo-600 font-bold">1.</span>
                    <div>
                        <strong>Accept the drift:</strong>
                        Run <code class="bg-indigo-100 px-2 py-1 rounded">terraform apply -refresh-only</code>
                        to update state to match AWS.
                    </div>
                </div>
                <div class="flex items-start gap-3">
                    <span class="text-indigo-600 font-bold">2.</span>
                    <div>
                        <strong>Revert to Terraform:</strong>
                        Run <code class="bg-indigo-100 px-2 py-1 rounded">terraform apply</code>
                        to change AWS back to match your code.
                    </div>
                </div>
                <div class="flex items-start gap-3">
                    <span class="text-indigo-600 font-bold">3.</span>
                    <div>
                        <strong>Update your code:</strong>
                        Modify your Terraform files to match the desired state, then apply.
                    </div>
                </div>
            </div>
        </section>
        {% endif %}

        <!-- CTA -->
        <section class="bg-gradient-to-r from-indigo-600 to-purple-600 rounded-lg p-8 text-white text-center">
            <h2 class="text-2xl font-bold mb-4">Need Help Managing Infrastructure Drift?</h2>
            <p class="text-indigo-100 mb-6">
                We can help you set up automated drift detection, alerting, and remediation workflows.
            </p>
            <a href="mailto:hello@comply.sh?subject=Drift%20Management%20Help"
               class="inline-block px-6 py-3 bg-white text-indigo-600 font-bold rounded-lg hover:bg-indigo-50">
                Contact Us
            </a>
        </section>

    </main>

    <!-- Footer -->
    <footer class="bg-gray-800 text-gray-400 py-6 mt-8">
        <div class="max-w-6xl mx-auto px-4 text-center text-sm">
            <p>Generated by RepliMap Drift Detector</p>
            <p class="mt-1">Scan duration: {{ report.scan_duration_seconds }}s | State: {{ report.state_file }}</p>
        </div>
    </footer>

</body>
</html>
