<script lang="ts">
	import { client } from '$lib/graphql/client';
	import { UPDATE_TEAM_PROFILE, type UpdateTeamProfileInput } from '$lib/graphql/mutations/profile';
	import type { TeamProfile, TeamRole, EntityStatus } from '$lib/graphql/queries/team';

	interface Props {
		profile: TeamProfile;
		onSuccess: () => void;
		onCancel: () => void;
	}

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

	// Form state - use $effect to sync from props
	let firstName = $state('');
	let lastName = $state('');
	let email = $state('');
	let phone = $state('');
	let role = $state<TeamRole>('TEAM_MEMBER');
	let status = $state<EntityStatus>('ACTIVE');
	let notes = $state('');

	$effect(() => {
		firstName = profile.firstName;
		lastName = profile.lastName;
		email = profile.email ?? '';
		phone = profile.phone ?? '';
		role = profile.role;
		status = profile.status;
		notes = profile.notes ?? '';
	});

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

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

		try {
			// Always send values - empty string clears optional fields
			const input: UpdateTeamProfileInput = {
				firstName,
				lastName,
				email,
				phone,
				role,
				status,
				notes
			};

			await client.mutate({
				mutation: UPDATE_TEAM_PROFILE,
				variables: { id: profile.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 class="grid grid-cols-1 gap-4 sm:grid-cols-2">
		<div>
			<label for="firstName" class="form-label">
				First Name <span class="required-indicator">*</span>
			</label>
			<input
				id="firstName"
				type="text"
				bind:value={firstName}
				class="input-base"
				required
				disabled={loading}
			/>
		</div>

		<div>
			<label for="lastName" class="form-label">
				Last Name <span class="required-indicator">*</span>
			</label>
			<input
				id="lastName"
				type="text"
				bind:value={lastName}
				class="input-base"
				required
				disabled={loading}
			/>
		</div>
	</div>

	<div>
		<label for="email" class="form-label">Email</label>
		<input id="email" type="email" bind:value={email} class="input-base" disabled={loading} />
	</div>

	<div>
		<label for="phone" class="form-label">Phone</label>
		<input
			id="phone"
			type="tel"
			bind:value={phone}
			class="input-base"
			placeholder="(555) 123-4567"
			disabled={loading}
		/>
	</div>

	<div class="border-t border-theme pt-5">
		<h3 class="mb-3 text-sm font-medium text-theme">Role & Access</h3>

		<div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
			<div>
				<label for="role" class="form-label">Role</label>
				<select id="role" bind:value={role} class="select-base" disabled={loading}>
					<option value="ADMIN">Admin</option>
					<option value="TEAM_LEADER">Team Leader</option>
					<option value="TEAM_MEMBER">Team Member</option>
				</select>
			</div>

			<div>
				<label for="status" class="form-label">Status</label>
				<select id="status" bind:value={status} class="select-base" disabled={loading}>
					<option value="ACTIVE">Active</option>
					<option value="INACTIVE">Inactive</option>
					<option value="PENDING">Pending</option>
				</select>
			</div>
		</div>
	</div>

	<div class="border-t border-theme pt-5">
		<label for="notes" class="form-label">Notes</label>
		<textarea
			id="notes"
			bind:value={notes}
			class="textarea-base"
			rows="3"
			placeholder="Optional notes about this team member..."
			disabled={loading}
		></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 Profile
			{/if}
		</button>
	</div>
</form>
