<script lang="ts">
	import ArxletCard from '$lib/components/ArxletCard.svelte';
	import ArxletHeader from '$lib/components/ArxletHeader.svelte';
	import Icon from '@iconify/svelte';
	import LiquidGlass from '$lib/components/LiquidGlass.svelte';
	import type { Arxlet } from '$lib/eve';
	import { onMount } from 'svelte';
	import Loading from '$lib/components/PublicComponents/Loading.svelte';

	const arxlets: Arxlet[] = $state([]);

	let searchQuery = $state('');

	const filteredArxlets = $derived(() => {
		let filtered = [...arxlets];

		if (searchQuery.trim()) {
			const query = searchQuery.toLowerCase();
			filtered = filtered.filter(
				(arxlet) =>
					arxlet.name.toLowerCase().includes(query) ||
					arxlet.description?.toLowerCase()?.includes(query)
			);
		}

		filtered.sort((a, b) => a.name.localeCompare(b.name));

		return filtered;
	});

	function clearFilters() {
		searchQuery = '';
	}

	onMount(() => {
		window.eve.getRemoteArxlets().then((remoteArxlets) => arxlets.push(...remoteArxlets));
	});
</script>

<svelte:head>
	<title>Arxlet Store</title>
</svelte:head>

<ArxletHeader
	title="Arxlet Store"
	subtitle="Browse and install arxlets"
	icon="mdi:storefront"
	iconColor="#ff0000"
/>

<LiquidGlass>
	<div class="min-h-screen">
		<div class="container mx-auto px-6 py-8">
			<div class="flex flex-col lg:flex-row gap-6 mb-8">
				<div class="flex-1">
					<div class="form-control">
						<div class="input-group">
							<input
								bind:value={searchQuery}
								type="text"
								placeholder="Search arxlets..."
								class="input input-bordered w-full"
							/>
						</div>
					</div>
				</div>

				<button
					onclick={clearFilters}
					class="btn btn-outline btn-secondary"
					title="Clear all filters"
				>
					<Icon icon="mdi:filter-off" class="w-5 h-5" />
					Clear
				</button>
			</div>

			<div class="flex justify-between items-center mb-6">
				<h2 class="text-2xl font-semibold text-base-content">
					{#if searchQuery}
						{filteredArxlets().length}
						{filteredArxlets().length === 1 ? 'Arxlet' : 'Arxlets'} Found
					{/if}
				</h2>

				{#if searchQuery}
					<div class="text-sm text-base-content/60">
						<span class="badge badge-outline badge-sm mr-2">
							Search: "{searchQuery}"
						</span>
					</div>
				{/if}
			</div>

			{#if filteredArxlets().length > 0}
				<div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-6">
					{#each filteredArxlets() as arxlet (arxlet.eventId)}
						<ArxletCard {arxlet} />
					{/each}
				</div>
			{:else if searchQuery}
				<div class="text-center py-16">
					<Icon icon="mdi:package-variant-closed" class="w-24 h-24 text-base-300 mx-auto mb-4" />
					<h3 class="text-2xl font-semibold text-base-content/60 mb-2">No Arxlets Found</h3>
					<p class="text-base-content/40 mb-6">
						Try adjusting your search terms or filters to find what you're looking for.
					</p>
					<button onclick={clearFilters} class="btn btn-primary">
						<Icon icon="mdi:refresh" class="w-5 h-5 mr-2" />
						Reset Filters
					</button>
				</div>
			{:else}
				<Loading size="xl" />
			{/if}
		</div>
	</div>
</LiquidGlass>
