<script lang="ts">
	import type { GoogleSheet } from '$lib/google/sheets/index.js';
	
	// Props
	let { sheetsData, errors, loadRecentSheets, selectSheet, toggleSheetList } = $props<{
		sheetsData: {
			availableSheets: GoogleSheet[];
			selectedSheet: GoogleSheet | null;
			sheetData: string[][];
			columnMapping: {
				name: number;
				surname: number;
				email: number;
				confirmation: number;
			};
			loading: boolean;
			expandedSheetList: boolean;
		};
		errors: Record<string, string>;
		loadRecentSheets: () => Promise<void>;
		selectSheet: (sheet: GoogleSheet) => Promise<void>;
		toggleSheetList: () => void;
	}>();
</script>

<div class="space-y-6">
	<div>
		<h3 class="text-lg font-medium text-gray-900 mb-4">Select Google Sheet</h3>
		
		{#if sheetsData.loading && sheetsData.availableSheets.length === 0}
			<div class="space-y-3">
				{#each Array(5) as _}
					<div class="p-4 border border-gray-200 rounded animate-pulse">
						<div class="h-4 bg-gray-200 rounded w-3/4 mb-2"></div>
						<div class="h-3 bg-gray-100 rounded w-1/2"></div>
					</div>
				{/each}
			</div>
		{:else if sheetsData.availableSheets.length === 0}
			<div class="text-center py-8">
				<p class="text-gray-500">No Google Sheets found.</p>
				<button
					onclick={loadRecentSheets}
					class="mt-2 px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 transition"
				>
					Refresh
				</button>
			</div>
		{:else}
			<div class="space-y-3">
				{#if !sheetsData.expandedSheetList && sheetsData.selectedSheet}
					<!-- Selected sheet only (collapsed view) -->
					<div class="flex items-center justify-between p-4 border border-blue-500 bg-blue-50 rounded">
						<div>
							<div class="font-medium text-gray-900">{sheetsData.selectedSheet.name}</div>
							<div class="text-sm text-gray-500">
								Modified: {new Date(sheetsData.selectedSheet.modifiedTime).toLocaleDateString()}
							</div>
						</div>
						<button
							onclick={toggleSheetList}
							class="text-blue-600 hover:text-blue-800 flex items-center"
							aria-label="Show all sheets"
						>
							<span class="text-sm mr-1">Change</span>
							<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" viewBox="0 0 20 20" fill="currentColor">
								<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
							</svg>
						</button>
					</div>
				{:else}
					<!-- All sheets (expanded view) -->
					<div class="flex justify-between items-center mb-2">
						<h4 class="text-sm font-medium text-gray-700">Available Sheets</h4>
						{#if sheetsData.selectedSheet}
							<button
								onclick={toggleSheetList}
								class="text-sm text-blue-600 hover:text-blue-800"
								aria-label="Hide sheet list"
							>
								Collapse list
							</button>
						{/if}
					</div>
					<div class="grid gap-3">
						{#each sheetsData.availableSheets as sheet}
							<button
								onclick={() => selectSheet(sheet)}
								class="p-4 text-left border border-gray-200 rounded hover:border-blue-500 transition {
									sheetsData.selectedSheet?.id === sheet.id ? 'border-blue-500 bg-blue-50' : ''
								}"
							>
								<div class="font-medium text-gray-900">{sheet.name}</div>
								<div class="text-sm text-gray-500">
									Modified: {new Date(sheet.modifiedTime).toLocaleDateString()}
								</div>
							</button>
						{/each}
					</div>
				{/if}
			</div>
		{/if}

		{#if errors.sheet}
			<p class="mt-2 text-sm text-red-600">{errors.sheet}</p>
		{/if}
	</div>

	{#if sheetsData.selectedSheet && sheetsData.sheetData.length > 0}
		<div>
			<h3 class="text-lg font-medium text-gray-900 mb-4">Column Mapping</h3>
			
			<!-- Instructions for column mapping -->
			<div class="bg-white p-4 rounded-md border border-gray-300 mb-4">
				<p class="text-sm text-black-800 mb-2 font-medium">Column Mapping Instructions:</p>
				<p class="text-sm text-black-700">
					Select what each column represents by using the dropdown in each column header. 
					Make sure to assign Name, Surname, Email, and Confirmation columns.
				</p>
			</div>
			
			<div class="overflow-x-auto">
				<table class="w-full border border-gray-200 rounded">
					<thead>
						<tr class="bg-gray-50">
							{#each sheetsData.sheetData[0] || [] as header, index}
								<th class="px-3 py-2 border-b border-gray-200 text-left">
									<div class="flex flex-col gap-2">
										<div class="font-medium text-gray-900">
											{header || `Empty Column ${index + 1}`}
										</div>
										<select 
											class="text-sm normal-case font-normal px-2 py-1 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
											aria-label={`Select data type for column ${index + 1}`}
											onclick={(e) => e.stopPropagation()}
											onchange={(e) => {
												const value = e.target.value;
												if (value === "none") return;
												
												// Reset previous selection if this column was already mapped
												if (sheetsData.columnMapping.name === index + 1) sheetsData.columnMapping.name = 0;
												if (sheetsData.columnMapping.surname === index + 1) sheetsData.columnMapping.surname = 0;
												if (sheetsData.columnMapping.email === index + 1) sheetsData.columnMapping.email = 0;
												if (sheetsData.columnMapping.confirmation === index + 1) sheetsData.columnMapping.confirmation = 0;
												
												// Set new mapping
												if (value === "name") sheetsData.columnMapping.name = index + 1;
												else if (value === "surname") sheetsData.columnMapping.surname = index + 1;
												else if (value === "email") sheetsData.columnMapping.email = index + 1;
												else if (value === "confirmation") sheetsData.columnMapping.confirmation = index + 1;
											}}
										>
											<option value="none">Select data type</option>
											<option value="name" selected={sheetsData.columnMapping.name === index + 1}>Name</option>
											<option value="surname" selected={sheetsData.columnMapping.surname === index + 1}>Surname</option>
											<option value="email" selected={sheetsData.columnMapping.email === index + 1}>Email</option>
											<option value="confirmation" selected={sheetsData.columnMapping.confirmation === index + 1}>Confirmation</option>
										</select>
										<div class="h-7 mt-1"> <!-- Fixed height container to prevent layout shift -->
											{#if sheetsData.columnMapping.name === index + 1}
												<span class="bg-green-100 text-green-800 text-xs px-2 py-1 rounded">Name Column</span>
											{:else if sheetsData.columnMapping.surname === index + 1}
												<span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded">Surname Column</span>
											{:else if sheetsData.columnMapping.email === index + 1}
												<span class="bg-purple-100 text-purple-800 text-xs px-2 py-1 rounded">Email Column</span>
											{:else if sheetsData.columnMapping.confirmation === index + 1}
												<span class="bg-amber-100 text-amber-800 text-xs px-2 py-1 rounded">Confirmation Column</span>
											{:else}
												<span class="bg-gray-100 text-gray-400 text-xs px-2 py-1 rounded">Not Mapped</span>
											{/if}
										</div>
									</div>
								</th>
							{/each}
						</tr>
					</thead>
					<tbody>
						{#each sheetsData.sheetData.slice(0, 10) as row, rowIndex}
							<tr class="hover:bg-gray-50">
								{#each row as cell, cellIndex}
									<td class="px-3 py-2 border-b border-gray-100 text-sm">
										<span 
											class={
												sheetsData.columnMapping.name === cellIndex + 1 ? 'font-medium text-green-700' : 
												sheetsData.columnMapping.surname === cellIndex + 1 ? 'font-medium text-blue-700' : 
												sheetsData.columnMapping.email === cellIndex + 1 ? 'font-medium text-purple-700' : 
												sheetsData.columnMapping.confirmation === cellIndex + 1 ? 'font-medium text-amber-700' : 
												'text-gray-700'
											}
										>
											{cell || ''}
										</span>
									</td>
								{/each}
							</tr>
						{/each}
					</tbody>
				</table>
			</div>
			<p class="mt-2 text-sm text-gray-500">Showing first 10 rows</p>
		</div>
	{/if}

	{#if sheetsData.loading && sheetsData.selectedSheet}
		<div class="text-center py-4">
			<div class="text-gray-600">Loading sheet data...</div>
		</div>
	{/if}

	{#if errors.sheetData}
		<p class="text-sm text-red-600">{errors.sheetData}</p>
	{/if}
</div>
