<script lang="ts">
	import { createProjectScope, updateProjectScope } from '$lib/utils/scopes';
	import { offCanvas } from '$lib/stores/offCanvas.svelte';
	import type { ProjectScope$result } from '$houdini';
	import IconSpinner from '$lib/components/icons/IconSpinner.svelte';

	type Scope = NonNullable<ProjectScope$result['projectScope']>;

	interface Props {
		projectId: string;
		scope?: Scope;
		onSuccess?: () => void;
		onCancel?: () => void;
	}

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

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

	let name = $state(scope?.name ?? 'Project Scope');
	let description = $state(scope?.description ?? '');
	let isActive = $state(scope?.isActive ?? true);

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

		try {
			if (!name.trim()) {
				error = 'Please provide a scope name';
				return;
			}

			if (isEdit && scope) {
				await updateProjectScope({
					id: scope.id,
					name: name.trim(),
					description: description.trim() || null,
					isActive
				});
			} else {
				await createProjectScope({
					projectId,
					name: name.trim(),
					description: description.trim() || null,
					isActive
				});
			}

			offCanvas.closeRight();
			onSuccess?.();
		} catch (err) {
			error = err instanceof Error ? err.message : 'Failed to save scope';
		} 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="name" class="mb-1.5 block text-sm font-medium text-theme">
				Scope Name <span class="text-red-500">*</span>
			</label>
			<input
				id="name"
				type="text"
				bind:value={name}
				disabled={submitting}
				placeholder="e.g., Project Scope"
				class="placeholder-theme-muted 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="description" class="mb-1.5 block text-sm font-medium text-theme">
				Description
			</label>
			<textarea
				id="description"
				bind:value={description}
				disabled={submitting}
				rows="3"
				placeholder="Optional description of this project scope"
				class="placeholder-theme-muted 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"
			></textarea>
		</div>

		<div>
			<div class="flex items-center gap-2">
				<input
					id="isActive"
					type="checkbox"
					bind:checked={isActive}
					disabled={submitting}
					class="h-4 w-4 rounded border-input text-primary-500 focus:ring-primary-500 disabled:cursor-not-allowed disabled:opacity-50"
				/>
				<label for="isActive" class="text-sm text-theme">Active scope</label>
			</div>
		</div>

		<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' : 'Create Scope'}
				{/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>
