All files / web/src/app/api/generate route.ts

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
import { NextResponse } from 'next/server'
import { writeFileSync, mkdirSync, rmSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'
import { execSync } from 'child_process'
import type { FlashcardConfig } from '@/app/create/flashcards/page'
import { withAuth } from '@/lib/auth/withAuth'
import {
  generateFlashcardFront,
  generateFlashcardBack,
} from '@/utils/flashcards/generateFlashcardSvgs'

export const dynamic = 'force-dynamic'

/**
 * Parse range string to get all numbers
 */
function parseRange(range: string, step: number): number[] {
  const numbers: number[] = []

  if (range.includes('-')) {
    const [start, end] = range.split('-').map((n) => parseInt(n, 10))
    for (let i = start; i <= end; i += step) {
      numbers.push(i)
    }
  } else if (range.includes(',')) {
    const parts = range.split(',').map((n) => parseInt(n.trim(), 10))
    numbers.push(...parts)
  } else {
    numbers.push(parseInt(range, 10))
  }

  return numbers
}

/**
 * Shuffle array with seed for reproducibility
 */
function shuffleWithSeed<T>(array: T[], seed?: number): T[] {
  const shuffled = [...array]
  const rng = seed !== undefined ? seededRandom(seed) : Math.random

  for (let i = shuffled.length - 1; i > 0; i--) {
    const j = Math.floor(rng() * (i + 1))
    ;[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]
  }

  return shuffled
}

/**
 * Simple seeded random number generator (Mulberry32)
 */
function seededRandom(seed: number) {
  return () => {
    seed = (seed + 0x6d2b79f5) | 0
    let t = Math.imul(seed ^ (seed >>> 15), 1 | seed)
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296
  }
}

export const POST = withAuth(async (request) => {
  let tempDir: string | null = null

  try {
    const config: FlashcardConfig = await request.json()
    const {
      range = '0-99',
      step = 1,
      cardsPerPage = 6,
      paperSize = 'us-letter',
      orientation = 'portrait',
      margins,
      gutter = '5mm',
      shuffle = false,
      seed,
      showCutMarks = false,
      showRegistration = false,
      beadShape = 'diamond',
      colorScheme = 'place-value',
      colorPalette = 'default',
      hideInactiveBeads = false,
      showEmptyColumns = false,
      columns = 'auto',
      scaleFactor = 0.9,
      coloredNumerals = false,
      format = 'pdf',
    } = config

    // Dynamic import to avoid Next.js bundler issues
    const { renderToStaticMarkup } = await import('react-dom/server')

    // Create temp directory for SVG files
    tempDir = join(tmpdir(), `flashcards-${Date.now()}-${Math.random()}`)
    mkdirSync(tempDir, { recursive: true })

    // Get all numbers
    let numbers = parseRange(range, step)

    // Apply shuffle if requested
    if (shuffle) {
      numbers = shuffleWithSeed(numbers, seed)
    }

    if (numbers.length === 0) {
      return NextResponse.json({ error: 'No valid numbers in range' }, { status: 400 })
    }

    // Generate SVG files for each card (front and back)
    const svgConfig = {
      beadShape,
      colorScheme,
      colorPalette,
      hideInactiveBeads,
      showEmptyColumns,
      columns: (columns === 'auto' ? 'auto' : Number(columns)) as number | 'auto',
      scaleFactor,
      coloredNumerals,
    }

    for (let i = 0; i < numbers.length; i++) {
      const num = numbers[i]

      // Generate front (abacus)
      const frontElement = generateFlashcardFront(num, svgConfig)
      const frontSvg = renderToStaticMarkup(frontElement)
      writeFileSync(join(tempDir, `card_${i}_front.svg`), frontSvg)

      // Generate back (numeral)
      const backElement = generateFlashcardBack(num, svgConfig)
      const backSvg = renderToStaticMarkup(backElement)
      writeFileSync(join(tempDir, `card_${i}_back.svg`), backSvg)
    }

    // Calculate paper dimensions and layout
    const paperDimensions = {
      'us-letter': { width: 8.5, height: 11 },
      a4: { width: 8.27, height: 11.69 },
      a3: { width: 11.69, height: 16.54 },
      a5: { width: 5.83, height: 8.27 },
    }

    const paper = paperDimensions[paperSize] || paperDimensions['us-letter']
    const [pageWidth, pageHeight] =
      orientation === 'landscape' ? [paper.height, paper.width] : [paper.width, paper.height]

    // Calculate grid layout (typically 2 columns × 3 rows for 6 cards)
    const cols = 2
    const rows = Math.ceil(cardsPerPage / cols)

    // Use provided margins or defaults
    const margin = {
      top: margins?.top || '0.5in',
      bottom: margins?.bottom || '0.5in',
      left: margins?.left || '0.5in',
      right: margins?.right || '0.5in',
    }

    // Parse gutter (convert from string like "5mm" to inches for calculation)
    const gutterInches = parseFloat(gutter) / 25.4 // Rough mm to inch conversion

    // Calculate available space (approximate, Typst will handle exact layout)
    const marginInches = 0.5 // Simplified for now
    const availableWidth = pageWidth - 2 * marginInches - gutterInches * (cols - 1)
    const availableHeight = pageHeight - 2 * marginInches - gutterInches * (rows - 1)
    const cardWidth = availableWidth / cols
    const cardHeight = availableHeight / rows

    // Generate pages
    const totalPages = Math.ceil(numbers.length / cardsPerPage)
    const pages: string[] = []

    for (let pageNum = 0; pageNum < totalPages; pageNum++) {
      const startIdx = pageNum * cardsPerPage
      const endIdx = Math.min(startIdx + cardsPerPage, numbers.length)
      const pageCards = []

      for (let i = startIdx; i < endIdx; i++) {
        pageCards.push(
          `  image("card_${i}_front.svg", width: ${cardWidth}in, height: ${cardHeight}in, fit: "contain")`
        )
      }

      // Fill remaining slots with empty cells if needed
      const remaining = cardsPerPage - pageCards.length
      for (let i = 0; i < remaining; i++) {
        pageCards.push(`  []`) // Empty cell
      }

      pages.push(`#grid(
  columns: ${cols},
  rows: ${rows},
  column-gutter: ${gutter},
  row-gutter: ${gutter},
${pageCards.join(',\n')}
)`)
    }

    // Generate Typst document
    const typstContent = `
#set page(
  paper: "${paperSize}",
  margin: (x: ${margin.left}, y: ${margin.top}),
  flipped: ${orientation === 'landscape'},
)

${pages.join('\n\n#pagebreak()\n\n')}
`

    // Compile with Typst
    let pdfBuffer: Buffer
    try {
      pdfBuffer = execSync('typst compile --format pdf - -', {
        input: typstContent,
        cwd: tempDir, // Run in temp dir so relative paths work
        maxBuffer: 100 * 1024 * 1024, // 100MB limit for large sets
      })
    } catch (error) {
      console.error('Typst compilation error:', error)
      return NextResponse.json(
        { error: 'Failed to compile PDF. Is Typst installed?' },
        { status: 500 }
      )
    }

    // Clean up temp directory
    rmSync(tempDir, { recursive: true, force: true })
    tempDir = null

    // Create filename for download
    const filename = `soroban-flashcards-${range}.pdf`

    // Return PDF directly as download
    return new NextResponse(new Uint8Array(pdfBuffer), {
      headers: {
        'Content-Type': 'application/pdf',
        'Content-Disposition': `attachment; filename="${filename}"`,
        'Content-Length': pdfBuffer.length.toString(),
      },
    })
  } catch (error) {
    console.error('Error generating flashcards:', error)

    // Clean up temp directory if it exists
    if (tempDir) {
      try {
        rmSync(tempDir, { recursive: true, force: true })
      } catch (cleanupError) {
        console.error('Failed to clean up temp directory:', cleanupError)
      }
    }

    const errorMessage = error instanceof Error ? error.message : String(error)
    return NextResponse.json(
      { error: 'Failed to generate flashcards', message: errorMessage },
      { status: 500 }
    )
  }
})

// Health check endpoint
export const GET = withAuth(async () => {
  try {
    // Check if Typst is available
    execSync('typst --version', { encoding: 'utf8' })

    return NextResponse.json({
      status: 'healthy',
      generator: 'typescript-typst',
      dependencies: {
        typst: true,
        python: false, // No longer needed!
      },
    })
  } catch (error) {
    return NextResponse.json(
      {
        status: 'unhealthy',
        error: 'Typst not available',
        message: error instanceof Error ? error.message : 'Unknown error',
      },
      { status: 500 }
    )
  }
})