package components

import "context"

type TwoFactorSetupData struct {
	QRCodeURL    string
	Secret       string
	BackupCodes  []string
	ErrorMessage string
}

templ TwoFactorSetup(ctx context.Context, data TwoFactorSetupData) {
	@LayoutWithContext("Two-Factor Authentication Setup", 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-shield-alt w-6 h-6 mr-2 text-blue-500"></i>
					Two-Factor Authentication Setup
				</h1>
			</div>
			<p class="text-sm text-gray-500 dark:text-gray-400 mb-6">
				Enhance your account security with 2FA by setting up a verification 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>
			}

			<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
				<!-- QR Code Section -->
				<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-qrcode text-blue-600 dark:text-blue-300"></i>
							</div>
							<div>
								<h3 class="text-lg font-semibold text-gray-900 dark:text-white">
									1. Scan QR Code
								</h3>
								<p class="text-sm text-gray-500 dark:text-gray-400">Scan this QR code with your authenticator app</p>
							</div>
						</div>
					</div>
					<div class="p-6 flex flex-col items-center">
						<div class="mb-6 bg-white p-2 rounded-lg border border-gray-200 dark:border-gray-700">
							<img src={ data.QRCodeURL } alt="QR Code" class="h-48 w-48"/>
						</div>
						<div class="w-full text-center mb-2">
							<p class="text-sm text-gray-500 dark:text-gray-400 mb-2">
								Can't scan the QR code? Use this code instead:
							</p>
							<div class="bg-gray-50 dark:bg-gray-700 rounded-lg border border-gray-200 dark:border-gray-600 p-3 font-mono text-sm text-gray-800 dark:text-gray-200 break-all">
								{ data.Secret }
							</div>
						</div>
					</div>
				</div>

				<!-- Verification Section -->
				<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-check-circle text-blue-600 dark:text-blue-300"></i>
							</div>
							<div>
								<h3 class="text-lg font-semibold text-gray-900 dark:text-white">
									2. Verify Setup
								</h3>
								<p class="text-sm text-gray-500 dark:text-gray-400">Enter the code from your authenticator app</p>
							</div>
						</div>
					</div>
					<div class="p-6">
						<form 
							method="POST"
							action="/profile/2fa/verify"
							class="space-y-4"
							x-data="{ code: '', loading: false }"
							@submit="loading = true">
							<div>
								<label for="code" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
									Enter the 6-digit code from your authenticator app
								</label>
								<input 
									type="text" 
									id="code"
									name="code"
									x-model="code"
									class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
									pattern="[0-9]*"
									inputmode="numeric"
									maxlength="6"
									placeholder="Enter 6-digit code"
									required/>
							</div>
							<button 
								type="submit"
								class="w-full 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"
								x-bind:disabled="code.length !== 6 || loading">
								<span x-show="!loading" class="flex items-center justify-center">
									<i class="fas fa-shield-alt mr-2"></i>
									Verify and Enable 2FA
								</span>
								<span x-show="loading" class="flex items-center justify-center">
									<svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
										<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
										<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
									</svg>
									Verifying...
								</span>
							</button>
						</form>
					</div>
				</div>
			</div>

			if len(data.BackupCodes) > 0 {
				<div class="mt-6 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">
									3. Save Backup Codes
								</h3>
								<p class="text-sm text-gray-500 dark:text-gray-400">Store these backup codes in a safe place</p>
							</div>
						</div>
					</div>
					<div class="p-6">
						<div class="p-4 mb-4 text-sm text-yellow-800 rounded-lg bg-yellow-50 dark:bg-yellow-900/50 dark:text-yellow-400 border border-yellow-200 dark:border-yellow-800" role="alert">
							<div class="flex">
								<i class="fas fa-exclamation-triangle flex-shrink-0 mr-2 mt-1"></i>
								<div>
									<span class="font-medium">Important:</span> These codes can be used to access your account if you lose your authenticator device. Each code can only be used once. Keep these codes safe and secure.
								</div>
							</div>
						</div>
						
						<div class="grid grid-cols-2 md:grid-cols-3 gap-3 mb-6">
							for _, code := range data.BackupCodes {
								<div class="p-2 bg-gray-50 dark:bg-gray-700 rounded-lg border border-gray-200 dark:border-gray-600 font-mono text-sm text-center text-gray-800 dark:text-gray-200">
									{ code }
								</div>
							}
						</div>
						
						<div class="text-center">
							<button 
								class="py-2.5 px-5 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded-lg border border-gray-200 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 inline-flex items-center"
								onclick="downloadBackupCodes(this)">
								<i class="fas fa-download mr-2"></i>
								Download Backup Codes
							</button>
							<script>
								function downloadBackupCodes(button) {
									// Get backup codes from the displayed elements
									const codes = Array.from(
										document.querySelectorAll('.bg-gray-50.dark\\:bg-gray-700.font-mono')
									).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" +
										"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. Keep these codes safe and secure.";
									
									// 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.txt';
									
									// Trigger download
									document.body.appendChild(a);
									a.click();
									
									// Cleanup
									window.URL.revokeObjectURL(url);
									document.body.removeChild(a);
								}
							</script>
						</div>
					</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-info-circle flex-shrink-0 mr-3 mt-0.5"></i>
					<div>
						<h4 class="text-sm font-medium mb-1">About Two-Factor Authentication</h4>
						<p>After enabling 2FA, you'll need to enter a verification code from your authenticator app when signing in. This adds an extra layer of security to your account even if your password is compromised.</p>
					</div>
				</div>
			</div>
		</div>
	}
} 