import DriftDecision from '@/features/interventions/detail/DriftDecision';
import { resolvePageRef } from '@/features/interventions/detail/pageRef';
import { fileNameFromSrc } from '@/features/interventions/detail/altText';

/**
 * Detail for the `imgaltsingle` / `drift` intervention (type selection).
 *
 * The user edited alt-texts Flavio had applied; the scanner noticed. It's a
 * single decision for all changed images: keep_current (adopt the user's edits)
 * or revert_to_expected (restore Flavio's). Built on the shared DriftDecision,
 * one card per image with its thumbnail.
 *
 * Contract metadata: `drift:{ images:[{src,current_alt,expected_alt}] }`.
 * userResponse: { choice:"keep_current" | "revert_to_expected" }.
 *
 * Defensive on metadata: every field is read with a fallback.
 */
const ImgAltDrift = ({
	intervention = {},
	interventionId,
	onBack,
	onResolved,
}) => {
	const m = intervention.metadata || {};
	const images = Array.isArray(m.drift?.images) ? m.drift.images : [];
	const { pageLabel, pageUrl } = resolvePageRef(m);

	const changes = images.map((img) => ({
		label: fileNameFromSrc(img?.src),
		thumbnail: img?.src || '',
		current: img?.current_alt || '',
		expected: img?.expected_alt || '',
	}));

	const count = images.length;

	return (
		<DriftDecision
			interventionId={interventionId}
			onBack={onBack}
			onResolved={onResolved}
			heading="You edited some image descriptions"
			subtitle="Tell me whether to keep them as they are now or put my versions back. I'll respect your choice next time."
			pageLabel={pageLabel}
			pageUrl={pageUrl}
			changesLabel={`Differences I found · ${count} image${count === 1 ? '' : 's'}`}
			changes={changes}
			emptyText={'empty (alt="")'}
			keepLabel="Keep the current descriptions"
			keepDoneText="Got it. I'll keep your descriptions and stop managing these images."
			revertDoneText="Done. I'll restore my descriptions shortly."
		/>
	);
};

export default ImgAltDrift;
