/**
 * Northfield Theatre — Contact Form
 *
 * Client-side validation + simulated submission. In production, swap the
 * submit handler for a real Contact Form 7 / Fluent Forms submission.
 *
 * @package Northfield_Theatre
 * @since 1.0.0
 */

(function () {
	'use strict';

	var form = document.getElementById('contact-form');
	if (!form) {
		return;
	}

	var status = document.getElementById('contact-form-status');

	var validators = {
		name: function (val) {
			if (!val || val.trim().length < 2) {
				return 'Please enter your name.';
			}
			return null;
		},
		email: function (val) {
			if (!val) {
				return 'Please enter an email address.';
			}
			if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val)) {
				return 'Please enter a valid email address.';
			}
			return null;
		},
		subject: function (val) {
			if (!val) {
				return 'Please choose what your enquiry is about.';
			}
			return null;
		},
		message: function (val) {
			if (!val || val.trim().length < 10) {
				return 'Please give us a few more details — at least 10 characters.';
			}
			return null;
		},
		consent: function (val, checked) {
			if (!checked) {
				return 'Please confirm you\'ve read the privacy policy.';
			}
			return null;
		},
	};

	function showError(fieldName, message) {
		var errorEl = document.getElementById('contact-' + fieldName + '-error');
		var field = document.getElementById('contact-' + fieldName);
		if (errorEl) {
			errorEl.textContent = message;
			errorEl.hidden = false;
		}
		if (field) {
			field.setAttribute('aria-invalid', 'true');
			field.setAttribute('aria-describedby', 'contact-' + fieldName + '-error');
		}
	}

	function clearError(fieldName) {
		var errorEl = document.getElementById('contact-' + fieldName + '-error');
		var field = document.getElementById('contact-' + fieldName);
		if (errorEl) {
			errorEl.hidden = true;
			errorEl.textContent = '';
		}
		if (field) {
			field.removeAttribute('aria-invalid');
			field.removeAttribute('aria-describedby');
		}
	}

	function validateField(fieldName, value, checked) {
		var err = validators[fieldName] ? validators[fieldName](value, checked) : null;
		if (err) {
			showError(fieldName, err);
			return false;
		}
		clearError(fieldName);
		return true;
	}

	// Validate on blur for better UX
	['name', 'email', 'subject', 'message'].forEach(function (name) {
		var el = document.getElementById('contact-' + name);
		if (el) {
			el.addEventListener('blur', function () {
				validateField(name, el.value);
			});
		}
	});

	var consentEl = document.getElementById('contact-consent');
	if (consentEl) {
		consentEl.addEventListener('change', function () {
			validateField('consent', '', consentEl.checked);
		});
	}

	form.addEventListener('submit', function (e) {
		e.preventDefault();

		var allValid = true;
		['name', 'email', 'subject', 'message'].forEach(function (name) {
			var el = document.getElementById('contact-' + name);
			if (el && !validateField(name, el.value)) {
				allValid = false;
			}
		});

		if (!validateField('consent', '', consentEl ? consentEl.checked : false)) {
			allValid = false;
		}

		if (!allValid) {
			// Focus the first invalid field
			var firstInvalid = form.querySelector('[aria-invalid="true"]');
			if (firstInvalid) {
				firstInvalid.focus();
			}
			if (status) {
				status.textContent = 'Please fix the highlighted fields and try again.';
				status.className = 'contact-form__status contact-form__status--error';
				status.hidden = false;
			}
			return;
		}

		// Simulated submission. In production, this would submit via
		// Contact Form 7, WP REST, or similar. For the demo, fake a
		// delay and show a success confirmation.
		var submitBtn = form.querySelector('button[type="submit"]');
		if (submitBtn) {
			submitBtn.disabled = true;
			submitBtn.setAttribute('aria-busy', 'true');
			submitBtn.textContent = 'Sending…';
		}

		setTimeout(function () {
			form.reset();
			if (status) {
				status.textContent = 'Thanks — we\'ve got your message and will be in touch within two working days.';
				status.className = 'contact-form__status contact-form__status--success';
				status.hidden = false;
			}
			if (submitBtn) {
				submitBtn.disabled = false;
				submitBtn.removeAttribute('aria-busy');
				submitBtn.textContent = 'Send message';
			}
		}, 800);
	});
})();
