import TextProposal from '@/features/interventions/detail/TextProposal';

/**
 * Detail for the `googbusinessoptimize` / `propose` intervention (type selection).
 *
 * Flavio proposes a better Google Business Profile description; the user edits it
 * inline and Publishes, or keeps the current one. The executor writes to GBP via
 * the Google API once acknowledged, so the plugin's client-side validation is the
 * only guard before submission: not empty, within `char_limit`, and free of links
 * and phone numbers (GBP rejects both).
 *
 * Contract (doc wins over the task): options [apply, dismiss]; userResponse is
 * `{ choice:"apply", final_description }`. So Publish → `acknowledge` with the
 * edited value, and the secondary action → `dismiss`.
 *
 * Defensive on metadata: every field is read with a fallback.
 */
const GBP_CHAR_LIMIT = 750;

// GBP descriptions can't contain links or phone numbers; block them client-side.
const URL_RE =
	/(https?:\/\/|www\.|\b[a-z0-9-]+\.(?:com|net|org|io|co|es|info|biz|us|uk|de|fr|eu)\b)/i;
const PHONE_RE = /\+?\d[\d\s().-]{6,}\d/;

const GbpDescription = ({
	intervention = {},
	interventionId,
	onBack,
	onResolved,
}) => {
	const m = intervention.metadata || {};
	const charLimit = Number(m.char_limit) || GBP_CHAR_LIMIT;
	const locationName = m.location_name || '';

	const validate = (value) => {
		if (value.length > charLimit) {
			return `Keep it under ${charLimit} characters.`;
		}
		if (URL_RE.test(value)) {
			return "Google Business descriptions can't include web links. Please remove it.";
		}
		if (PHONE_RE.test(value)) {
			return "Google Business descriptions can't include phone numbers. Please remove it.";
		}
		return null;
	};

	const subtitle = locationName
		? `This is the description people see for ${locationName} on Google. Edit it if you want, or publish as is.`
		: 'This is the description people see on your Google Business Profile. Edit it if you want, or publish as is.';

	return (
		<TextProposal
			interventionId={interventionId}
			onBack={onBack}
			onResolved={onResolved}
			heading="Approve your Google Business Profile description"
			subtitle={subtitle}
			fieldLabel="Description"
			currentValue={m.current_description || ''}
			proposedValue={m.proposed_description || ''}
			emptyText="No description set yet"
			multiline
			maxLength={charLimit}
			counterLabel="Characters"
			validate={validate}
			reasoning={m.reasoning || ''}
			primaryLabel="Publish"
			secondaryLabel="Keep as is"
			primaryAction={(value) => ({
				status: 'acknowledge',
				userResponse: { choice: 'apply', final_description: value },
			})}
			secondaryAction={() => ({
				status: 'dismiss',
				userResponse: { choice: 'dismiss' },
			})}
			footerNote="Nothing goes live on your profile until you publish."
			dismissTitle="Dismiss this suggestion?"
			dismissDescription="Nothing changes on your Google Business Profile, and I won't suggest this again."
			dismissConfirmLabel="Yes, dismiss"
			primaryDoneText="Great. I'll update your Google Business Profile shortly."
			secondaryDoneText="No problem. I'll keep your current description."
		/>
	);
};

export default GbpDescription;
