`;
const generateArguments = () => {
const motion = dom.motionInput.value.trim();
if (!motion) {
dom.motionInput.focus();
dom.motionInput.classList.add('border-red-500');
return;
}
dom.motionInput.classList.remove('border-red-500');
dom.forArgumentsContainer.innerHTML = argumentTemplates.for.map(arg => createArgumentHTML(arg, 'for')).join('');
dom.againstArgumentsContainer.innerHTML = argumentTemplates.against.map(arg => createArgumentHTML(arg, 'against')).join('');
dom.resultsContainer.style.display = 'block';
};
const downloadPDF = () => {
try {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const motion = dom.motionInput.value.trim();
const pageWidth = doc.internal.pageSize.getWidth();
const margin = 15;
let cursorY = 20;
// --- PDF Header ---
doc.setFont('helvetica', 'bold');
doc.setFontSize(16);
doc.text('Debate Briefing Document', pageWidth / 2, cursorY, { align: 'center' });
cursorY += 10;
doc.setFont('helvetica', 'normal');
doc.setFontSize(12);
const motionLines = doc.splitTextToSize(`Motion: ${motion || 'Not Specified'}`, pageWidth - margin * 2);
doc.text(motionLines, pageWidth / 2, cursorY, { align: 'center' });
cursorY += motionLines.length * 5 + 10;
doc.setLineWidth(0.5);
doc.line(margin, cursorY, pageWidth - margin, cursorY);
cursorY += 10;
const addArgumentsToPDF = (title, args, color) => {
if (cursorY > doc.internal.pageSize.getHeight() - 60) {
doc.addPage();
cursorY = margin;
}
doc.setFont('helvetica', 'bold');
doc.setFontSize(14);
doc.setTextColor(color[0], color[1], color[2]);
doc.text(title, margin, cursorY);
cursorY += 8;
doc.setTextColor(0, 0, 0);
args.forEach(arg => {
if (cursorY > doc.internal.pageSize.getHeight() - 40) {
doc.addPage();
cursorY = margin;
}
doc.setFont('helvetica', 'bold');
doc.setFontSize(11);
const claimLines = doc.splitTextToSize(arg.claim, pageWidth - margin * 2);
doc.text(claimLines, margin, cursorY);
cursorY += claimLines.length * 4.5 + 2;
doc.setFont('helvetica', 'normal');
doc.setFontSize(10);
const warrantLines = doc.splitTextToSize(`Warrant: ${arg.warrant}`, pageWidth - margin * 2 - 5);
doc.text(warrantLines, margin + 5, cursorY);
cursorY += warrantLines.length * 4.5 + 2;
const impactLines = doc.splitTextToSize(`Impact: ${arg.impact}`, pageWidth - margin * 2 - 5);
doc.text(impactLines, margin + 5, cursorY);
cursorY += impactLines.length * 4.5 + 8;
});
};
addArgumentsToPDF('Arguments For (Proposition)', argumentTemplates.for, [34, 197, 94]); // Green
cursorY += 5;
doc.setLineWidth(0.2);
doc.line(margin, cursorY, pageWidth - margin, cursorY);
cursorY += 10;
addArgumentsToPDF('Arguments Against (Opposition)', argumentTemplates.against, [239, 68, 68]); // Red
doc.save(`Debate_Briefing_${motion.substring(0,20).replace(/\s+/g, '_')}.pdf`);
} catch (error) {
console.error("Failed to generate PDF:", error);
if (dom.pdfButtonContainer && !dom.pdfButtonContainer.querySelector('.error-msg')) {
const errorMsg = document.createElement('p');
errorMsg.textContent = 'Sorry, the PDF could not be created.';
errorMsg.className = 'text-red-600 text-sm mt-2 error-msg';
dom.pdfButtonContainer.appendChild(errorMsg);
}
}
};
dom.generateButton.addEventListener('click', generateArguments);
dom.downloadPdfButton.addEventListener('click', downloadPDF);
});
