/**
 * DIY print & bind guides for each pocket-book format.
 * Ported from rPubs-online/src/lib/diy-guides.ts.
 */

export interface DiyGuide {
	formatId: string;
	formatName: string;
	parentSheet: "A4" | "US Letter";
	pagesPerSheet: number;
	foldType: "half" | "quarters";
	bindingType: "saddle-stitch" | "perfect-bind";
	tools: string[];
	paperRecommendation: string;
	foldInstructions: string[];
	bindingInstructions: string[];
	tips: string[];
}

export function paddedPageCount(pageCount: number): number {
	return Math.ceil(pageCount / 4) * 4;
}

const GUIDES: Record<string, DiyGuide> = {
	a7: {
		formatId: "a7",
		formatName: "A7 Pocket",
		parentSheet: "A4",
		pagesPerSheet: 4,
		foldType: "quarters",
		bindingType: "saddle-stitch",
		tools: [
			"Printer (A4 paper, double-sided)",
			"Bone folder or ruler edge",
			"Stapler (long-reach) or needle + thread",
			"Craft knife and cutting mat (optional, for trimming)",
		],
		paperRecommendation:
			"Standard 80gsm A4 paper works well. For a nicer feel, try 100gsm recycled paper. Use a heavier sheet (160gsm) for the cover wrap.",
		foldInstructions: [
			"Print the imposition PDF double-sided, flipping on the short edge.",
			"Take each printed sheet and fold it in half widthwise (hamburger fold).",
			"Fold in half again, bringing the top down to the bottom — you now have a small A7 signature.",
			"Crease firmly along each fold with a bone folder or ruler edge.",
			"Nest all signatures inside each other in page order.",
		],
		bindingInstructions: [
			"Align all nested signatures so the spine edges are flush.",
			"Open the booklet flat to the center spread.",
			"Mark two staple points on the spine fold, each about 1/4 from the top and bottom.",
			"Staple through from the outside of the spine, or sew a pamphlet stitch.",
			"Close the booklet and press firmly along the spine.",
		],
		tips: [
			"A7 is tiny — test your printer alignment with a single sheet first.",
			"If your printer can't do double-sided, print odd pages, flip the stack, then print even pages.",
			"Trim the outer edges with a craft knife for a clean finish.",
		],
	},
	a6: {
		formatId: "a6",
		formatName: "A6 Booklet",
		parentSheet: "A4",
		pagesPerSheet: 4,
		foldType: "half",
		bindingType: "saddle-stitch",
		tools: [
			"Printer (A4 paper, double-sided)",
			"Bone folder or ruler edge",
			"Stapler (long-reach) or needle + thread",
			"Craft knife and cutting mat (optional)",
		],
		paperRecommendation:
			"Standard 80-100gsm A4 paper. Use 120-160gsm card stock for a separate cover if desired.",
		foldInstructions: [
			"Print the imposition PDF double-sided, flipping on the short edge.",
			"Fold each printed A4 sheet in half widthwise — the fold becomes the spine.",
			"Crease firmly with a bone folder.",
			"Nest all folded sheets inside each other in page order.",
		],
		bindingInstructions: [
			"Align all nested sheets so the spine fold is flush.",
			"Open the booklet flat to the center spread.",
			"Mark 2 or 3 staple/stitch points evenly along the spine fold.",
			"Staple through from outside, or sew a pamphlet stitch.",
			"Close and press the spine flat.",
		],
		tips: [
			"A6 from A4 is the most natural zine format — minimal waste.",
			"For thicker booklets (>12 sheets), consider making 2-3 separate signatures and sewing them together.",
			"A rubber band around the finished booklet while drying helps keep it flat.",
		],
	},
	"quarter-letter": {
		formatId: "quarter-letter",
		formatName: "Quarter Letter",
		parentSheet: "US Letter",
		pagesPerSheet: 4,
		foldType: "quarters",
		bindingType: "saddle-stitch",
		tools: [
			"Printer (US Letter paper, double-sided)",
			"Bone folder or ruler edge",
			"Stapler (long-reach) or needle + thread",
			"Craft knife and cutting mat (optional)",
		],
		paperRecommendation:
			'Standard 20lb (75gsm) US Letter paper. For a sturdier feel, use 24lb (90gsm). Card stock (65lb / 176gsm) makes a good separate cover.',
		foldInstructions: [
			"Print the imposition PDF double-sided, flipping on the short edge.",
			'Fold each sheet in half widthwise — bringing the 11" edges together.',
			'Fold in half again — you now have a quarter-letter booklet (4.25" x 5.5").',
			"Crease all folds firmly.",
			"Nest folded signatures inside each other in order.",
		],
		bindingInstructions: [
			"Align nested signatures with spine edges flush.",
			"Open to the center spread.",
			"Mark 2 staple points on the spine, 1/4 from top and bottom.",
			"Staple or stitch through the spine at each mark.",
			"Close and press flat.",
		],
		tips: [
			"Quarter Letter is the classic American zine size — easy to photocopy and distribute.",
			"If you don't have a long-reach stapler, open it flat and push staples through from inside the fold onto cardboard, then bend the legs flat.",
			"Trim the open edges for a professional finish.",
		],
	},
	digest: {
		formatId: "digest",
		formatName: 'Digest (5.5" x 8.5")',
		parentSheet: "US Letter",
		pagesPerSheet: 2,
		foldType: "half",
		bindingType: "saddle-stitch",
		tools: [
			"Printer (US Letter paper, double-sided)",
			"Bone folder or ruler edge",
			"Stapler (long-reach) or needle + thread + awl",
			"Binder clips",
			"PVA glue + brush (for perfect binding, if >48 pages)",
		],
		paperRecommendation:
			'Standard 20lb US Letter paper for the interior. For perfect binding, use 24lb paper and a separate cover on card stock.',
		foldInstructions: [
			"Print the imposition PDF double-sided, flipping on the short edge.",
			'Fold each US Letter sheet in half along the 11" edge.',
			"Crease firmly with a bone folder.",
			"Nest folded sheets inside each other in page order.",
		],
		bindingInstructions: [
			"For saddle-stitch (up to ~48 pages / 12 sheets):",
			"  Align all nested sheets with spine flush.",
			"  Open to center spread, mark 3 stitch points along the spine.",
			"  Staple or sew through at each point.",
			"",
			"For perfect binding (thicker books, 48+ pages):",
			"  Stack all folded signatures in order (don't nest — stack).",
			"  Clamp the spine edge with binder clips, leaving 3mm exposed.",
			"  Score the spine with shallow cuts every 3mm to help glue grip.",
			"  Apply PVA glue thinly. Let dry 5 min, apply a second coat.",
			"  Wrap a cover sheet around the glued spine.",
			"  Clamp and let dry for 1-2 hours.",
			"  Trim the three open edges.",
		],
		tips: [
			"Digest is the most common POD size — your home print will match professional prints.",
			"For saddle-stitch, keep it under 48 pages (12 folded sheets) or it won't fold flat.",
			"For perfect binding, work in a well-ventilated area.",
			"A paper cutter gives cleaner edges than a craft knife for the final trim.",
		],
	},
};

export function getGuide(formatId: string): DiyGuide | undefined {
	return GUIDES[formatId];
}

export function recommendedBinding(
	formatId: string,
	pageCount: number,
): "saddle-stitch" | "perfect-bind" {
	if (formatId === "digest" && pageCount > 48) return "perfect-bind";
	return "saddle-stitch";
}
