import { useEffect } from 'react';
import { createPortal } from 'react-dom';
import { X, LifeBuoy } from 'lucide-react';
import { openBeacon, BEACON_ROUTE_SCOUT } from '@/lib/helpscout';

/**
 * Support panel shown before the Help Scout Beacon.
 *
 * Users took the Beacon for a chat with Flavio, so the "Help" button opens
 * this panel first: it says who they are about to talk to and then hands
 * over to the Beacon, opened on Scout.
 *
 * It copies the Beacon window's geometry in "manual" display mode (size,
 * position, radius, shadow, enter animation, small-height and mobile
 * layouts), taken from Help Scout's beacon-v2 CSS. The Beacon then opens in
 * exactly the same spot and reads as the next screen of the same widget, not
 * as a confirmation modal.
 *
 * @param {object} props
 * @param {boolean} props.open
 * @param {() => void} props.onClose
 */
const SupportPanel = ({ open, onClose }) => {
	useEffect(() => {
		if (!open) return undefined;
		const onKeyDown = (event) => {
			if (event.key === 'Escape') onClose();
		};
		document.addEventListener('keydown', onKeyDown);
		return () => document.removeEventListener('keydown', onKeyDown);
	}, [open, onClose]);

	if (!open) return null;

	const handleStartChat = () => {
		onClose();
		openBeacon(BEACON_ROUTE_SCOUT);
	};

	// Portal to <body>: a transformed ancestor (page transitions) would
	// otherwise anchor `position: fixed` to it instead of the viewport.
	return createPortal(
		<div
			role="dialog"
			aria-labelledby="flavio-support-panel-title"
			className={[
				// Beacon window, "manual" display mode
				'fixed z-[100000] flex flex-col overflow-hidden bg-[#f1f3f5]',
				'right-10 bottom-[55px] w-[375px] rounded-[10px]',
				'h-[calc(100vh-123px)] min-h-[250px] max-h-[650px]',
				'shadow-[0_0_0_1px_rgba(0,0,0,0.05),0_5px_30px_0_rgba(0,0,0,0.15),0_3px_3px_0_rgba(0,0,0,0.05)]',
				// Beacon enter animation: fade in while rising 20px, 250ms
				'animate-in fade-in slide-in-from-bottom-5 duration-[250ms] ease-out',
				// Short viewports: the Beacon moves closer to the corner
				'[@media(max-height:740px)]:right-5 [@media(max-height:740px)]:bottom-[25px] [@media(max-height:740px)]:h-[calc(100vh-63px)]',
				// Mobile: full screen
				'max-[450px]:inset-0 max-[450px]:h-full max-[450px]:max-h-full max-[450px]:w-full max-[450px]:rounded-none',
			].join(' ')}
		>
			<div className="relative shrink-0 bg-[#ff72d3] px-6 pt-8 pb-10 text-neutral-900">
				<button
					type="button"
					onClick={onClose}
					aria-label="Close"
					className="absolute top-3 right-3 flex h-8 w-8 items-center justify-center rounded-full text-neutral-900/80 transition-colors hover:bg-neutral-900/10 hover:text-neutral-900 outline-none focus-visible:ring-2 focus-visible:ring-neutral-900 cursor-pointer"
				>
					<X className="h-5 w-5" />
				</button>
				<LifeBuoy className="mb-3 h-7 w-7" />
				<h2
					id="flavio-support-panel-title"
					className="m-0 text-xl font-semibold leading-[120%] tracking-[-0.02em] text-neutral-900"
				>
					Need a hand?
				</h2>
				<p className="small-regular mt-1 text-neutral-900/80">
					Our support team is here to help.
				</p>
			</div>

			<div className="-mt-5 flex flex-1 flex-col overflow-auto px-4 pb-4">
				<div className="rounded-md bg-white p-5 shadow-[0_4px_10px_1px_rgba(49,67,81,0.04),0_2px_3px_0_rgba(49,67,81,0.1),0_1px_0_0_rgba(49,67,81,0.08)]">
					<p className="small-semibold text-foreground">
						This chat is with our support team, not with Flavio.
					</p>
					<p className="small-regular mt-2 text-muted-foreground">
						Flavio won&apos;t see what you write here. An assistant
						may reply first with help articles, and someone from the
						team will follow up if needed.
					</p>
				</div>

				<div className="mt-auto pt-4">
					<button
						type="button"
						onClick={handleStartChat}
						className="block h-12 w-full rounded-[4px] bg-foreground px-6 text-sm font-semibold text-background transition-colors hover:bg-foreground/90 outline-none focus-visible:ring-2 focus-visible:ring-ring cursor-pointer"
					>
						Chat with support
					</button>
				</div>
			</div>
		</div>,
		document.body
	);
};

export default SupportPanel;
