<script lang="ts">
	import type { PageData } from './$types';
	import ContentContainer from '$lib/components/layout/ContentContainer.svelte';
	import CustomerForm from '$lib/components/admin/customers/CustomerForm.svelte';
	import { offCanvas } from '$lib/stores/offCanvas.svelte';
	import { invalidateAll, goto } from '$app/navigation';
	import { page } from '$app/state';

	let { data }: { data: PageData } = $props();

	// Auto-open create form if action=create is in URL
	$effect(() => {
		if (page.url.searchParams.get('action') === 'create') {
			showCreateForm();
			// Remove the action param from URL without navigation
			goto('/admin/customers', { replaceState: true, noScroll: true });
		}
	});

	let customers = $derived(data.customers?.data?.customers ?? []);
	let searchQuery = $state('');

	let filteredCustomers = $derived(
		customers.filter((c) => c.name.toLowerCase().includes(searchQuery.toLowerCase()))
	);

	function showCreateForm() {
		offCanvas.showRight({
			title: 'Create Customer',
			content: createFormContent
		});
	}

	async function handleSuccess() {
		await invalidateAll();
	}
</script>

{#snippet createFormContent()}
	<CustomerForm onSuccess={handleSuccess} />
{/snippet}

<svelte:head>
	<title>Customers - Admin - Nexus</title>
</svelte:head>

<div class="py-8">
	<ContentContainer>
		<div class="mb-8">
			<div class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
				<div class="flex items-start gap-4">
					<a
						href="/admin"
						class="rounded-lg p-1 text-theme-muted hover:bg-black/5 hover:text-theme dark:hover:bg-white/10"
						aria-label="Go back"
					>
						<svg class="h-5 w-5" style="fill: none" stroke="currentColor" viewBox="0 0 24 24">
							<path
								stroke-linecap="round"
								stroke-linejoin="round"
								stroke-width="2"
								d="M15 19l-7-7 7-7"
							/>
						</svg>
					</a>
					<div>
						<h1 class="text-2xl font-bold text-accent3-600 md:text-3xl dark:text-accent3-400">
							Customers
						</h1>
						<p class="mt-2 text-theme-secondary">
							Manage customer profiles, contacts, and billing information.
						</p>
					</div>
				</div>
				<button
					type="button"
					onclick={showCreateForm}
					class="hidden items-center gap-2 rounded-lg bg-primary-500 px-4 py-2 text-white transition-colors hover:bg-primary-600 sm:inline-flex"
				>
					<svg class="h-5 w-5" style="fill: none" stroke="currentColor" viewBox="0 0 24 24">
						<path
							stroke-linecap="round"
							stroke-linejoin="round"
							stroke-width="2"
							d="M12 4v16m8-8H4"
						/>
					</svg>
					<span>Add Customer</span>
				</button>
			</div>
		</div>

		<!-- Search -->
		<div class="mb-6">
			<div class="relative">
				<svg
					class="absolute top-1/2 left-3 h-5 w-5 -translate-y-1/2 text-theme-muted"
					style="fill: none"
					stroke="currentColor"
					viewBox="0 0 24 24"
				>
					<path
						stroke-linecap="round"
						stroke-linejoin="round"
						stroke-width="2"
						d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
					/>
				</svg>
				<input
					type="text"
					bind:value={searchQuery}
					placeholder="Search customers..."
					class="placeholder-theme-muted w-full rounded-lg border border-theme bg-theme-card py-2 pr-4 pl-10 text-theme focus:border-primary-500 focus:ring-1 focus:ring-primary-500"
				/>
			</div>
		</div>

		{#if filteredCustomers.length > 0}
			<div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
				{#each filteredCustomers as customer (customer.id)}
					<a
						href="/admin/customers/{customer.id}"
						class="flex items-center justify-between card-customer"
					>
						<div>
							<h2 class="text-lg font-semibold text-theme">{customer.name}</h2>
							<div class="flex items-center gap-2">
								{#if customer.accounts && customer.accounts.length > 0}
									<p class="text-sm text-theme-secondary">
										{customer.accounts.length} account{customer.accounts.length === 1 ? '' : 's'}
									</p>
								{:else}
									<p class="text-sm text-theme-muted">No accounts</p>
								{/if}
								{#if customer.waveCustomerId}
									<span class="badge-primary">Linked</span>
								{/if}
							</div>
						</div>
						<svg
							class="h-5 w-5 flex-shrink-0 text-theme-muted"
							style="fill: none"
							stroke="currentColor"
							viewBox="0 0 24 24"
						>
							<path
								stroke-linecap="round"
								stroke-linejoin="round"
								stroke-width="2"
								d="M9 5l7 7-7 7"
							/>
						</svg>
					</a>
				{/each}
			</div>
		{:else if searchQuery}
			<div class="empty-state">
				<svg class="empty-state-icon" style="fill: none" stroke="currentColor" viewBox="0 0 24 24">
					<path
						stroke-linecap="round"
						stroke-linejoin="round"
						stroke-width="1.5"
						d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
					/>
				</svg>
				<h2 class="empty-state-title">No customers found</h2>
				<p class="empty-state-text">
					No customers match "{searchQuery}". Try a different search term.
				</p>
			</div>
		{:else}
			<div class="empty-state">
				<svg class="empty-state-icon" style="fill: none" stroke="currentColor" viewBox="0 0 24 24">
					<path
						stroke-linecap="round"
						stroke-linejoin="round"
						stroke-width="1.5"
						d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"
					/>
				</svg>
				<h2 class="empty-state-title">No customers yet</h2>
				<p class="empty-state-text">
					Get started by adding your first customer. Customers can be linked to service accounts.
				</p>
				<button
					type="button"
					onclick={showCreateForm}
					class="mt-4 inline-flex items-center gap-2 rounded-lg bg-primary-500 px-4 py-2 text-white transition-colors hover:bg-primary-600"
				>
					<svg class="h-5 w-5" style="fill: none" stroke="currentColor" viewBox="0 0 24 24">
						<path
							stroke-linecap="round"
							stroke-linejoin="round"
							stroke-width="2"
							d="M12 4v16m8-8H4"
						/>
					</svg>
					<span>Add Customer</span>
				</button>
			</div>
		{/if}
	</ContentContainer>
</div>

<!-- Mobile FAB -->
<button
	type="button"
	onclick={showCreateForm}
	class="fixed right-4 bottom-20 z-30 flex h-14 w-14 items-center justify-center rounded-full bg-primary-500 text-white shadow-lg transition-colors hover:bg-primary-600 sm:hidden"
	aria-label="Add customer"
>
	<svg class="h-6 w-6" style="fill: none" stroke="currentColor" viewBox="0 0 24 24">
		<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
	</svg>
</button>
