package app

import (
	"fmt"
	"net/url"

	"github.com/seaweedfs/seaweedfs/weed/admin/dash"
)

templ IcebergNamespaces(data dash.IcebergNamespacesData) {
	<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
		<h1 class="h2">
			<nav aria-label="breadcrumb">
				<ol class="breadcrumb mb-0">
					<li class="breadcrumb-item">
						<a href={ dash.PUrl(ctx, "/object-store/iceberg") }>
							<i class="fas fa-snowflake me-1"></i>Iceberg Catalog
						</a>
					</li>
					<li class="breadcrumb-item active">{ data.CatalogName }</li>
				</ol>
			</nav>
		</h1>
		<div class="btn-toolbar mb-2 mb-md-0">
			<div class="btn-group me-2">
				<button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#createIcebergNamespaceModal">
					<i class="fas fa-plus me-1"></i>Create Namespace
				</button>
			</div>
		</div>
	</div>
	<div class="mb-3">
		<span class="text-muted">Bucket ARN: { data.BucketARN }</span>
	</div>
	<div id="iceberg-namespaces-content" data-bucket-arn={ data.BucketARN } data-catalog-name={ data.CatalogName }>
		<!-- Stats Cards -->
		<div class="row mb-4">
			<div class="col-xl-4 col-md-6 mb-4">
				<div class="card border-left-primary shadow h-100 py-2">
					<div class="card-body">
						<div class="row no-gutters align-items-center">
							<div class="col mr-2">
								<div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
									Catalog
								</div>
								<div class="h5 mb-0 font-weight-bold text-gray-800">
									{ data.CatalogName }
								</div>
							</div>
							<div class="col-auto">
								<i class="fas fa-snowflake fa-2x text-gray-300"></i>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="col-xl-4 col-md-6 mb-4">
				<div class="card border-left-success shadow h-100 py-2">
					<div class="card-body">
						<div class="row no-gutters align-items-center">
							<div class="col mr-2">
								<div class="text-xs font-weight-bold text-success text-uppercase mb-1">
									Total Namespaces
								</div>
								<div class="h5 mb-0 font-weight-bold text-gray-800">
									{ fmt.Sprintf("%d", data.TotalNamespaces) }
								</div>
							</div>
							<div class="col-auto">
								<i class="fas fa-folder fa-2x text-gray-300"></i>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>

		<!-- Catalog Browser Tree -->
		<div class="row">
			<div class="col-12">
				<div class="card shadow mb-4">
					<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
						<h6 class="m-0 font-weight-bold text-primary">
							<i class="fas fa-sitemap me-2"></i>Catalog Browser
						</h6>
					</div>
					<div class="card-body">
						<ul class="list-group" id="icebergNamespaceTree">
							for idx, ns := range data.Namespaces {
								<li class="list-group-item">
									{{ namespaceID := fmt.Sprintf("iceberg-namespace-%d", idx) }}
									<div class="d-flex align-items-center justify-content-between">
										<button class="btn btn-link p-0 text-decoration-none iceberg-namespace-toggle" type="button" data-bs-toggle="collapse" data-bs-target={ "#" + namespaceID } aria-expanded="false" aria-controls={ namespaceID }>
											<i class="fas fa-folder text-warning me-2"></i>{ ns.Name }
										</button>
										<a class="btn btn-sm btn-outline-primary" href={ dash.PUrl(ctx, fmt.Sprintf("/object-store/iceberg/%s/namespaces/%s/tables", url.PathEscape(data.CatalogName), url.PathEscape(ns.Name))) }>
											<i class="fas fa-table me-1"></i>Open
										</a>
									</div>
									<div class="collapse mt-2 iceberg-namespace-collapse" id={ namespaceID } data-namespace={ ns.Name } data-catalog-name={ data.CatalogName }>
										<div class="text-muted small">Loading...</div>
									</div>
								</li>
							}
							if len(data.Namespaces) == 0 {
								<li class="list-group-item text-center text-muted py-4">
									<i class="fas fa-folder-open fa-2x mb-2 text-muted"></i>
									<div>No namespaces available.</div>
								</li>
							}
						</ul>
					</div>
				</div>
			</div>
		</div>

		<!-- Namespace List -->
		<div class="row">
			<div class="col-12">
				<div class="card shadow mb-4">
					<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
						<h6 class="m-0 font-weight-bold text-primary">
							<i class="fas fa-folder me-2"></i>Namespaces
						</h6>
					</div>
					<div class="card-body">
						<div class="table-responsive">
							<table class="table table-hover" width="100%" cellspacing="0">
								<thead>
									<tr>
										<th>Namespace</th>
										<th>REST Endpoint</th>
										<th>Created</th>
										<th>Actions</th>
									</tr>
								</thead>
								<tbody>
									for _, ns := range data.Namespaces {
										<tr>
											<td>
												<i class="fas fa-folder text-warning me-2"></i>
												<strong>{ ns.Name }</strong>
											</td>
											<td>
												<code class="small">/v1/{ data.CatalogName }/namespaces/{ ns.Name }/tables</code>
											</td>
											<td>
												if ns.CreatedAt.IsZero() {
													<span class="text-muted">-</span>
												} else {
													{ ns.CreatedAt.Format("2006-01-02 15:04") }
												}
											</td>
											<td>
												<div class="btn-group btn-group-sm" role="group">
													<a class="btn btn-outline-primary btn-sm" href={ dash.PUrl(ctx, fmt.Sprintf("/object-store/iceberg/%s/namespaces/%s/tables", url.PathEscape(data.CatalogName), url.PathEscape(ns.Name))) } title="Browse Tables">
														<i class="fas fa-table"></i>
													</a>
												</div>
											</td>
										</tr>
									}
									if len(data.Namespaces) == 0 {
										<tr>
											<td colspan="4" class="text-center text-muted py-4">
												<i class="fas fa-folder-open fa-3x mb-3 text-muted"></i>
												<div>
													<h5>No namespaces found</h5>
													<p>Create namespaces via the Iceberg REST API or S3 Tables API.</p>
												</div>
											</td>
										</tr>
									}
								</tbody>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="modal fade" id="createIcebergNamespaceModal" tabindex="-1" aria-labelledby="createIcebergNamespaceModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title" id="createIcebergNamespaceModalLabel">
						<i class="fas fa-plus me-2"></i>Create Namespace
					</h5>
					<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
				</div>
				<form id="createIcebergNamespaceForm">
					<div class="modal-body">
						<input type="hidden" id="icebergNamespaceBucketArn" name="bucket_arn" value={ data.BucketARN }/>
						<input type="hidden" id="icebergNamespaceCsrfToken" name="csrf_token"/>
						<div class="mb-3">
							<label for="icebergNamespaceName" class="form-label">Namespace</label>
							<input type="text" class="form-control" id="icebergNamespaceName" name="name" placeholder="analytics" required/>
							<div class="form-text">Use lowercase letters, numbers, and underscores. Use dots for nested namespaces (for example, analytics.daily).</div>
						</div>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
						<button type="submit" class="btn btn-primary">
							<i class="fas fa-plus me-1"></i>Create
						</button>
					</div>
				</form>
			</div>
		</div>
	</div>
	<script>
		document.addEventListener('DOMContentLoaded', function() {
			initIcebergNamespaces();
		});
	</script>
}
