import { NextRequest, NextResponse } from 'next/server'
import fs from 'fs/promises'
import path from 'path'

export async function GET(
  request: NextRequest,
  context: any
) {
  try {
    const screenshotsDir = path.join(process.cwd(), 'screenshots')
    const filePath = path.join(screenshotsDir, context.params.filename)
    const file = await fs.readFile(filePath)
    return new NextResponse(file, {
      headers: {
        'Content-Type': 'image/png',
        'Content-Disposition': `inline; filename="${context.params.filename}"`
      }
    })
  } catch (e: any) {
    return NextResponse.json({ error: e.message }, { status: 404 })
  }
}
