// Lalaluxe booking — booking/screens/subaddons.jsx
// Page: "subaddons" — what a 3-visit plan covers: its nail art tier, then the plan extras.
// Nail art is priced by tier on a plan as it is on a single set, but the choice is the tier itself:
// one tier covers every visit (planTierOf), at the tier's price for each of the three. So the
// same ladder as the look screen is drawn with each rung as the tap target — each carries what
// that tier adds to the plan, the rung she picks is marked, the rungs under it are covered, and
// what the plan does not cover stands back.
// One screen per file. Load before app.jsx (see docs/architecture.md, "The no-build multi-file Babel contract").
function SubAddonsStep({ cfg, s, set, toggleArr }) {
	const SDESC = {
		"acrylic-toes": "Matching toes at every visit",
		repair: "Repairs for a loose charm or broken tip.",
	};
	const pieces = orderedKeys(cfg.content.order?.art, cfg.art);
	// a tier with no nail art in it has nothing to sell, so it is not offered (unless it is already the plan's)
	const tiers = orderedKeys(cfg.content.order?.tier, cfg.tier)
		.filter((t) => t === planTierOf(s) || pieces.some((a) => cfg.art[a] === t));
	const rank = tiers.indexOf(planTierOf(s));

	// Zero-price perks (repairs) are part of every plan: rendered locked, "Included ✓", not as
	// a live toggle.
	const opts = orderedKeys(cfg.content.order?.subAdd, cfg.subAdd).map((k) => ({
		id: k,
		name: cfg.labels.subAddon[k],
		desc: SDESC[k],
		included: cfg.subAdd[k] === 0,
		price: priceTag(cfg.subAdd[k]),
	}));

	return (
		<div className="screen">
			<ScreenHead cfg={cfg} screen="subaddons">
				Your Plan’s <em>Nail Art</em>
			</ScreenHead>
			<div className="body">
				<ol className="tiers tiers--plan">
					{tiers.map((t, i) => {
						const mine = pieces.filter((a) => cfg.art[a] === t);
						const state = i === rank ? "on" : i < rank ? "under" : "";
						const price = priceTag(cfg.tier[t] * PLAN_VISITS); // added to the plan's own price
						return (
							<li key={t} className={("tier " + state).trim()}>
								<span className="tier-no" aria-hidden="true">{i + 1}</span>
								<div className="tier-head" aria-hidden="true">
									<span className="tier-name">{cfg.labels.tier[t]}</span>
									<span className="tier-price">
										{state === "on" ? <small>Your Tier</small> : state === "" && <small>3 Visits</small>}
										{state === "under" ? <small>Covered</small> : (Number(cfg.tier[t]) > 0 ? <b>{price}</b> : <small>Included</small>)}
									</span>
									{cfg.content.tierDesc?.[t] && <span className="tier-desc">{cfg.content.tierDesc[t]}</span>}
								</div>
								{mine.length > 0 && (
									<div className="art-grid" aria-hidden="true">
										{mine.map((a) => (
											<span key={a} className="art">
												<ArtIcon kind={cfg.content.artIcon?.[a]} />
												<span className="art-name">{cfg.labels.art[a]}</span>
											</span>
										))}
									</div>
								)}
								<button
									className="tier-pick"
									aria-pressed={i === rank}
									aria-label={`${cfg.labels.tier[t]}: ${mine.map((a) => cfg.labels.art[a]).join(", ")}. ${price} for three visits`}
									onClick={() => set({ planTier: t })}
								/>
							</li>
						);
					})}
				</ol>

				<section className="block">
					<h3 className="co-section">Extras <small>Optional</small></h3>
					<div className="opt-list">
						{opts.map((o) => (
							<AddonRow
								key={o.id}
								item={o}
								included={o.included}
								on={!o.included && s.subaddons.indexOf(o.id) > -1}
								onToggle={() => toggleArr("subaddons", o.id)}
							/>
						))}
					</div>
				</section>
			</div>
		</div>
	);
}
