package components

import "context"
import "strings"

type TwoFactorBackupCodesData struct {
	BackupCodes     []string
	ErrorMessage    string
	SuccessMessage  string
}

templ TwoFactorBackupCodes(ctx context.Context, data TwoFactorBackupCodesData) {
	@LayoutWithContext("Two-Factor Authentication Backup Codes", ctx) {
		<div class="p-4 md:p-6 2xl:p-10">
			<!-- Page Header -->
			<div class="mb-6 flex flex-col md:flex-row md:items-center md:justify-between gap-4">
				<h1 class="text-2xl font-bold text-gray-900 dark:text-white flex items-center">
					<i class="fas fa-key w-6 h-6 mr-2 text-blue-500"></i>
					Two-Factor Authentication Backup Codes
				</h1>
			</div>
			<p class="text-sm text-gray-500 dark:text-gray-400 mb-6">
				These codes can be used to login if you lose access to your authenticator app
			</p>

			if data.ErrorMessage != "" {
				<div class="p-4 mb-6 text-sm text-red-800 rounded-lg bg-red-50 dark:bg-red-900/50 dark:text-red-400 border border-red-200 dark:border-red-800" role="alert">
					<div class="flex items-center">
						<i class="fas fa-exclamation-circle flex-shrink-0 mr-2"></i>
						<span>{ data.ErrorMessage }</span>
					</div>
				</div>
			}

			if data.SuccessMessage != "" {
				if (strings.Contains(data.SuccessMessage, "IMPORTANT")) {
					<!-- Important warning alert -->
					<div class="p-4 mb-6 text-sm text-yellow-800 rounded-lg bg-yellow-50 dark:bg-yellow-900/50 dark:text-yellow-300 border border-yellow-200 dark:border-yellow-800" role="alert">
						<div class="flex items-center">
							<i class="fas fa-exclamation-triangle flex-shrink-0 mr-2"></i>
							<span class="font-bold">{ data.SuccessMessage }</span>
						</div>
					</div>
				} else {
					<!-- Success alert -->
					<div class="p-4 mb-6 text-sm text-green-800 rounded-lg bg-green-50 dark:bg-green-900/50 dark:text-green-400 border border-green-200 dark:border-green-800" role="alert">
						<div class="flex items-center">
							<i class="fas fa-check-circle flex-shrink-0 mr-2"></i>
							<span>{ data.SuccessMessage }</span>
						</div>
					</div>
				}
			}

			<div class="space-y-8">
				if len(data.BackupCodes) > 0 {
					<!-- Backup codes card -->
					<div class="bg-white border border-gray-200 rounded-lg shadow-sm dark:border-gray-700 dark:bg-gray-800">
						<div class="p-4 border-b border-gray-200 dark:border-gray-700">
							<div class="flex items-center space-x-3">
								<div class="h-10 w-10 bg-blue-100 dark:bg-blue-900 rounded-full flex items-center justify-center">
									<i class="fas fa-key text-blue-600 dark:text-blue-300"></i>
								</div>
								<div>
									<h3 class="text-lg font-semibold text-gray-900 dark:text-white">
										Your Backup Codes
									</h3>
									<p class="text-sm text-gray-500 dark:text-gray-400">Store these securely for account recovery</p>
								</div>
							</div>
						</div>
						
						<div class="p-6">
							<!-- Security Alert -->
							<div class="p-4 mb-6 text-sm text-yellow-800 rounded-lg bg-yellow-50 dark:bg-yellow-900/50 dark:text-yellow-300 border border-yellow-200 dark:border-yellow-800" role="alert">
								<div class="flex items-start">
									<i class="fas fa-shield-alt flex-shrink-0 mr-3 mt-0.5"></i>
									<div>
										<h4 class="text-sm font-medium mb-1">Security information</h4>
										<p class="font-semibold mb-2">These codes are your backup method to access your account.</p>
										<ul class="list-disc pl-5 space-y-1">
											<li>Each code can only be used once.</li>
											<li>Store these codes in a secure password manager.</li>
											<li>These codes allow access to your account - keep them safe!</li>
											<li>For security, you can only view codes immediately after generating them.</li>
										</ul>
									</div>
								</div>
							</div>
							
							<!-- Backup Codes Grid -->
							<div class="grid grid-cols-2 gap-4 mb-6">
								for _, code := range data.BackupCodes {
									if strings.Contains(code, "REDACTED") {
										<div class="p-2.5 bg-gray-100 dark:bg-gray-700 rounded font-mono text-sm text-center text-gray-500 dark:text-gray-400">
											{ code }
										</div>
									} else {
										<div class="p-2.5 bg-blue-50 dark:bg-blue-900/30 border border-blue-200 dark:border-blue-800 rounded font-mono text-sm text-center font-bold text-blue-900 dark:text-blue-300">
											{ code }
										</div>
									}
								}
							</div>
							
							<!-- Buttons -->
							<div class="flex flex-col sm:flex-row justify-center gap-3 mt-6">
								if !strings.Contains(data.BackupCodes[0], "REDACTED") {
									<button 
										class="inline-flex items-center justify-center px-5 py-2.5 text-sm font-medium text-gray-900 bg-white border border-gray-200 rounded-lg focus:outline-none hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700"
										onclick="downloadBackupCodes(this)">
										<i class="fas fa-download mr-2"></i>
										Download Backup Codes
									</button>
								}
								<form method="POST" action="/profile/2fa/regenerate-codes" class="inline">
									<button type="submit" class="inline-flex items-center justify-center w-full sm:w-auto text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">
										<i class="fas fa-sync-alt mr-2"></i>
										Generate New Codes
									</button>
								</form>
							</div>
						</div>
					</div>

					<script>
						function downloadBackupCodes(button) {
							// Get backup codes from the displayed elements that don't contain "REDACTED"
							const codes = Array.from(
								document.querySelectorAll('.bg-blue-50.dark\\:bg-blue-900\\/30')
							).map(el => el.textContent.trim());
							
							// Create content for the file
							const content = 
								"2FA BACKUP CODES - KEEP THESE SAFE!\n" +
								"=====================================\n\n" +
								codes.join("\n") +
								"\n\n" +
								"Generated: " + new Date().toISOString().split('T')[0] + "\n" +
								"SECURITY WARNINGS:\n" +
								"* These codes can be used to access your account if you lose access to your authenticator app.\n" +
								"* Each code can only be used once.\n" +
								"* Keep these codes in a secure location like a password manager.\n" + 
								"* Treat these codes with the same security as your password.";
							
							// Create blob and download link
							const blob = new Blob([content], { type: 'text/plain' });
							const url = window.URL.createObjectURL(blob);
							const a = document.createElement('a');
							a.href = url;
							a.download = '2fa-backup-codes-secure.txt';
							
							// Trigger download
							document.body.appendChild(a);
							a.click();
							
							// Cleanup
							window.URL.revokeObjectURL(url);
							document.body.removeChild(a);
						}
					</script>
				} else {
					<!-- No backup codes state -->
					<div class="bg-white border border-gray-200 rounded-lg shadow-sm dark:border-gray-700 dark:bg-gray-800 p-8 flex flex-col items-center justify-center text-center">
						<div class="inline-flex h-16 w-16 flex-shrink-0 items-center justify-center rounded-full bg-gray-100 mb-4 dark:bg-gray-700">
							<i class="fas fa-key text-gray-400 dark:text-gray-500 text-3xl"></i>
						</div>
						<h3 class="mb-2 text-lg font-semibold text-gray-900 dark:text-white">No Backup Codes Available</h3>
						<p class="text-gray-500 dark:text-gray-400 mb-4">You don't have any backup codes. Generate new ones for account recovery.</p>
						<form method="POST" action="/profile/2fa/regenerate-codes">
							<button type="submit" class="inline-flex items-center px-3 py-2 text-sm font-medium text-center text-white bg-blue-700 rounded-lg hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">
								<i class="fas fa-sync-alt w-4 h-4 mr-2"></i>
								Generate Backup Codes
							</button>
						</form>
					</div>
				}
			</div>

			<!-- Help Notice -->
			<div class="mt-6">
				<div class="p-4 text-sm text-blue-800 rounded-lg bg-blue-50 dark:bg-blue-900/50 dark:text-blue-400 border border-blue-200 dark:border-blue-800 flex items-start">
					<i class="fas fa-shield-alt flex-shrink-0 mr-3 mt-0.5"></i>
					<div>
						<h4 class="text-sm font-medium mb-1">Security Information</h4>
						<p>Backup codes are single-use passwords that allow you to sign in to your account when you don't have access to your authenticator app. Keep these codes in a secure place, such as a password manager or a secure file.</p>
					</div>
				</div>
			</div>

			<!-- Back to Profile Link -->
			<div class="mt-6 flex justify-start">
				<a href="/profile" class="inline-flex items-center text-sm font-medium text-blue-600 dark:text-blue-500 hover:underline">
					<i class="fas fa-arrow-left mr-2"></i>
					Back to Profile
				</a>
			</div>
		</div>
	}
} 