All files / web/src/lib/flowcharts worksheet-generator.ts

0% Statements 0/623
0% Branches 0/1
0% Functions 0/1
0% Lines 0/623

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
/**
 * Flowchart Worksheet Generator
 *
 * Server-side generation of PDF worksheets from flowchart examples.
 * Uses Typst for high-quality print-ready output.
 *
 * @module flowcharts/worksheet-generator
 */

import { execSync } from 'child_process'
import * as fs from 'fs/promises'
import * as path from 'path'
import * as os from 'os'
import { getFlowchartByIdAsync } from './definitions'
import { loadFlowchart, simulateWalk, extractAnswer } from './loader'
import {
  generateDiverseExamples,
  type GeneratedExample,
  DEFAULT_CONSTRAINTS,
} from './example-generator'
import { formatProblemDisplay } from './formatting'
import type { ExecutableFlowchart, ProblemValue } from './schema'

// =============================================================================
// Types
// =============================================================================

export interface WorksheetConfig {
  /** Distribution of problems by difficulty tier */
  distribution: {
    easy: number
    medium: number
    hard: number
  }
  /** Total number of problems */
  problemCount: number
  /** Number of pages */
  pageCount: number
  /** Whether to include an answer key */
  includeAnswerKey: boolean
  /** Optional custom title */
  title?: string
  /** Order problems by progressive difficulty (easy → medium → hard) */
  orderByDifficulty?: boolean
}

export interface WorksheetProblem {
  /** Problem values */
  values: Record<string, ProblemValue>
  /** Display string (e.g., "45 − 27") */
  display: string
  /** Difficulty tier */
  tier: 'easy' | 'medium' | 'hard'
  /** Answer (computed from flowchart) - plain text */
  answer: string
  /** Answer formatted as Typst math mode (for PDF answer key) */
  typstAnswer: string
}

// =============================================================================
// Tier Classification
// =============================================================================

/**
 * Get difficulty tier for an example based on its complexity score
 */
function getTier(
  example: GeneratedExample,
  range: { min: number; max: number }
): 'easy' | 'medium' | 'hard' {
  const score = example.complexity.decisions + example.complexity.checkpoints
  if (range.max === range.min) return 'easy'
  const normalized = (score - range.min) / (range.max - range.min)
  if (normalized < 0.25) return 'easy'
  if (normalized < 0.75) return 'medium'
  return 'hard'
}

/**
 * Calculate difficulty range from examples
 */
function calculateDifficultyRange(examples: GeneratedExample[]): { min: number; max: number } {
  if (examples.length === 0) return { min: 0, max: 0 }
  const scores = examples.map((ex) => ex.complexity.decisions + ex.complexity.checkpoints)
  return { min: Math.min(...scores), max: Math.max(...scores) }
}

// =============================================================================
// Problem Selection
// =============================================================================

/**
 * Select problems from examples based on distribution
 */
function selectProblems(
  flowchart: ExecutableFlowchart,
  examples: GeneratedExample[],
  config: WorksheetConfig
): WorksheetProblem[] {
  const range = calculateDifficultyRange(examples)

  // Classify all examples by tier
  const byTier: Record<'easy' | 'medium' | 'hard', GeneratedExample[]> = {
    easy: [],
    medium: [],
    hard: [],
  }

  for (const example of examples) {
    const tier = getTier(example, range)
    byTier[tier].push(example)
  }

  // Shuffle each tier
  for (const tier of ['easy', 'medium', 'hard'] as const) {
    byTier[tier] = shuffleArray(byTier[tier])
  }

  // Calculate how many from each tier
  const targetCounts = {
    easy: Math.round((config.distribution.easy / 100) * config.problemCount),
    hard: Math.round((config.distribution.hard / 100) * config.problemCount),
    medium: 0, // Calculated to make total exact
  }
  targetCounts.medium = config.problemCount - targetCounts.easy - targetCounts.hard

  // Select problems, filling from other tiers if needed
  const selected: WorksheetProblem[] = []
  const remaining: GeneratedExample[] = []

  for (const tier of ['easy', 'medium', 'hard'] as const) {
    const available = byTier[tier]
    const target = targetCounts[tier]
    const toTake = Math.min(target, available.length)

    for (let i = 0; i < toTake; i++) {
      selected.push(exampleToProblem(flowchart, available[i], tier))
    }

    // Save extras for potential fill
    for (let i = toTake; i < available.length; i++) {
      remaining.push(available[i])
    }
  }

  // Fill remaining slots from any tier
  while (selected.length < config.problemCount && remaining.length > 0) {
    const example = remaining.shift()!
    const tier = getTier(example, range)
    selected.push(exampleToProblem(flowchart, example, tier))
  }

  // Either sort by difficulty or shuffle to mix tiers
  if (config.orderByDifficulty) {
    // Sort by tier: easy → medium → hard
    const tierOrder = { easy: 0, medium: 1, hard: 2 }
    return selected.sort((a, b) => tierOrder[a.tier] - tierOrder[b.tier])
  } else {
    // Shuffle to mix tiers randomly
    return shuffleArray(selected)
  }
}

/**
 * Convert an example to a worksheet problem
 */
function exampleToProblem(
  flowchart: ExecutableFlowchart,
  example: GeneratedExample,
  tier: 'easy' | 'medium' | 'hard'
): WorksheetProblem {
  const display = formatProblemDisplay(flowchart, example.values)

  // Use simulateWalk + extractAnswer for unified answer computation
  let answer = '?'
  let typstAnswer = '?'
  try {
    const terminalState = simulateWalk(flowchart, example.values)
    const { display: answerDisplay } = extractAnswer(flowchart, terminalState)
    answer = answerDisplay.text || '?'
    // Use typst template if provided, otherwise convert from text
    typstAnswer = answerDisplay.typst || convertToTypstAnswer(answer)
  } catch (err) {
    console.error('Failed to compute answer via simulateWalk:', err)
    typstAnswer = convertToTypstAnswer(answer)
  }

  return {
    values: example.values,
    display,
    tier,
    answer,
    typstAnswer,
  }
}

/**
 * Convert a plain text answer to Typst math mode format.
 *
 * Handles:
 * - Simple numbers: "42" → "42"
 * - Decimals: "0.375" → "0.375"
 * - Simple fractions: "3/4" → "$frac(3, 4)$"
 * - Mixed numbers: "2 1/2" → "$2 frac(1, 2)$"
 * - Negative fractions: "-3/4" → "$-frac(3, 4)$"
 * - Linear equation answers: "x = 5" → "$x = 5$"
 */
function convertToTypstAnswer(answer: string): string {
  // If it's "?" or empty, return as-is
  if (answer === '?' || !answer) return answer

  // Check for "x = N" pattern (linear equations)
  const xEqualsMatch = answer.match(/^x\s*=\s*(-?\d+(?:\.\d+)?)$/)
  if (xEqualsMatch) {
    return `$x = ${xEqualsMatch[1]}$`
  }

  // Check for mixed number pattern: "N M/D" (e.g., "2 1/2" or "-3 1/4")
  const mixedMatch = answer.match(/^(-?)(\d+)\s+(\d+)\/(\d+)$/)
  if (mixedMatch) {
    const [, sign, whole, num, denom] = mixedMatch
    return `$${sign}${whole} frac(${num}, ${denom})$`
  }

  // Check for simple fraction pattern: "N/D" (e.g., "3/4" or "-1/2")
  const fractionMatch = answer.match(/^(-?)(\d+)\/(\d+)$/)
  if (fractionMatch) {
    const [, sign, num, denom] = fractionMatch
    return `$${sign}frac(${num}, ${denom})$`
  }

  // For simple numbers (integers or decimals), return as-is
  // This handles cases like "42", "0.375", "-7", etc.
  return answer
}

// =============================================================================
// Typst Template Generation
// =============================================================================

/**
 * Generate Typst source for the worksheet
 */
export function generateWorksheetTypst(
  flowchart: ExecutableFlowchart,
  problems: WorksheetProblem[],
  config: WorksheetConfig
): string {
  const title = config.title || flowchart.definition.title
  const problemsPerPage = Math.ceil(problems.length / config.pageCount)
  const schema = flowchart.definition.problemInput.schema

  // Choose formatter based on schema
  const formatProblem = getSchemaFormatter(schema)

  // Split into pages
  const pages: WorksheetProblem[][] = []
  for (let i = 0; i < problems.length; i += problemsPerPage) {
    pages.push(problems.slice(i, i + problemsPerPage))
  }

  // Build Typst source
  let typst = `
#set page(paper: "us-letter", margin: (x: 0.5in, y: 0.75in))
#set text(size: 11pt)

// Title
#align(center)[
  #text(size: 22pt, weight: "bold")[${escapeTypst(title)}]
  #v(0.1in)
  #text(size: 10pt, fill: rgb("#666666"))[Practice Worksheet]
]

#v(0.3in)

// Name line
#grid(
  columns: (auto, 1fr),
  gutter: 0.3in,
  [*Name:*],
  [#line(length: 100%, stroke: 0.5pt + rgb("#cccccc"))]
)

#v(0.4in)
`

  // Problem pages
  for (let pageIdx = 0; pageIdx < pages.length; pageIdx++) {
    const pageProblems = pages[pageIdx]

    if (pageIdx > 0) {
      typst += `\n#pagebreak()\n\n`
    }

    // Determine grid layout based on problem count per page
    const cols = pageProblems.length <= 8 ? 2 : 3
    const cellWidth = cols === 2 ? '45%' : '30%'

    typst += `
// Problem Grid - Page ${pageIdx + 1}
#grid(
  columns: (${Array(cols).fill(cellWidth).join(', ')}),
  gutter: 0.3in,
  row-gutter: 0.5in,
`

    for (let i = 0; i < pageProblems.length; i++) {
      const problem = pageProblems[i]
      const problemNum = pageIdx * problemsPerPage + i + 1

      // Get difficulty indicator
      const difficultyDot = getDifficultyDot(problem.tier)

      typst += `
  // Problem ${problemNum}
  [
    #align(center)[
      #box(
        stroke: 1pt + rgb("#e5e7eb"),
        radius: 8pt,
        inset: 12pt,
        width: 100%,
      )[
        #grid(
          columns: (auto, 1fr, auto),
          align: (left, center, right),
          [#text(size: 9pt, fill: rgb("#9ca3af"))[${problemNum}.]],
          [],
          [${difficultyDot}]
        )
        #v(0.15in)
        ${formatProblem(problem)}
        #v(0.2in)
        #line(length: 60%, stroke: 0.5pt + rgb("#d1d5db"))
      ]
    ]
  ],
`
    }

    typst += `)\n`
  }

  // Answer key
  if (config.includeAnswerKey) {
    typst += `
#pagebreak()

// Answer Key
#align(center)[
  #text(size: 18pt, weight: "bold")[Answer Key]
]

#v(0.3in)

#grid(
  columns: (1fr, 1fr, 1fr, 1fr),
  gutter: 0.2in,
`

    for (let i = 0; i < problems.length; i++) {
      const problem = problems[i]
      // Use typstAnswer for proper fraction rendering in the PDF
      typst += `
  [
    #text(size: 10pt)[
      *${i + 1}.* ${problem.typstAnswer}
    ]
  ],
`
    }

    typst += `)\n`
  }

  // Footer
  typst += `
#v(1fr)
#align(center)[
  #text(size: 8pt, fill: rgb("#9ca3af"))[
    Generated by abaci.one
  ]
]
`

  return typst
}

/**
 * Get schema-specific problem formatter
 */
function getSchemaFormatter(schema: string): (problem: WorksheetProblem) => string {
  switch (schema) {
    case 'two-digit-subtraction':
      return formatSubtractionProblem
    case 'two-fractions-with-op':
      return formatFractionProblem
    case 'linear-equation':
      return formatLinearEquationProblem
    default:
      return formatGenericProblem
  }
}

/**
 * Format a subtraction problem as vertical stack
 */
function formatSubtractionProblem(problem: WorksheetProblem): string {
  const minuend = problem.values.minuend as number
  const subtrahend = problem.values.subtrahend as number

  return `
        #align(center)[
          #stack(
            dir: ttb,
            spacing: 4pt,
            align(right)[#text(size: 18pt)[${minuend}]],
            align(right)[
              #text(size: 18pt)[−]#h(0.1in)#text(size: 18pt)[${subtrahend}]
            ],
            line(length: 50pt, stroke: 1.5pt),
          )
        ]
`
}

/**
 * Format a fraction problem
 */
function formatFractionProblem(problem: WorksheetProblem): string {
  const leftWhole = (problem.values.leftWhole as number) || 0
  const leftNum = (problem.values.leftNum as number) || 0
  const leftDenom = (problem.values.leftDenom as number) || 1
  const rightWhole = (problem.values.rightWhole as number) || 0
  const rightNum = (problem.values.rightNum as number) || 0
  const rightDenom = (problem.values.rightDenom as number) || 1
  const op = problem.values.op as string

  // Helper to format a mixed number or fraction in Typst math mode
  // In math mode, use frac(num, denom) without the # prefix
  const formatMixed = (whole: number, num: number, denom: number) => {
    if (whole > 0 && num > 0) {
      return `${whole} frac(${num}, ${denom})`
    } else if (whole > 0) {
      return `${whole}`
    } else if (num > 0) {
      return `frac(${num}, ${denom})`
    } else {
      return `0`
    }
  }

  const left = formatMixed(leftWhole, leftNum, leftDenom)
  const right = formatMixed(rightWhole, rightNum, rightDenom)
  const opSymbol = op === '+' ? '+' : '-'

  return `
        #align(center)[
          #text(size: 16pt)[
            $${left} ${opSymbol} ${right} = $
            #box(width: 40pt, stroke: (bottom: 0.5pt))
          ]
        ]
`
}

/**
 * Format a linear equation problem
 */
function formatLinearEquationProblem(problem: WorksheetProblem): string {
  const coefficient = problem.values.coefficient as number
  const operation = problem.values.operation as string
  const constant = problem.values.constant as number
  const equals = problem.values.equals as number

  const opSymbol = operation === '+' ? '+' : '−'
  const coeffStr = coefficient === 1 ? '' : String(coefficient)

  return `
        #align(center)[
          #text(size: 16pt)[
            $${coeffStr}x ${opSymbol} ${constant} = ${equals}$
          ]
          #v(0.15in)
          #text(size: 12pt)[$x = $]
          #box(width: 40pt, stroke: (bottom: 0.5pt))
        ]
`
}

/**
 * Format a generic problem (fallback)
 */
function formatGenericProblem(problem: WorksheetProblem): string {
  return `
        #align(center)[
          #text(size: 16pt)[${escapeTypst(problem.display)}]
          #v(0.2in)
          #box(width: 60pt, stroke: (bottom: 0.5pt))
        ]
`
}

/**
 * Get difficulty indicator dot in Typst
 */
function getDifficultyDot(tier: 'easy' | 'medium' | 'hard'): string {
  const colors = {
    easy: '#10b981', // emerald-500
    medium: '#f59e0b', // amber-500
    hard: '#ef4444', // red-500
  }
  return `#circle(radius: 3pt, fill: rgb("${colors[tier]}"))`
}

// =============================================================================
// PDF Generation
// =============================================================================

/**
 * Generate a worksheet PDF from a flowchart ID
 *
 * @param flowchartId - ID of the flowchart (database or hardcoded)
 * @param config - Worksheet configuration
 * @returns PDF buffer
 */
export async function generateWorksheetPDF(
  flowchartId: string,
  config: WorksheetConfig
): Promise<Buffer> {
  // Load flowchart
  const flowchartData = await getFlowchartByIdAsync(flowchartId)
  if (!flowchartData) {
    throw new Error(`Flowchart not found: ${flowchartId}`)
  }

  const flowchart = await loadFlowchart(flowchartData.definition, flowchartData.mermaid)

  return generateWorksheetPDFFromFlowchart(flowchart, config)
}

/**
 * Generate a worksheet PDF from an ExecutableFlowchart directly
 *
 * This is useful for workshop drafts that haven't been saved to the database yet.
 *
 * @param flowchart - The executable flowchart
 * @param config - Worksheet configuration
 * @returns PDF buffer
 */
export async function generateWorksheetPDFFromFlowchart(
  flowchart: ExecutableFlowchart,
  config: WorksheetConfig
): Promise<Buffer> {
  // Generate examples
  const exampleCount = Math.max(config.problemCount * 3, 100) // Generate extra for variety
  const examples = generateDiverseExamples(flowchart, exampleCount, DEFAULT_CONSTRAINTS)

  if (examples.length === 0) {
    throw new Error('Could not generate any examples for this flowchart')
  }

  // Select problems
  const problems = selectProblems(flowchart, examples, config)

  if (problems.length < config.problemCount) {
    console.warn(
      `Could only generate ${problems.length} problems (requested ${config.problemCount})`
    )
  }

  // Generate Typst
  const typstSource = generateWorksheetTypst(flowchart, problems, config)

  // Compile to PDF
  const tmpDir = os.tmpdir()
  const timestamp = Date.now()
  const typstFile = path.join(tmpDir, `worksheet-${timestamp}.typ`)
  const pdfFile = path.join(tmpDir, `worksheet-${timestamp}.pdf`)

  try {
    await fs.writeFile(typstFile, typstSource, 'utf-8')

    execSync(`typst compile "${typstFile}" "${pdfFile}"`, {
      timeout: 30000,
      stdio: ['pipe', 'pipe', 'pipe'],
    })

    const pdfBuffer = await fs.readFile(pdfFile)
    return pdfBuffer
  } finally {
    // Cleanup
    try {
      await fs.unlink(typstFile)
    } catch {
      // Ignore
    }
    try {
      await fs.unlink(pdfFile)
    } catch {
      // Ignore
    }
  }
}

// =============================================================================
// Utility Functions
// =============================================================================

/** Shuffle array in place (Fisher-Yates) */
function shuffleArray<T>(array: T[]): T[] {
  const result = [...array]
  for (let i = result.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1))
    ;[result[i], result[j]] = [result[j], result[i]]
  }
  return result
}

/** Escape special characters for Typst */
function escapeTypst(str: string): string {
  return str.replace(/[#$@\\]/g, '\\$&').replace(/"/g, '\\"')
}