<script>
	import { page } from '$app/stores';
	import { authStore, currentUser, userRole } from '$stores/auth.js';
	import { hasRole } from '$utils/helpers.js';

	let { collapsed = false, mobileOpen = false, onclose = () => {} } = $props();
	let user = null;
	let role = '';

	currentUser.subscribe((v) => (user = v));
	userRole.subscribe((v) => (role = v));

	const navItems = [
		{ href: '/dashboard', label: 'Dashboard', icon: 'M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6', roles: ['admin', 'manager'] },
		{ href: '/pos', label: 'POS Billing', icon: 'M9 7h6m0 10v-3m-3 3h.01M9 17h.01M9 14h.01M12 14h.01M15 11h.01M12 11h.01M9 11h.01M7 21h10a2 2 0 002-2V5a2 2 0 00-2-2H7a2 2 0 00-2 2v14a2 2 0 002 2z', roles: ['admin', 'manager', 'cashier'] },
		{ href: '/tables', label: 'Tables', icon: 'M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zm10 0a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zm10 0a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z', roles: ['admin', 'manager', 'cashier', 'captain'] },
		{ href: '/captain', label: 'Captain', icon: 'M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z', roles: ['admin', 'manager', 'captain'] },
		{ href: '/kds', label: 'Kitchen Display', icon: 'M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10', roles: ['admin', 'manager', 'kitchen'] },
		{ href: '/online-orders', label: 'Online Orders', icon: 'M3.75 3v11.25A2.25 2.25 0 006 16.5h2.25M3.75 3h-1.5m1.5 0h16.5m0 0h1.5m-1.5 0v11.25A2.25 2.25 0 0118 16.5h-2.25m-7.5 0h7.5m-7.5 0l-1 3m8.5-3l1 3m0 0l.5 1.5m-.5-1.5h-9.5m0 0l-.5 1.5', roles: ['admin', 'manager', 'cashier'] },
		{ type: 'divider', roles: ['admin', 'manager'] },
		{ href: '/orders', label: 'Orders', icon: 'M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01', roles: ['admin', 'manager', 'cashier', 'accountant'] },
		{ href: '/reports', label: 'Reports', icon: 'M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z', roles: ['admin', 'manager', 'accountant'] },
		{ href: '/admin', label: 'Settings', icon: 'M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.066 2.573c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.573 1.066c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.066-2.573c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z', roles: ['admin'] },
	];

	let currentPath = '';
	page.subscribe((p) => (currentPath = p.url?.pathname || ''));

	function visibleItems() {
		return navItems.filter((item) => {
			if (!item.roles) return true;
			return item.roles.some((r) => r === role);
		});
	}

	function handleNavClick() {
		// Close drawer on mobile after navigation
		if (mobileOpen) onclose();
	}
</script>

<!-- Mobile backdrop overlay -->
{#if mobileOpen}
	<!-- svelte-ignore a11y_click_events_have_key_events -->
	<!-- svelte-ignore a11y_no_static_element_interactions -->
	<div class="mobile-overlay" onclick={onclose}></div>
{/if}

<aside
	class="h-screen bg-white border-r border-gray-200 flex flex-col transition-all duration-300 flex-shrink-0
		{collapsed ? 'w-16' : 'w-56'}
		max-lg:fixed max-lg:inset-y-0 max-lg:left-0 max-lg:z-50 max-lg:w-64 max-lg:shadow-xl
		{mobileOpen ? 'max-lg:translate-x-0' : 'max-lg:-translate-x-full'}
	"
>
	<!-- Logo -->
	<div class="px-4 py-5 border-b border-gray-200 flex items-center gap-3">
		<div class="w-8 h-8 rounded-lg bg-pos-highlight flex items-center justify-center flex-shrink-0">
			<span class="text-white font-bold text-sm">H</span>
		</div>
		<span class="font-bold text-base text-gray-800 truncate lg:{collapsed ? 'hidden' : 'block'}">HSS POS</span>
		<!-- Mobile close button -->
		<button
			class="ml-auto p-2 rounded-lg hover:bg-gray-100 text-gray-400 lg:hidden"
			onclick={onclose}
		>
			<svg class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
				<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
			</svg>
		</button>
	</div>

	<!-- Navigation -->
	<nav class="flex-1 overflow-y-auto py-3 px-2">
		{#each visibleItems() as item}
			{#if item.type === 'divider'}
				<div class="my-2 mx-2 border-t border-gray-200"></div>
			{:else}
				<a
					href={item.href}
					class="flex items-center gap-3 px-3 py-3 rounded-lg text-sm transition-colors mb-0.5
						{currentPath.startsWith(item.href) ? 'bg-red-50 text-pos-highlight font-medium' : 'text-gray-600 hover:text-gray-900 hover:bg-gray-100'}"
					title={collapsed ? item.label : ''}
					onclick={handleNavClick}
				>
					<svg class="w-5 h-5 flex-shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
						<path stroke-linecap="round" stroke-linejoin="round" d={item.icon} />
					</svg>
					<span class="truncate max-lg:block {collapsed ? 'lg:hidden' : ''}">{item.label}</span>
				</a>
			{/if}
		{/each}
	</nav>

	<!-- User info -->
	<div class="border-t border-gray-200 p-3">
		<div class="flex items-center gap-3">
			<div class="w-8 h-8 rounded-full bg-gray-200 flex items-center justify-center flex-shrink-0">
				<span class="text-xs font-medium text-gray-600">{user?.full_name?.[0] || '?'}</span>
			</div>
			<div class="flex-1 min-w-0 max-lg:block {collapsed ? 'lg:hidden' : ''}">
				<p class="text-sm font-medium text-gray-800 truncate">{user?.full_name || 'User'}</p>
				<p class="text-xs text-gray-500 capitalize">{role}</p>
			</div>
			<button
				onclick={() => authStore.logout()}
				class="p-2 text-gray-400 hover:text-pos-danger transition-colors rounded-lg hover:bg-gray-100 flex-shrink-0"
				title="Logout"
			>
				<svg class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
					<path stroke-linecap="round" stroke-linejoin="round" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1" />
				</svg>
			</button>
		</div>
	</div>
</aside>
