<!-- #region GitDashboardPage [C:3] [TYPE Page] [SEMANTICS sveltekit,git,settings,provider,integration] -->
<!-- @BRIEF Thin rendering layer for Git configuration management. -->
<!-- @LAYER Page -->
<!-- @RELATION BINDS_TO -> [GitConfigModel] -->
<!-- @INVARIANT This component contains NO business logic — all logic is in GitConfigModel. -->
<!-- @UX_STATE Loading -> Configs fetched on mount; list renders on data. -->
<!-- @UX_STATE Editing -> Configuration form supports create and edit flows. -->
<!-- @UX_STATE Testing -> Connection validation feedback shown before save. -->
<!-- @UX_STATE Error -> Toast feedback reports failures while preserving form values. -->

<script lang="ts">
	import { onMount } from 'svelte';
	import { t } from '$lib/i18n/index.svelte.js';
	import { Button, Input, Card, PageHeader, Select } from '$lib/ui';
	import HelpTooltip from '$lib/ui/HelpTooltip.svelte';
	import { GitConfigModel } from '$lib/models/GitConfigModel.svelte.ts';

	const model = new GitConfigModel();

	onMount(() => { model.loadConfigs(); });

	$effect(() => {
		if (model.selectedGiteaConfigId) void model.loadGiteaRepos();
	});
</script>

<div class="p-6 max-w-6xl mx-auto">
	<PageHeader title={$t.nav?.settings_git} />

	<div class="grid grid-cols-1 md:grid-cols-2 gap-8">
		<Card title={$t.settings?.configured_servers}>
			{#if model.configs.length === 0}
				<p class="text-gray-500">{$t.git?.no_servers_configured}</p>
			{:else}
				<ul class="divide-y divide-gray-100">
					{#each model.configs as config}
						<li class="py-4 flex justify-between items-center">
							<div>
								<div class="flex items-center gap-2">
									<span class="font-medium text-gray-900">{config.name}</span>
									<span class="text-xs font-mono bg-gray-50 text-gray-500 px-1.5 py-0.5 rounded">{config.provider}</span>
								</div>
								<div class="text-xs text-gray-400 mt-1">{config.url}</div>
							</div>
							<div class="flex items-center space-x-4">
								<span class="px-2 py-1 text-xs font-medium rounded {config.status === 'CONNECTED' ? 'bg-green-50 text-green-700' : 'bg-red-50 text-red-700'}">{config.status}</span>
								<button onclick={() => { model.startEdit(config.id); window.scrollTo({ top: 0, behavior: 'smooth' }); }} class="text-gray-400 hover:text-blue-600 transition-colors" title={$t.common?.edit || "Edit"}>
									<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor"><path d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z"/></svg>
								</button>
								<button onclick={() => { if (confirm($t.settings?.git_delete_confirm)) model.deleteConfig(config.id); }} class="text-gray-400 hover:text-red-600 transition-colors" title={$t.common?.delete}>
									<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z" clip-rule="evenodd"/></svg>
								</button>
							</div>
						</li>
					{/each}
				</ul>
			{/if}
		</Card>

		<Card title={model.isEditing ? ($t.settings?.edit_git_server || "Edit Git server") : $t.settings?.add_git_server}>
			<div class="space-y-6">
				<div>
					<span class="flex items-center gap-1.5 mb-1.5">
						<span class="text-sm font-medium text-gray-700">{$t.settings?.display_name}</span>
						<HelpTooltip text={$t.settings?.help_git_display_name || ""} />
					</span>
					<Input aria-label={$t.settings?.display_name} bind:value={model.formData.name} placeholder={$t.settings?.display_name_placeholder} />
				</div>
				<div>
					<span class="flex items-center gap-1.5 mb-1.5">
						<span class="text-sm font-medium text-gray-700">{$t.llm?.type}</span>
						<HelpTooltip text={$t.settings?.help_git_provider || ""} />
					</span>
					<Select aria-label={$t.llm?.type} bind:value={model.formData.provider} options={[{ value: "GITHUB", label: "GitHub" }, { value: "GITLAB", label: "GitLab" }, { value: "GITEA", label: "Gitea" }]} />
				</div>
				<div>
					<span class="flex items-center gap-1.5 mb-1.5">
						<span class="text-sm font-medium text-gray-700">{$t.settings?.server_url}</span>
						<HelpTooltip text={$t.settings?.help_git_server_url || ""} />
					</span>
					<Input aria-label={$t.settings?.server_url} bind:value={model.formData.url} />
				</div>
				<div>
					<span class="flex items-center gap-1.5 mb-1.5">
						<span class="text-sm font-medium text-gray-700">{$t.settings?.personal_access_token}</span>
						<HelpTooltip text={$t.settings?.help_git_pat || ""} />
					</span>
					<Input aria-label={$t.settings?.personal_access_token} type="password" bind:value={model.formData.pat} />
				</div>
				<div>
					<span class="flex items-center gap-1.5 mb-1.5">
						<span class="text-sm font-medium text-gray-700">{$t.settings?.default_repository_optional}</span>
						<HelpTooltip text={$t.settings?.help_git_default_repo || ""} />
					</span>
					<Input aria-label={$t.settings?.default_repository_optional} bind:value={model.formData.default_repository} placeholder="org/repo" />
				</div>
				<div>
					<span class="flex items-center gap-1.5 mb-1.5">
						<span class="text-sm font-medium text-gray-700">{$t.settings?.default_branch || "Default branch"}</span>
						<HelpTooltip text={$t.settings?.help_git_default_branch || ""} />
					</span>
					<Input aria-label={$t.settings?.default_branch || "Default branch"} bind:value={model.formData.default_branch} placeholder="main" />
				</div>
				<div class="flex gap-3 pt-2">
					<Button variant="secondary" onclick={() => model.testConnection()} isLoading={model.testingConnection}>{$t.llm?.test}</Button>
					<Button variant="primary" onclick={() => model.saveConfig()} isLoading={model.saving}>{model.isEditing ? ($t.settings?.update_configuration || "Update configuration") : $t.settings?.save_configuration}</Button>
					{#if model.isEditing}
						<Button variant="secondary" onclick={() => model.cancelEdit()}>{$t.common?.cancel || "Cancel"}</Button>
					{/if}
				</div>
			</div>
		</Card>
	</div>

	<div class="mt-8">
		<Card title="Gitea repositories">
			<div class="space-y-4">
				<Select label="Gitea server config" bind:value={model.selectedGiteaConfigId} options={[{ value: "", label: "Select config" }, ...model.configs.filter((c) => c.provider === "GITEA").map((c) => ({ value: c.id, label: `${c.name} (${c.url})` }))]} />
				{#if model.selectedGiteaConfigId}
					<div class="grid grid-cols-1 md:grid-cols-2 gap-3">
						<Input label="Repository name" bind:value={model.newGiteaRepo.name} placeholder="dashboard-configs" />
						<Input label="Default branch" bind:value={model.newGiteaRepo.default_branch} placeholder="main" />
						<Input class="md:col-span-2" label="Description" bind:value={model.newGiteaRepo.description} placeholder="Repository for Superset dashboards" />
					</div>
					<div class="flex gap-3">
						<Button variant="primary" onclick={() => model.createRemoteRepo()} isLoading={model.giteaCreating} disabled={!model.newGiteaRepo.name.trim()}>Create repository</Button>
						<Button variant="secondary" onclick={() => model.loadGiteaRepos()} isLoading={model.giteaReposLoading}>Reload repositories</Button>
					</div>
					{#if model.giteaRepos.length === 0 && !model.giteaReposLoading}
						<p class="text-sm text-gray-500">No repositories found for this Gitea user.</p>
					{:else}
						<div class="divide-y divide-gray-100 border border-gray-200 rounded-lg">
							{#each model.giteaRepos as repo}
								<div class="p-3 flex items-center justify-between gap-4">
									<div class="min-w-0">
										<div class="font-medium text-gray-900 truncate">{repo.full_name || repo.name}</div>
										<div class="text-xs text-gray-500 truncate">{repo.clone_url || repo.html_url}</div>
									</div>
									<Button variant="danger" size="sm" onclick={() => { if (confirm(`Delete repository ${repo.full_name || repo.name}?`)) model.deleteRemoteRepo(repo); }} disabled={model.giteaDeleting}>Delete</Button>
								</div>
							{/each}
						</div>
					{/if}
				{/if}
			</div>
		</Card>
	</div>
</div>
<!-- #endregion GitDashboardPage -->
