package components

import (
	"context"
	"github.com/starfleetcptn/gomft/internal/db"
	"strconv"
)

type UsersData struct {
	Users []db.User
}

templ Users(ctx context.Context, data UsersData) {
	@LayoutWithContext("User Management", ctx) {
		<div class="p-4 md:p-6 2xl:p-10">
			<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-users w-6 h-6 mr-2 text-blue-500"></i>
					User Management
				</h1>
				<a href="/admin/users/new" class="flex items-center justify-center text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg px-5 py-2.5 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">
					<i class="fas fa-user-plus w-4 h-4 mr-2"></i>
					Add User
				</a>
			</div>
			
			<div class="mt-6">
				if len(data.Users) == 0 {
					<div class="flex p-4 mb-4 text-sm text-yellow-800 rounded-lg bg-yellow-50 dark:bg-yellow-900 dark:text-yellow-300" role="alert">
						<i class="fas fa-exclamation-triangle flex-shrink-0 inline w-5 h-5 me-3"></i>
						<span class="sr-only">Info</span>
						<div>
							<span class="font-medium">No users found!</span> Click the "Add User" button to create your first user.
						</div>
					</div>
				} else {
					<div class="bg-white border border-gray-200 rounded-lg shadow-sm dark:border-gray-700 dark:bg-gray-800 overflow-hidden">
						<div class="overflow-x-auto">
							<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
								<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
									<tr>
										<th scope="col" class="px-6 py-3">
											<div class="flex items-center">
												<i class="fas fa-envelope w-4 h-4 mr-1"></i>
												Email
											</div>
										</th>
										<th scope="col" class="px-6 py-3">
											<div class="flex items-center">
												<i class="fas fa-user-tag w-4 h-4 mr-1"></i>
												Role
											</div>
										</th>
										<th scope="col" class="px-6 py-3">
											<div class="flex items-center">
												<i class="fas fa-calendar-plus w-4 h-4 mr-1"></i>
												Created
											</div>
										</th>
										<th scope="col" class="px-6 py-3">
											<span class="sr-only">Actions</span>
										</th>
									</tr>
								</thead>
								<tbody>
									if len(data.Users) == 0 {
										<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
											<td colspan="4" class="px-6 py-4 whitespace-nowrap text-center text-sm text-gray-500 dark:text-gray-400">
												No users found
											</td>
										</tr>
									} else {
										for _, user := range data.Users {
											<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600">
												<td class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
													{ user.Email }
												</td>
												<td class="px-6 py-4">
													if user.GetIsAdmin() {
														<span class="bg-blue-100 text-blue-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-full dark:bg-blue-900 dark:text-blue-300">
															<i class="fas fa-user-shield w-3 h-3 mr-1"></i> Admin
														</span>
													} else {
														<span class="bg-gray-100 text-gray-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-full dark:bg-gray-700 dark:text-gray-300">
															<i class="fas fa-user w-3 h-3 mr-1"></i> User
														</span>
													}
												</td>
												<td class="px-6 py-4">
													{ user.CreatedAt.Format("Jan 02, 2006") }
												</td>
												<td class="px-6 py-4 text-right">
													<a
														href={ templ.SafeURL("/admin/users/" + strconv.Itoa(int(user.ID)) + "/edit") }
														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-3 py-1.5 text-center inline-flex items-center me-2 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
													>
														<i class="fas fa-edit w-3.5 h-3.5 mr-1.5"></i> Edit
													</a>
													
													<button
														class="text-white bg-red-700 hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 font-medium rounded-lg text-sm px-3 py-1.5 text-center inline-flex items-center dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-800"
														hx-delete={ "/admin/users/" + strconv.Itoa(int(user.ID)) }
														hx-confirm="Are you sure you want to delete this user? This action cannot be undone."
														hx-target="body"
													>
														<i class="fas fa-trash-alt w-3.5 h-3.5 mr-1.5"></i> Delete
													</button>
												</td>
											</tr>
										}
									}
								</tbody>
							</table>
						</div>
					</div>
				}
			</div>
			
			<!-- Help Notice -->
			<div class="mt-8">
				<div class="flex p-4 text-sm text-blue-800 rounded-lg bg-blue-50 dark:bg-blue-900 dark:text-blue-400" role="alert">
					<i class="fas fa-shield-alt flex-shrink-0 inline w-4 h-4 me-3 mt-0.5"></i>
					<span class="sr-only">Info</span>
					<div>
						User accounts provide secure access to the GoMFT application with role-based permissions
					</div>
				</div>
			</div>
		</div>
	}
}

type UserFormData struct {
	IsNew bool
	ErrorMessage string
}

templ UserForm(ctx context.Context, data UserFormData) {
	@LayoutWithContext("Add User", ctx) {
		<div class="p-4 md:p-6 2xl:p-10">
			<div class="max-w-3xl mx-auto">
				<div class="mb-8">
					<h1 class="text-2xl font-bold text-gray-900 dark:text-white flex items-center">
						<i class="fas fa-user-plus w-6 h-6 mr-2 text-blue-500"></i>
						Add New User
					</h1>
				</div>
				
				if data.ErrorMessage != "" {
					<div class="mb-6 p-4 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>
				}
				
				<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-6">
						<form 
							class="space-y-6" 
							action="/admin/users/new" 
							method="POST"
							x-data="{ 
								password: '',
								confirmPassword: '',
								isAdmin: false,
								passwordsMatch() {
									return this.password === this.confirmPassword || this.confirmPassword === '';
								}
							}"
						>
							<div>
								<div class="mb-6">
									<h3 class="text-lg font-medium leading-none text-gray-900 dark:text-white">User Information</h3>
									<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">Create a new user account with appropriate permissions.</p>
								</div>
								
								<div class="space-y-4">
									<div>
										<label for="email" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Email</label>
										<input 
											type="email" 
											name="email" 
											id="email" 
											required 
											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>
										<label for="password" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Password</label>
										<input 
											type="password" 
											name="password" 
											id="password" 
											required 
											x-model="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" 
											placeholder="••••••••"
										/>
									</div>
									
									<div>
										<label for="password_confirm" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Confirm Password</label>
										<input 
											type="password" 
											name="password_confirm" 
											id="password_confirm" 
											required
											x-model="confirmPassword"
											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="••••••••" 
										/>
										<p class="mt-1 text-sm text-red-600 dark:text-red-500" x-show="!passwordsMatch() && confirmPassword !== ''">
											Passwords do not match
										</p>
									</div>
									
									<div class="pt-2">
										<div class="flex items-start">
											<div class="flex items-center h-5">
												<input 
													id="is_admin" 
													name="is_admin" 
													type="checkbox" 
													x-model="isAdmin" 
													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 dark:focus:ring-offset-gray-800" 
												/>
											</div>
											<div class="ml-3 text-sm">
												<label for="is_admin" class="font-medium text-gray-900 dark:text-white">Administrator</label>
												<p class="text-xs font-normal text-gray-500 dark:text-gray-400">Grant administrative privileges to this user</p>
											</div>
										</div>
									</div>
								</div>
							</div>
							
							<div class="flex items-center justify-between pt-4 border-t border-gray-200 dark:border-gray-700">
								<a href="/admin/users" class="text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-gray-200 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-700 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-600 dark:focus:ring-gray-600">
									<i class="fas fa-arrow-left mr-2"></i>
									Cancel
								</a>
								<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"
									x-bind:disabled="!passwordsMatch() && confirmPassword !== ''"
								>
									<i class="fas fa-save mr-2"></i>
									Create User
								</button>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	}
} 