<script lang="ts">
	import { client } from '$lib/graphql/client';
	import { UPDATE_SERVICE, type UpdateServiceInput } from '$lib/graphql/mutations/service';
	import type { WorkStatus } from '$lib/graphql/queries/services';

	interface Service {
		id: string;
		date: string;
		status: WorkStatus;
		calendarEventId?: string | null;
		notes?: string | null;
	}

	interface Props {
		service: Service;
		onSuccess: () => void;
		onCancel: () => void;
	}

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

	// Form state - use $effect to sync from props
	let date = $state('');
	let status = $state<WorkStatus>('SCHEDULED');
	let calendarEventId = $state('');
	let notes = $state('');

	$effect(() => {
		date = service.date;
		status = service.status;
		calendarEventId = service.calendarEventId ?? '';
		notes = service.notes ?? '';
	});

	let loading = $state(false);
	let error = $state<string | null>(null);

	async function handleSubmit(event: SubmitEvent) {
		event.preventDefault();
		loading = true;
		error = null;

		try {
			const input: UpdateServiceInput = {
				date: date || undefined,
				status: status || undefined,
				calendarEventId: calendarEventId, // Allow empty string to clear
				notes: notes || undefined
			};

			await client.mutate({
				mutation: UPDATE_SERVICE,
				variables: { id: service.id, input }
			});

			onSuccess();
		} catch (err) {
			error = err instanceof Error ? err.message : 'An error occurred';
		} finally {
			loading = false;
		}
	}
</script>

<form onsubmit={handleSubmit} class="space-y-5">
	{#if error}
		<div class="rounded-lg border border-error-500/30 bg-error-50/50 p-3 dark:bg-error-950/30">
			<p class="text-sm text-error-700 dark:text-error-400">{error}</p>
		</div>
	{/if}

	<div>
		<label for="date" class="form-label">
			Service Date <span class="required-indicator">*</span>
		</label>
		<input id="date" type="date" bind:value={date} class="input-base" required disabled={loading} />
	</div>

	<div>
		<label for="status" class="form-label">Status</label>
		<select id="status" bind:value={status} class="input-base" disabled={loading}>
			<option value="SCHEDULED">Scheduled</option>
			<option value="IN_PROGRESS">In Progress</option>
			<option value="COMPLETED">Completed</option>
			<option value="CANCELLED">Cancelled</option>
		</select>
	</div>

	<div>
		<label for="calendarEventId" class="form-label">Calendar Event ID</label>
		<input
			id="calendarEventId"
			type="text"
			bind:value={calendarEventId}
			class="input-base"
			disabled={loading}
			placeholder="Enter calendar event ID..."
		/>
		<p class="mt-1 text-xs text-theme-muted">Google Calendar event ID for scheduling</p>
	</div>

	<div>
		<label for="notes" class="form-label">Notes</label>
		<textarea
			id="notes"
			bind:value={notes}
			class="textarea-base"
			rows="4"
			disabled={loading}
			placeholder="Add any notes about this service..."
		></textarea>
	</div>

	<div class="form-actions border-t border-theme pt-5">
		<button type="button" class="btn-cancel" onclick={onCancel} disabled={loading}>Cancel</button>
		<button type="submit" class="btn-submit" disabled={loading}>
			{#if loading}
				Saving...
			{:else}
				Update Service
			{/if}
		</button>
	</div>
</form>
