<script lang="ts">
	import { CreateLaborStore, UpdateLaborStore } from '$houdini';
	import { offCanvas } from '$lib/stores/offCanvas.svelte';
	import type { GetAccount$result } from '$houdini';
	import IconSpinner from '$lib/components/icons/IconSpinner.svelte';

	type Labor = NonNullable<GetAccount$result['account']>['addresses'][number]['labors'][number];

	interface Props {
		accountAddressId: string;
		labor?: Labor;
		onSuccess?: () => void;
		onCancel?: () => void;
	}

	let { accountAddressId, labor, onSuccess, onCancel }: Props = $props();

	let isEdit = $derived(!!labor);
	let submitting = $state(false);
	let error = $state('');

	let amount = $state(labor?.amount?.toString() ?? '');
	let startDate = $state(labor?.startDate ?? '');
	let endDate = $state(labor?.endDate ?? '');

	const createStore = new CreateLaborStore();
	const updateStore = new UpdateLaborStore();

	async function handleSubmit(e: SubmitEvent) {
		e.preventDefault();
		submitting = true;
		error = '';

		try {
			const parsedAmount = parseFloat(amount);
			if (isNaN(parsedAmount) || parsedAmount < 0) {
				error = 'Please provide a valid amount';
				return;
			}

			if (!startDate) {
				error = 'Please provide a start date';
				return;
			}

			if (isEdit && labor) {
				const res = await updateStore.mutate({
					input: {
						id: labor.id,
						amount: parsedAmount,
						startDate,
						endDate: endDate || null
					}
				});
				if (res.errors?.length) {
					error = res.errors.map((e) => e.message).join(', ');
					return;
				}
			} else {
				const res = await createStore.mutate({
					input: {
						accountAddressId,
						amount: parsedAmount,
						startDate,
						endDate: endDate || null
					}
				});
				if (res.errors?.length) {
					error = res.errors.map((e) => e.message).join(', ');
					return;
				}
			}

			offCanvas.closeRight();
			onSuccess?.();
		} catch (err) {
			error = err instanceof Error ? err.message : 'Failed to save labor';
		} finally {
			submitting = false;
		}
	}

	function handleCancel() {
		offCanvas.closeRight();
		onCancel?.();
	}
</script>

<div class="min-w-0 space-y-6">
	{#if error}
		<div class="alert-error">
			{error}
		</div>
	{/if}

	<form class="space-y-4" onsubmit={handleSubmit}>
		<div>
			<label for="amount" class="mb-1.5 block text-sm font-medium text-theme">
				Labor Rate (per visit) <span class="text-red-500">*</span>
			</label>
			<div class="relative">
				<span class="absolute top-1/2 left-3 -translate-y-1/2 text-theme-muted">$</span>
				<input
					id="amount"
					type="number"
					step="0.01"
					min="0"
					bind:value={amount}
					disabled={submitting}
					placeholder="0.00"
					class="placeholder-theme-muted w-full rounded-lg border border-theme bg-theme py-2 pr-3 pl-7 text-theme focus:border-primary-500 focus:ring-1 focus:ring-primary-500 disabled:cursor-not-allowed disabled:opacity-50"
				/>
			</div>
		</div>

		<div class="grid grid-cols-2 gap-4">
			<div>
				<label for="startDate" class="mb-1.5 block text-sm font-medium text-theme">
					Start Date <span class="text-red-500">*</span>
				</label>
				<input
					id="startDate"
					type="date"
					bind:value={startDate}
					disabled={submitting}
					class="w-full rounded-lg border border-theme bg-theme px-3 py-2 text-theme focus:border-primary-500 focus:ring-1 focus:ring-primary-500 disabled:cursor-not-allowed disabled:opacity-50"
				/>
			</div>

			<div>
				<label for="endDate" class="mb-1.5 block text-sm font-medium text-theme"> End Date </label>
				<input
					id="endDate"
					type="date"
					bind:value={endDate}
					disabled={submitting}
					class="w-full rounded-lg border border-theme bg-theme px-3 py-2 text-theme focus:border-primary-500 focus:ring-1 focus:ring-primary-500 disabled:cursor-not-allowed disabled:opacity-50"
				/>
			</div>
		</div>

		<p class="text-xs text-theme-muted">
			Leave end date empty to mark this as the current labor rate.
		</p>

		<div class="flex items-center gap-3 pt-4">
			<button
				type="submit"
				disabled={submitting}
				class="flex-1 rounded-lg bg-primary-500 px-4 py-2 font-medium text-white transition-colors hover:bg-primary-600 disabled:cursor-not-allowed disabled:opacity-50"
			>
				{#if submitting}
					<span class="flex items-center justify-center gap-2">
						<IconSpinner class="h-4 w-4" />
						<span>{isEdit ? 'Saving...' : 'Creating...'}</span>
					</span>
				{:else}
					{isEdit ? 'Save Changes' : 'Add Labor'}
				{/if}
			</button>
			<button
				type="button"
				onclick={handleCancel}
				disabled={submitting}
				class="rounded-lg border border-theme bg-theme px-4 py-2 font-medium text-theme transition-colors hover:bg-black/5 disabled:cursor-not-allowed disabled:opacity-50 dark:hover:bg-white/10"
			>
				Cancel
			</button>
		</div>
	</form>
</div>
