package components

import (
	"context"
	"fmt"
)

// NotificationService defines a notification service configuration
type NotificationService struct {
	ID              uint
	Name            string
	Type            string // "email", "webhook", etc.
	IsEnabled       bool
	Config          map[string]string
	Description     string
	EventTriggers   []string
	PayloadTemplate string
	SecretKey       string
	RetryPolicy     string
	SuccessCount    int
	FailureCount    int
}

// SettingsNotificationsData contains data for the notifications page in settings
type SettingsNotificationsData struct {
	NotificationServices []NotificationService
	ErrorMessage         string
	SuccessMessage       string
}

// SettingsData contains data for the settings page
type SettingsData struct {
	NotificationServices []NotificationService
	ErrorMessage         string
	SuccessMessage       string
}

templ Settings(ctx context.Context, data SettingsData) {
	@LayoutWithContext("Application Settings", ctx) {
		<div id="settings-container" style="min-height: 100vh;" class="bg-gray-50 dark:bg-gray-900">
			<div class="pb-8 w-full">
				<div class="mb-6">
					<h1 class="text-2xl font-bold text-gray-900 dark:text-white flex items-center">
						<i class="fas fa-cog w-6 h-6 mr-2 text-blue-500"></i>
						Application Settings
					</h1>
					<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">
						Configure global settings for your GoMFT instance.
					</p>
				</div>
				<!-- Success Message -->
				if data.SuccessMessage != "" {
					<div class="p-4 mb-6 text-sm text-green-800 rounded-lg bg-green-50 dark:bg-green-900 dark:text-green-400" role="alert">
						<div class="flex items-center">
							<i class="fas fa-check-circle mr-2"></i>
							<span>{ data.SuccessMessage }</span>
						</div>
					</div>
				}
				<!-- Error Message -->
				if data.ErrorMessage != "" {
					<div class="p-4 mb-6 text-sm text-red-800 rounded-lg bg-red-50 dark:bg-red-900 dark:text-red-400" role="alert">
						<div class="flex items-center">
							<i class="fas fa-exclamation-circle mr-2"></i>
							<span>{ data.ErrorMessage }</span>
						</div>
					</div>
				}
				<!-- Tabs -->
				<div class="mb-4 border-b border-gray-200 dark:border-gray-700">
					<ul class="flex flex-wrap -mb-px text-sm font-medium text-center" id="settingsTabs" role="tablist">
						<li class="mr-2" role="presentation">
							<button class="inline-block p-4 border-b-2 rounded-t-lg border-blue-600 text-blue-600 dark:text-blue-500 dark:border-blue-500" id="notifications-tab" data-tabs-target="#notifications" type="button" role="tab" aria-controls="notifications" aria-selected="true">
								<i class="fas fa-bell mr-2"></i>Notifications
							</button>
						</li>
						<li class="mr-2" role="presentation">
							<button class="inline-block p-4 border-b-2 border-transparent rounded-t-lg hover:border-gray-300 hover:text-gray-600 dark:hover:text-gray-300" id="general-tab" data-tabs-target="#general" type="button" role="tab" aria-controls="general" aria-selected="false" disabled>
								<i class="fas fa-sliders-h mr-2"></i>General (Coming Soon)
							</button>
						</li>
						<li role="presentation">
							<button class="inline-block p-4 border-b-2 border-transparent rounded-t-lg hover:border-gray-300 hover:text-gray-600 dark:hover:text-gray-300" id="security-tab" data-tabs-target="#security" type="button" role="tab" aria-controls="security" aria-selected="false" disabled>
								<i class="fas fa-shield-alt mr-2"></i>Security (Coming Soon)
							</button>
						</li>
					</ul>
				</div>
				<!-- Tab Content -->
				<div id="settingsTabContent">
					<!-- Notifications Tab -->
					<div class="block p-4 rounded-lg bg-white dark:bg-gray-800" id="notifications" role="tabpanel" aria-labelledby="notifications-tab">
						<div class="flex justify-between items-center mb-4">
							<h2 class="text-xl font-semibold text-gray-900 dark:text-white">Notification Services</h2>
						</div>
						<!-- Add Notification Service Form -->
						<div class="mb-6 p-6 border border-gray-200 rounded-lg shadow-sm dark:border-gray-700">
							<h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">
								Add Notification Service
							</h3>
							<form id="add-notification-form" hx-post="/admin/settings/notifications" hx-target="body">
								<div class="mb-6">
									<label for="notification_type" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Notification Type</label>
									<select id="notification_type" name="type" 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">
										<option value="">Select a type</option>
										<option value="webhook">Webhook</option>
										<option value="email" disabled>Email (Coming Soon)</option>
									</select>
								</div>
								<div class="mb-6 hidden common-fields">
									<label for="notification_name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Name</label>
									<input type="text" id="notification_name" name="name" 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" placeholder="My Notification Service" required/>
								</div>
								<div class="mb-6 hidden common-fields">
									<label for="notification_description" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Description</label>
									<textarea id="notification_description" name="description" rows="3" 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" placeholder="Description for this notification service"></textarea>
								</div>
								<!-- Dynamic fields based on notification type -->
								<div id="email_fields" class="hidden notification-fields">
									<div class="mb-6">
										<label for="smtp_host" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">SMTP Host</label>
										<input type="text" id="smtp_host" name="smtp_host" 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" placeholder="smtp.example.com"/>
									</div>
									<div class="mb-6">
										<label for="smtp_port" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">SMTP Port</label>
										<input type="number" id="smtp_port" name="smtp_port" 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" placeholder="587"/>
									</div>
									<div class="mb-6">
										<label for="smtp_username" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">SMTP Username</label>
										<input type="text" id="smtp_username" name="smtp_username" 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" placeholder="user@example.com"/>
									</div>
									<div class="mb-6">
										<label for="smtp_password" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">SMTP Password</label>
										<input type="password" id="smtp_password" name="smtp_password" 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"/>
									</div>
									<div class="mb-6">
										<label for="from_email" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">From Email</label>
										<input type="email" id="from_email" name="from_email" 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" placeholder="notifications@example.com"/>
									</div>
								</div>
								<div id="webhook_fields" class="hidden notification-fields">
									<div class="mb-6">
										<label for="webhook_url" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Webhook URL</label>
										<input type="url" id="webhook_url" name="webhook_url" 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" placeholder="https://api.example.com/webhook"/>
									</div>
									<div class="mb-6">
										<label for="method" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">HTTP Method</label>
										<select id="method" name="method" 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">
											<option value="POST">POST</option>
											<option value="PUT">PUT</option>
										</select>
									</div>
									<div class="mb-6">
										<label for="headers" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Headers (JSON)</label>
										<textarea id="headers" name="headers" rows="3" 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" placeholder='{"Content-Type": "application/json", "Authorization": "Bearer token"}'></textarea>
									</div>
									<div class="mb-6">
										<label for="payload_template" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Payload Template (JSON)</label>
										<textarea
											id="payload_template"
											name="payload_template"
											rows="5"
											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"
											placeholder='{
  "event": "{{job.event}}",
  "job": {
    "id": "{{job.id}}",
    "name": "{{job.name}}",
    "status": "{{job.status}}",
    "message": "{{job.message}}",
    "started_at": "{{job.started_at}}",
    "completed_at": "{{job.completed_at}}",
    "duration_seconds": {{job.duration_seconds}},
    "config_id": "{{job.config_id}}",
    "config_name": "{{job.config_name}}",
    "transfer_bytes": {{job.transfer_bytes}},
    "file_count": {{job.file_count}}
  },
  "instance": {
    "id": "{{instance.id}}",
    "name": "{{instance.name}}",
    "version": "{{instance.version}}",
    "environment": "{{instance.environment}}"
  },
  "timestamp": "{{timestamp}}",
  "notification_id": "{{notification.id}}"
}'
										></textarea>
										<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">Use { `variable` } placeholders for dynamic values. Available variables: job.*, instance.*, timestamp, notification.*</p>
									</div>
									<div class="mb-6">
										<label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Event Triggers</label>
										<div class="space-y-2">
											<div class="flex items-center">
												<input id="trigger_job_start" name="trigger_job_start" type="checkbox" class="w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-blue-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-blue-600 dark:ring-offset-gray-800" checked/>
												<label for="trigger_job_start" class="ml-2 text-sm font-medium text-gray-900 dark:text-white">Job Start</label>
											</div>
											<div class="flex items-center">
												<input id="trigger_job_complete" name="trigger_job_complete" type="checkbox" class="w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-blue-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-blue-600 dark:ring-offset-gray-800" checked/>
												<label for="trigger_job_complete" class="ml-2 text-sm font-medium text-gray-900 dark:text-white">Job Complete</label>
											</div>
											<div class="flex items-center">
												<input id="trigger_job_error" name="trigger_job_error" type="checkbox" class="w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-blue-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-blue-600 dark:ring-offset-gray-800" checked/>
												<label for="trigger_job_error" class="ml-2 text-sm font-medium text-gray-900 dark:text-white">Job Error</label>
											</div>
										</div>
									</div>
									<div class="mb-6">
										<label for="secret_key" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Secret Key (for signature verification)</label>
										<input type="text" id="secret_key" name="secret_key" 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" placeholder="Optional signature verification key"/>
										<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">If provided, all webhooks will include an X-GoMFT-Signature header</p>
									</div>
									<div class="mb-6">
										<label for="retry_policy" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Retry Policy</label>
										<select id="retry_policy" name="retry_policy" 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">
											<option value="none">No retries</option>
											<option value="simple" selected>Simple (3 retries)</option>
											<option value="exponential">Exponential backoff</option>
										</select>
									</div>
									<!-- Test notification button -->
									<div class="mb-6 p-4 bg-gray-50 border border-gray-200 rounded-lg dark:bg-gray-700 dark:border-gray-600">
										<div class="flex items-center justify-between mb-2">
											<h4 class="text-base font-medium text-gray-900 dark:text-white">Test Configuration</h4>
											<button
												type="button"
												id="test-webhook-btn"
												hx-post="/admin/settings/notifications/test"
												hx-trigger="click"
												hx-target="#test-notification-result"
												hx-swap="outerHTML"
												class="px-3 py-2 text-xs font-medium text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 rounded-lg dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800"
											>
												<i class="fas fa-paper-plane mr-1"></i>
												Send Test Notification
											</button>
										</div>
										<p class="text-sm text-gray-500 dark:text-gray-400">
											Send a test notification to verify your configuration works correctly before saving.
										</p>
										<div id="test-notification-result" class="mt-3 hidden">
											<!-- Result will be shown here -->
										</div>
									</div>
								</div>
								<div class="flex items-start mb-6 hidden common-fields">
									<div class="flex items-center h-5">
										<input id="is_enabled" name="is_enabled" type="checkbox" class="w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-blue-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-blue-600 dark:ring-offset-gray-800" checked/>
									</div>
									<div class="ml-3 text-sm">
										<label for="is_enabled" class="font-medium text-gray-900 dark:text-white">Enable this notification service</label>
									</div>
								</div>
								<div class="hidden common-fields">
									<button type="submit" class="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">Add Service</button>
								</div>
							</form>
						</div>
						if len(data.NotificationServices) == 0 {
							<div class="text-center py-8">
								<div class="inline-flex items-center justify-center w-16 h-16 rounded-full bg-blue-100 dark:bg-blue-900 mb-4">
									<i class="fas fa-bell text-2xl text-blue-600 dark:text-blue-400"></i>
								</div>
								<h3 class="mb-2 text-lg font-semibold text-gray-900 dark:text-white">No notification services configured</h3>
								<p class="text-gray-500 dark:text-gray-400 mb-4">Use the form above to add your first notification service.</p>
							</div>
						} else {
							<div class="bg-white border border-gray-200 rounded-lg shadow-sm dark:border-gray-700 dark:bg-gray-800 overflow-hidden">
								<ul class="divide-y divide-gray-200 dark:divide-gray-700">
									for _, service := range data.NotificationServices {
										<li>
											<div class="block hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">
												<div class="px-4 py-4 sm:px-6">
													<div class="flex items-center justify-between">
														<div class="flex items-center">
															if service.Type == "email" {
																<div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center text-blue-600 dark:bg-blue-900 dark:text-blue-400 mr-3">
																	<i class="fas fa-envelope"></i>
																</div>
															} else if service.Type == "webhook" {
																<div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center text-green-600 dark:bg-green-900 dark:text-green-400 mr-3">
																	<i class="fas fa-code"></i>
																</div>
															} else {
																<div class="w-10 h-10 rounded-full bg-gray-100 flex items-center justify-center text-gray-600 dark:bg-gray-700 dark:text-gray-400 mr-3">
																	<i class="fas fa-bell"></i>
																</div>
															}
															<div>
																<p class="text-sm font-medium text-blue-600 dark:text-blue-400 truncate">
																	{ service.Name }
																</p>
																<p class="text-sm text-gray-500 dark:text-gray-400 mt-1">
																	{ service.Description }
																</p>
															</div>
														</div>
														<div class="ml-2 flex-shrink-0 flex space-x-2">
															<button
																type="button"
																class="text-gray-500 bg-white focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-200 rounded-lg text-sm p-2 mr-1 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
																data-modal-target="edit-notification-modal"
																data-modal-toggle="edit-notification-modal"
																data-service-id={ fmt.Sprint(service.ID) }
															>
																<i class="fas fa-edit"></i>
															</button>
															<button
																type="button"
																class="text-red-500 bg-white focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-200 rounded-lg text-sm p-2 dark:bg-gray-800 dark:text-red-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
																hx-delete={ "/admin/settings/notifications/" + fmt.Sprint(service.ID) }
																hx-confirm="Are you sure you want to delete this notification service? This cannot be undone."
																hx-target="body"
															>
																<i class="fas fa-trash-alt"></i>
															</button>
														</div>
													</div>
													<div class="mt-3 sm:flex sm:justify-between">
														<div class="sm:flex flex-col md:flex-row gap-2 md:gap-6">
															<div class="flex items-center">
																<span
																	class={ "px-2 py-1 text-xs font-medium rounded-full", 
																templ.KV("bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300", service.IsEnabled),
																templ.KV("bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300", !service.IsEnabled) }
																>
																	if service.IsEnabled {
																		Active
																	} else {
																		Disabled
																	}
																</span>
																<span class="ml-2 px-2 py-1 text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300 rounded-full">
																	{ service.Type }
																</span>
																if len(service.EventTriggers) > 0 && service.Type == "webhook" {
																	<span class="ml-2 px-2 py-1 text-xs font-medium bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-300 rounded-full">
																		{ fmt.Sprintf("%d triggers", len(service.EventTriggers)) }
																	</span>
																}
																if service.SuccessCount > 0 || service.FailureCount > 0 {
																	<span class="ml-2 px-2 py-1 text-xs font-medium bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300 rounded-full">
																		{ fmt.Sprintf("%d/%d", service.SuccessCount, service.SuccessCount + service.FailureCount) }
																	</span>
																}
															</div>
														</div>
														if service.Type == "webhook" {
															<div class="mt-2 md:mt-0 flex items-center space-x-4">
																<div class="text-xs">
																	<span class="text-gray-500 dark:text-gray-400">Events:</span>
																	<span class="ml-1 text-gray-900 dark:text-gray-300">
																		if len(service.EventTriggers) == 0 {
																			None
																		} else {
																			for i, trigger := range service.EventTriggers {
																				if i > 0 {
																					<span>, </span>
																				}
																				{ trigger }
																			}
																		}
																	</span>
																</div>
																<div class="text-xs">
																	<span class="text-gray-500 dark:text-gray-400">Retry:</span>
																	<span class="ml-1 text-gray-900 dark:text-gray-300">
																		if service.RetryPolicy == "" {
																			Default
																		} else {
																			{ service.RetryPolicy }
																		}
																	</span>
																</div>
															</div>
														} else {
															<div class="mt-2 md:mt-0 flex items-center text-sm text-gray-500 dark:text-gray-400">
																<i class="far fa-clock w-4 h-4 mr-1.5 text-gray-400 dark:text-gray-500"></i>
																<p>
																	Last sent: 
																	if service.SuccessCount > 0 {
																		"Recently"
																	} else {
																		"Never"
																	}
																</p>
															</div>
														}
													</div>
												</div>
											</div>
										</li>
									}
								</ul>
							</div>
						}
					</div>
					<!-- General Tab -->
					<div class="hidden p-4 rounded-lg bg-white dark:bg-gray-800" id="general" role="tabpanel" aria-labelledby="general-tab">
						<h2 class="text-xl font-semibold text-gray-900 dark:text-white mb-4">General Settings</h2>
						<form class="space-y-6" action="/settings/general" method="POST">
							<div class="grid gap-6 mb-6 md:grid-cols-2">
								<div>
									<label for="app_name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Application Name</label>
									<input type="text" id="app_name" name="app_name" 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" value="GoMFT"/>
								</div>
								<div>
									<label for="base_url" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Base URL</label>
									<input type="url" id="base_url" name="base_url" 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" value="http://localhost:8080"/>
								</div>
							</div>
							<button type="submit" class="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">Save Settings</button>
						</form>
					</div>
					<!-- Security Tab -->
					<div class="hidden p-4 rounded-lg bg-white dark:bg-gray-800" id="security" role="tabpanel" aria-labelledby="security-tab">
						<h2 class="text-xl font-semibold text-gray-900 dark:text-white mb-4">Security Settings</h2>
						<!-- Authentication Sources -->
						<div class="mb-8">
							<div class="flex justify-between items-center mb-4">
								<h3 class="text-lg font-medium text-gray-900 dark:text-white">Authentication Sources</h3>
								<button type="button" data-modal-target="add-auth-source-modal" data-modal-toggle="add-auth-source-modal" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-4 py-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">
									<i class="fas fa-plus mr-2"></i>Add Source
								</button>
							</div>
							<div class="bg-white border border-gray-200 rounded-lg shadow-sm dark:border-gray-700 dark:bg-gray-800 overflow-hidden">
								<div class="p-4 border-b border-gray-200 dark:border-gray-700">
									<p class="text-sm text-gray-500 dark:text-gray-400">
										Configure external authentication providers to allow users to log in using existing accounts.
									</p>
								</div>
								<!-- Authentication Sources List -->
								<ul class="divide-y divide-gray-200 dark:divide-gray-700">
									<!-- Local Authentication (always present) -->
									<li>
										<div class="px-4 py-4 sm:px-6">
											<div class="flex items-center justify-between">
												<div class="flex items-center">
													<div class="w-10 h-10 rounded-full bg-gray-100 flex items-center justify-center text-gray-600 dark:bg-gray-700 dark:text-gray-400 mr-3">
														<i class="fas fa-key"></i>
													</div>
													<div>
														<p class="text-sm font-medium text-blue-600 dark:text-blue-400">
															Local Authentication
														</p>
														<p class="text-sm text-gray-500 dark:text-gray-400 mt-1">
															Default username and password authentication
														</p>
													</div>
												</div>
												<div class="ml-2 flex-shrink-0 flex">
													<span class="px-2 py-1 text-xs font-medium bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300 rounded-full">
														Active
													</span>
												</div>
											</div>
										</div>
									</li>
									<!-- Authentik SSO (if configured) -->
									<!-- This would be populated from backend data in a real implementation -->
									<li>
										<div class="px-4 py-4 sm:px-6">
											<div class="flex items-center justify-between">
												<div class="flex items-center">
													<div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center text-purple-600 dark:bg-purple-900 dark:text-purple-400 mr-3">
														<i class="fas fa-passport"></i>
													</div>
													<div>
														<p class="text-sm font-medium text-blue-600 dark:text-blue-400">
															Authentik SSO
														</p>
														<p class="text-sm text-gray-500 dark:text-gray-400 mt-1">
															Single Sign-On with Authentik Identity Provider
														</p>
													</div>
												</div>
												<div class="ml-2 flex items-center space-x-2">
													<span class="px-2 py-1 text-xs font-medium bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300 rounded-full">
														Active
													</span>
													<button
														type="button"
														class="text-gray-500 bg-white focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-200 rounded-lg text-sm p-2 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
														data-modal-target="edit-auth-source-modal"
														data-modal-toggle="edit-auth-source-modal"
													>
														<i class="fas fa-edit"></i>
													</button>
													<button
														type="button"
														class="text-red-500 bg-white focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-200 rounded-lg text-sm p-2 dark:bg-gray-800 dark:text-red-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
														hx-delete="/admin/settings/auth-sources/1"
														hx-confirm="Are you sure you want to delete this authentication source?"
														hx-target="body"
													>
														<i class="fas fa-trash-alt"></i>
													</button>
												</div>
											</div>
											<div class="mt-2 sm:flex sm:justify-between">
												<div class="sm:flex gap-4">
													<div class="flex items-center text-xs">
														<span class="text-gray-500 dark:text-gray-400">Client ID:</span>
														<span class="ml-1 text-gray-900 dark:text-gray-300">gomft_app</span>
													</div>
													<div class="flex items-center text-xs">
														<span class="text-gray-500 dark:text-gray-400">Provider URL:</span>
														<span class="ml-1 text-gray-900 dark:text-gray-300">https://auth.example.com</span>
													</div>
												</div>
											</div>
										</div>
									</li>
								</ul>
							</div>
						</div>
						<form class="space-y-6" hx-post="/admin/settings/security" hx-target="body">
							<div class="grid gap-6 mb-6 md:grid-cols-2">
								<div>
									<label for="session_timeout" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Session Timeout (minutes)</label>
									<input type="number" id="session_timeout" name="session_timeout" 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" value="60" min="5" max="1440"/>
								</div>
								<div>
									<label for="password_policy" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Password Policy</label>
									<select id="password_policy" name="password_policy" 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">
										<option value="standard">Standard (8+ chars, 1 uppercase, 1 number)</option>
										<option value="strong">Strong (12+ chars, uppercase, number, symbol)</option>
										<option value="very_strong">Very Strong (16+ chars, uppercase, number, symbol)</option>
									</select>
								</div>
							</div>
							<div class="flex items-start mb-6">
								<div class="flex items-center h-5">
									<input id="enable_2fa" name="enable_2fa" type="checkbox" class="w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-blue-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-blue-600 dark:ring-offset-gray-800" checked/>
								</div>
								<div class="ml-3 text-sm">
									<label for="enable_2fa" class="font-medium text-gray-900 dark:text-white">Enable Two-Factor Authentication</label>
									<p class="text-xs font-normal text-gray-500 dark:text-gray-400">Require 2FA for all admin users</p>
								</div>
							</div>
							<button type="submit" class="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">Save Settings</button>
						</form>
					</div>
				</div>
			</div>
		</div>
	}
	@toggleNotificationFields()
	@addAuthSourceModals()
	@setupTabSwitching()
}

// Add new templ component for auth source modals
templ addAuthSourceModals() {
	<!-- Add Authentication Source Modal -->
	<div id="add-auth-source-modal" tabindex="-1" aria-hidden="true" class="fixed top-0 left-0 right-0 z-50 hidden w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-[calc(100%-1rem)] max-h-full">
		<div class="relative w-full max-w-2xl max-h-full">
			<div class="relative bg-white rounded-lg shadow dark:bg-gray-800">
				<div class="flex items-start justify-between p-4 border-b rounded-t dark:border-gray-700">
					<h3 class="text-xl font-semibold text-gray-900 dark:text-white">
						Add Authentication Source
					</h3>
					<button type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ml-auto inline-flex justify-center items-center dark:hover:bg-gray-700 dark:hover:text-white" data-modal-hide="add-auth-source-modal">
						<i class="fas fa-times"></i>
						<span class="sr-only">Close modal</span>
					</button>
				</div>
				<form hx-post="/admin/settings/auth-sources" hx-target="body">
					<div class="p-6 space-y-6">
						<div class="mb-6">
							<label for="auth_type" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Authentication Type</label>
							<select id="auth_type" name="type" 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">
								<option value="">Select a type</option>
								<option value="authentik">Authentik SSO</option>
								<option value="oauth2">Generic OAuth 2.0</option>
								<option value="oidc">OpenID Connect</option>
								<option value="saml" disabled>SAML 2.0 (Coming Soon)</option>
							</select>
						</div>
						<div id="authentik_fields" class="auth-type-fields">
							<div class="mb-6">
								<label for="name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Name</label>
								<input type="text" id="name" name="name" 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" placeholder="Authentik SSO" required/>
							</div>
							<div class="mb-6">
								<label for="provider_url" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Authentik Provider URL</label>
								<input type="url" id="provider_url" name="provider_url" 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" placeholder="https://auth.example.com" required/>
								<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">The base URL of your Authentik instance</p>
							</div>
							<div class="grid gap-6 mb-6 md:grid-cols-2">
								<div>
									<label for="client_id" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Client ID</label>
									<input type="text" id="client_id" name="client_id" 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" placeholder="gomft_app" required/>
								</div>
								<div>
									<label for="client_secret" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Client Secret</label>
									<input type="password" id="client_secret" name="client_secret" 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" required/>
								</div>
							</div>
							<div class="mb-6">
								<label for="redirect_url" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Redirect URL</label>
								<input type="text" id="redirect_url" name="redirect_url" 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" value="http://localhost:8080/auth/callback" disabled/>
								<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">Configure this URL in your Authentik provider settings</p>
							</div>
							<div class="mb-6">
								<label for="scopes" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Scopes</label>
								<input type="text" id="scopes" name="scopes" 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" value="openid email profile" required/>
							</div>
							<div class="mb-6">
								<label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">User Attribute Mapping</label>
								<div class="grid gap-6 mb-6 md:grid-cols-2">
									<div>
										<label for="username_attribute" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Username Attribute</label>
										<input type="text" id="username_attribute" name="username_attribute" 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" value="preferred_username" required/>
									</div>
									<div>
										<label for="email_attribute" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Email Attribute</label>
										<input type="text" id="email_attribute" name="email_attribute" 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" value="email" required/>
									</div>
								</div>
								<div class="grid gap-6 mb-6 md:grid-cols-2">
									<div>
										<label for="name_attribute" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Name Attribute</label>
										<input type="text" id="name_attribute" name="name_attribute" 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" value="name" required/>
									</div>
									<div>
										<label for="groups_attribute" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Groups Attribute</label>
										<input type="text" id="groups_attribute" name="groups_attribute" 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" value="groups" required/>
									</div>
								</div>
							</div>
							<div class="flex items-start mb-6">
								<div class="flex items-center h-5">
									<input id="is_enabled" name="is_enabled" type="checkbox" class="w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-blue-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-blue-600 dark:ring-offset-gray-800" checked/>
								</div>
								<div class="ml-3 text-sm">
									<label for="is_enabled" class="font-medium text-gray-900 dark:text-white">Enable this authentication source</label>
								</div>
							</div>
							<div class="p-4 mb-6 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>
									<p>To configure Authentik for this application:</p>
									<ol class="list-decimal ml-5 mt-2">
										<li>Create a new OAuth2/OIDC Provider in Authentik</li>
										<li>Set the redirect URI to the value shown above</li>
										<li>Obtain the Client ID and Secret from Authentik</li>
										<li>Configure group mappings if needed for permission control</li>
									</ol>
								</div>
							</div>
						</div>
					</div>
					<div class="flex items-center p-6 space-x-2 border-t border-gray-200 rounded-b dark:border-gray-700">
						<button type="submit" class="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">Save</button>
						<button type="button" class="text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-blue-300 rounded-lg border border-gray-200 text-sm font-medium px-5 py-2.5 hover:text-gray-900 focus:z-10 dark:bg-gray-800 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-700 dark:focus:ring-gray-700" data-modal-hide="add-auth-source-modal">Cancel</button>
					</div>
				</form>
			</div>
		</div>
	</div>
	<!-- Edit Authentication Source Modal would be similar to the Add modal -->
}

// Script to toggle notification fields based on selection
script toggleNotificationFields() {
	document.addEventListener('DOMContentLoaded', function() {
		const typeSelector = document.getElementById('notification_type');
		const allFields = document.querySelectorAll('.notification-fields');
		const commonFields = document.querySelectorAll('.common-fields');
		
		typeSelector.addEventListener('change', function() {
			// Hide all fields first
			allFields.forEach(field => field.classList.add('hidden'));
			
			// Show/hide common fields based on selection
			const selectedType = this.value;
			if (selectedType) {
				// Show common fields (name, description)
				commonFields.forEach(field => field.classList.remove('hidden'));
				
				// Show the selected type's specific fields
				const fieldsToShow = document.getElementById(`${selectedType}_fields`);
				if (fieldsToShow) {
					fieldsToShow.classList.remove('hidden');
				}
			} else {
				// Hide common fields if no type selected
				commonFields.forEach(field => field.classList.add('hidden'));
			}
		});
	});
}

// Add script to toggle authentication source fields based on type selection
script toggleAuthSourceFields() {
	document.addEventListener('DOMContentLoaded', function() {
		const typeSelector = document.getElementById('auth_type');
		const authFields = document.querySelectorAll('.auth-type-fields');
		
		if (typeSelector) {
			typeSelector.addEventListener('change', function() {
				// Hide all auth type fields first
				authFields.forEach(field => field.classList.add('hidden'));
				
				// Show the selected type's fields
				const selectedType = this.value;
				if (selectedType) {
					const fieldsToShow = document.getElementById(`${selectedType}_fields`);
					if (fieldsToShow) {
						fieldsToShow.classList.remove('hidden');
					}
				}
			});
		}
	});
}

// Add script to handle tab switching
script setupTabSwitching() {
	document.addEventListener('DOMContentLoaded', function() {
		const tabButtons = document.querySelectorAll('[role="tab"]');
		const tabPanels = document.querySelectorAll('[role="tabpanel"]');
		
		function switchTab(targetId) {
			// Hide all tab panels
			tabPanels.forEach(panel => {
				panel.classList.add('hidden');
			});
			
			// Show the target panel
			const targetPanel = document.querySelector(targetId);
			if (targetPanel) {
				targetPanel.classList.remove('hidden');
				targetPanel.classList.add('block');
			}
			
			// Update tab button styles
			tabButtons.forEach(button => {
				// Get the target from the button
				const buttonTarget = button.getAttribute('data-tabs-target');
				
				if (buttonTarget === targetId) {
					// Active tab
					button.classList.add('border-blue-600', 'text-blue-600', 'dark:text-blue-500', 'dark:border-blue-500');
					button.classList.remove('border-transparent', 'hover:border-gray-300', 'hover:text-gray-600', 'dark:hover:text-gray-300');
					button.setAttribute('aria-selected', 'true');
				} else {
					// Inactive tab
					button.classList.remove('border-blue-600', 'text-blue-600', 'dark:text-blue-500', 'dark:border-blue-500');
					button.classList.add('border-transparent', 'hover:border-gray-300', 'hover:text-gray-600', 'dark:hover:text-gray-300');
					button.setAttribute('aria-selected', 'false');
				}
			});
		}
		
		// Add click event listeners to tab buttons
		tabButtons.forEach(button => {
			button.addEventListener('click', function(e) {
				e.preventDefault();
				const targetId = this.getAttribute('data-tabs-target');
				switchTab(targetId);
			});
		});
		
		// Initialize the tabs (select the one with aria-selected="true")
		const initialTab = document.querySelector('[role="tab"][aria-selected="true"]');
		if (initialTab) {
			const targetId = initialTab.getAttribute('data-tabs-target');
			switchTab(targetId);
		}
	});
}
