import { NextRequest, NextResponse } from 'next/server'
import * as fs from 'fs'
import * as path from 'path'
import * as XLSX from 'xlsx'

const DATA_DIR = path.join(process.cwd(), 'docs/infrastructure/data')

export async function POST(request: NextRequest) {
  try {
    const formData = await request.formData()
    const file = formData.get('file') as File
    const targetFile = formData.get('targetFile') as string

    if (!file || !targetFile) {
      return NextResponse.json(
        { error: 'File and targetFile are required' },
        { status: 400 }
      )
    }

    const buffer = Buffer.from(await file.arrayBuffer())
    const filePath = path.join(DATA_DIR, targetFile)

    // Handle different file types
    if (file.name.endsWith('.json')) {
      const data = JSON.parse(buffer.toString())
      fs.writeFileSync(filePath, JSON.stringify(data, null, 2))
    } else if (file.name.endsWith('.xlsx') || file.name.endsWith('.xls')) {
      const workbook = XLSX.read(buffer, { type: 'buffer' })
      const sheetName = workbook.SheetNames[0]
      const worksheet = workbook.Sheets[sheetName]
      const data = XLSX.utils.sheet_to_json(worksheet)
      fs.writeFileSync(filePath, JSON.stringify(data, null, 2))
    } else if (file.name.endsWith('.csv')) {
      const csv = buffer.toString()
      const lines = csv.split('\n')
      const headers = lines[0].split(',').map((h) => h.trim().replace(/^"|"$/g, ''))
      const data = lines.slice(1).map((line) => {
        const values = line.split(',').map((v) => v.trim().replace(/^"|"$/g, ''))
        return headers.reduce((obj, header, idx) => {
          obj[header] = values[idx] || ''
          return obj
        }, {} as Record<string, any>)
      })
      fs.writeFileSync(filePath, JSON.stringify(data, null, 2))
    } else {
      return NextResponse.json({ error: 'Unsupported file type' }, { status: 400 })
    }

    return NextResponse.json({
      success: true,
      filename: targetFile,
      records: Array.isArray(JSON.parse(fs.readFileSync(filePath, 'utf-8'))) 
        ? JSON.parse(fs.readFileSync(filePath, 'utf-8')).length 
        : 1,
    })
  } catch (error) {
    return NextResponse.json(
      {
        error: 'Failed to import file',
        message: error instanceof Error ? error.message : 'Unknown error',
      },
      { status: 500 }
    )
  }
}

