<template>
	<div class="form-section">
		<div class="section-header">
			<i class="pi pi-users" style="color: var(--primary-color); font-size: 1.2rem;"></i>
			<h3>Contact Information</h3>
		</div>
		<div class="form-grid">
			<div
				v-for="(contact, index) in localFormData.contacts"
				:key="index"
				class="contact-item"
			>
				<div class="contact-header">
					<div class="contact-title">
						<i class="pi pi-user" style="font-size: 0.9rem; color: var(--primary-color);"></i>
						<h4>Contact {{ index + 1 }}</h4>
					</div>
					<div class="interactables">
						<div class="form-field header-row">
							<input
								type="checkbox"
								class="contact-checkbox"
								:id="`check-${index}`"
								v-model="contact.isPrimary"
								label="Primary Contact"
								:disabled="isSubmitting"
								@change="setPrimary(index)"
							/>
							<label :for="`checkbox-${index}`">
								<i class="pi pi-star-fill" style="font-size: 0.7rem; margin-right: 0.25rem;"></i>Primary
							</label>
						</div>
						<Button
							v-if="localFormData.contacts.length > 1"
							@click="removeContact(index)"
							size="small"
							severity="danger"
							icon="pi pi-trash"
							class="remove-btn"
						/>
					</div>
				</div>
				<div class="form-rows">
					<div class="form-row">
						<div class="form-field">
							<label :for="`first-name-${index}`">
								<i class="pi pi-user" style="font-size: 0.75rem; margin-right: 0.25rem;"></i>First Name <span class="required">*</span>
							</label>
							<InputText
								:id="`first-name-${index}`"
								v-model="contact.firstName"
								:disabled="isSubmitting"
								placeholder="First name"
								class="w-full"
								@input="formatName(index, 'firstName', $event)"
							/>
						</div>
						<div class="form-field">
							<label :for="`last-name-${index}`">
								<i class="pi pi-user" style="font-size: 0.75rem; margin-right: 0.25rem;"></i>Last Name <span class="required">*</span>
							</label>
							<InputText
								:id="`last-name-${index}`"
								v-model="contact.lastName"
								:disabled="isSubmitting"
								placeholder="Last name"
								class="w-full"
								@input="formatName(index, 'lastName', $event)"
							/>
						</div>
						<div class="form-field">
							<label :for="`contact-role-${index}`"><i class="pi pi-briefcase" style="font-size: 0.75rem; margin-right: 0.25rem;"></i>Role</label>
							<Select
								:id="`contact-role-${index}`"
								v-model="contact.contactRole"
								:options="roleOptions"
								optionLabel="label"
								optionValue="value"
								:disabled="isSubmitting"
								placeholder="Select role"
								class="w-full"
							/>
						</div>
					</div>
					<div class="form-row">
						<div class="form-field">
							<label :for="`email-${index}`"><i class="pi pi-envelope" style="font-size: 0.75rem; margin-right: 0.25rem;"></i>Email</label>
							<InputText
								:id="`email-${index}`"
								v-model="contact.email"
								:disabled="isSubmitting"
								type="email"
								placeholder="email@example.com"
								class="w-full"
							/>
						</div>
						<div class="form-field">
							<label :for="`phone-number-${index}`"><i class="pi pi-phone" style="font-size: 0.75rem; margin-right: 0.25rem;"></i>Phone</label>
							<InputText
								:id="`phone-number-${index}`"
								v-model="contact.phoneNumber"
								:disabled="isSubmitting"
								placeholder="(555) 123-4567"
								class="w-full"
								@input="formatPhone(index, $event)"
								@keydown="handlePhoneKeydown($event, index)"
							/>
						</div>
					</div>
				</div>
			</div>
			<div class="form-field full-width">
				<Button label="Add another contact" @click="addContact" :disabled="isSubmitting" />
			</div>
		</div>
	</div>
</template>

<script setup>
import { ref, watch, computed, onMounted } from "vue";
import InputText from "primevue/inputtext";
import Select from "primevue/select";
import Button from "primevue/button";

const props = defineProps({
	formData: {
		type: Object,
		required: true,
	},
	isSubmitting: {
		type: Boolean,
		default: false,
	},
	isEditMode: {
		type: Boolean,
		default: false,
	},
	isNewClientLocked: {
		type: Boolean,
		default: false,
	},
});

const emit = defineEmits(["update:formData"]);

const localFormData = computed({
	get: () => {
		if (!props.formData.contacts || props.formData.contacts.length === 0) {
			props.formData.contacts = [
				{
					firstName: "",
					lastName: "",
					phoneNumber: "",
					email: "",
					contactRole: "",
					isPrimary: true,
				},
			];
		}
		return props.formData;
	},
	set: (value) => emit("update:formData", value),
});

const roleOptions = ref([
	{ label: "Owner", value: "Owner" },
	{ label: "Property Manager", value: "Property Manager" },
	{ label: "Tenant", value: "Tenant" },
	{ label: "Builder", value: "Builder" },
	{ label: "Neighbor", value: "Neighbor" },
	{ label: "Family Member", value: "Family Member" },
	{ label: "Realtor", value: "Realtor" },
	{ label: "Other", value: "Other" },
]);

// Ensure at least one contact
onMounted(() => {
	if (!localFormData.value.contacts || localFormData.value.contacts.length === 0) {
		localFormData.value.contacts = [
			{
				firstName: "",
				lastName: "",
				phoneNumber: "",
				email: "",
				contactRole: "",
				isPrimary: true,
			},
		];
	}
});

const addContact = () => {
	localFormData.value.contacts.push({
		firstName: "",
		lastName: "",
		phoneNumber: "",
		email: "",
		contactRole: "",
		isPrimary: false,
	});
};

const removeContact = (index) => {
	if (localFormData.value.contacts.length > 1) {
		const wasPrimary = localFormData.value.contacts[index].isPrimary;
		localFormData.value.contacts.splice(index, 1);
		if (wasPrimary && localFormData.value.contacts.length > 0) {
			localFormData.value.contacts[0].isPrimary = true;
		}
	}
};

const setPrimary = (index) => {
	localFormData.value.contacts.forEach((contact, i) => {
		contact.isPrimary = i === index;
	});
};

const formatName = (index, field, event) => {
	const value = event.target.value;
	if (!value) return;
	
	// Capitalize first letter, lowercase the rest
	const formatted = value.charAt(0).toUpperCase() + value.slice(1).toLowerCase();
	localFormData.value.contacts[index][field] = formatted;
};

const formatPhoneNumber = (value) => {
	const digits = value.replace(/\D/g, "").slice(0, 10);
	if (digits.length <= 3) return digits;
	if (digits.length <= 6) return `(${digits.slice(0, 3)}) ${digits.slice(3)}`;
	return `(${digits.slice(0, 3)}) ${digits.slice(3, 6)}-${digits.slice(6)}`;
};

const formatPhone = (index, event) => {
	const value = event.target.value;
	const formatted = formatPhoneNumber(value);
	localFormData.value.contacts[index].phoneNumber = formatted;
};

const handlePhoneKeydown = (event, index) => {
	const allowedKeys = [
		"Backspace",
		"Delete",
		"Tab",
		"Escape",
		"Enter",
		"ArrowLeft",
		"ArrowRight",
		"ArrowUp",
		"ArrowDown",
		"Home",
		"End",
	];

	if (allowedKeys.includes(event.key)) {
		return;
	}

	// Allow Ctrl+A, Ctrl+C, Ctrl+V, etc.
	if (event.ctrlKey || event.metaKey) {
		return;
	}

	// Check if it's a digit
	if (!/\d/.test(event.key)) {
		event.preventDefault();
		return;
	}

	// Check current digit count
	const currentDigits = localFormData.value.contacts[index].phoneNumber.replace(
		/\D/g,
		"",
	).length;
	if (currentDigits >= 10) {
		event.preventDefault();
	}
};

defineExpose({});
</script>

<style scoped>
.form-section {
	background: var(--surface-card);
	border-radius: 6px;
	padding: 0.75rem;
	border: 1px solid var(--surface-border);
	box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
	transition: box-shadow 0.2s ease;
}

.form-section:hover {
	box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
}

.section-header {
	display: flex;
	align-items: center;
	gap: 0.5rem;
	margin-bottom: 0.5rem;
	padding-bottom: 0.5rem;
	border-bottom: 2px solid var(--surface-border);
}

.section-header h3 {
	margin: 0;
	color: var(--text-color);
	font-size: 1.1rem;
	font-weight: 600;
}

.contact-item {
	border: 1px solid var(--surface-border);
	border-radius: 6px;
	padding: 0.75rem;
	margin-bottom: 0.75rem;
	background: var(--surface-section);
	min-width: 33%;
	transition: all 0.2s ease;
}

.contact-item:hover {
	border-color: var(--primary-color);
	box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
}

.contact-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 0.75rem;
	padding-bottom: 0.5rem;
	border-bottom: 1px solid var(--surface-border);
}

.contact-title {
	display: flex;
	align-items: center;
	gap: 0.5rem;
}

.contact-header h4 {
	margin: 0;
	color: var(--text-color);
	font-size: 0.95rem;
	font-weight: 600;
}

.interactables {
	display: flex;
	align-items: center;
	flex-direction: row;
	gap: 1rem;
}

.remove-btn {
	margin-left: auto;
}

.form-rows {
	display: flex;
	flex-direction: column;
	gap: 0.625rem;
}

.form-row {
	display: flex;
	gap: 0.625rem;
}

.form-field {
	display: flex;
	flex-direction: column;
	gap: 0.375rem;
	flex: 1;
}

.form-field.full-width {
	width: 100%;
}

.form-field label {
	font-weight: 500;
	color: var(--text-color);
	font-size: 0.85rem;
	color: var(--text-color-secondary);
	font-size: 0.9rem;
}

.form-field.header-row {
	flex-direction: row;
	align-items: baseline;
}

.contact-checkbox {
	margin-top: 0px;
}

.required {
	color: var(--red-500);
}

.w-full {
	width: 100% !important;
}

@media (max-width: 768px) {
	.form-grid {
		grid-template-columns: 1fr;
	}

	.section-header {
		flex-direction: column;
		align-items: flex-start;
		gap: 0.5rem;
	}
}
</style>
