import { NextRequest, NextResponse } from 'next/server' import fs from 'fs' import path from 'path' const DATA_DIR = process.env.NODE_ENV === 'production' && fs.existsSync('/data') ? '/data' : path.join(process.cwd(), 'data') const FLUTTER_APPS_DIR = path.join(DATA_DIR, 'documents', 'flutter_apps') export async function DELETE(request: NextRequest) { try { const searchParams = request.nextUrl.searchParams const name = searchParams.get('name') if (!name) { return NextResponse.json( { success: false, error: 'App name is required' }, { status: 400 } ) } // Sanitize name const safeName = name.replace(/[^a-zA-Z0-9_-]/g, '_').toLowerCase() const fileName = `${safeName}.flutter.json` const filePath = path.join(FLUTTER_APPS_DIR, fileName) if (!fs.existsSync(filePath)) { return NextResponse.json( { success: false, error: `Flutter app '${safeName}' not found` }, { status: 404 } ) } // Move to trash instead of permanent delete const trashDir = path.join(DATA_DIR, '.trash') if (!fs.existsSync(trashDir)) { fs.mkdirSync(trashDir, { recursive: true }) } const timestamp = Date.now() const trashPath = path.join(trashDir, `${timestamp}_${fileName}`) fs.renameSync(filePath, trashPath) return NextResponse.json({ success: true, appName: safeName, message: 'Flutter app deleted successfully' }) } catch (error) { console.error('Error deleting Flutter app:', error) return NextResponse.json( { success: false, error: 'Failed to delete Flutter app' }, { status: 500 } ) } }