import { NextRequest, NextResponse } from 'next/server' import { PackageService } from '@/services/packageService' import { SyncAuth } from '@/lib/sync/auth' export async function GET( request: NextRequest, { params }: { params: { id: string } } ) { try { const packageData = await PackageService.getById(params.id) if (!packageData) { return NextResponse.json( { error: 'Package not found' }, { status: 404 } ) } return NextResponse.json(packageData) } catch (error) { console.error('Error fetching package:', error) return NextResponse.json( { error: 'Failed to fetch package' }, { status: 500 } ) } } export async function PUT( request: NextRequest, { params }: { params: { id: string } } ) { // Check auth const auth = await SyncAuth.isWriteAllowed(request) if (!auth.allowed) { return NextResponse.json( { error: auth.reason || 'Unauthorized' }, { status: 403 } ) } try { const body = await request.json() const packageData = await PackageService.update(params.id, body) if (!packageData) { return NextResponse.json( { error: 'Package not found' }, { status: 404 } ) } return NextResponse.json(packageData) } catch (error) { console.error('Error updating package:', error) return NextResponse.json( { error: 'Failed to update package' }, { status: 500 } ) } } export async function DELETE( request: NextRequest, { params }: { params: { id: string } } ) { // Check auth const auth = await SyncAuth.isWriteAllowed(request) if (!auth.allowed) { return NextResponse.json( { error: auth.reason || 'Unauthorized' }, { status: 403 } ) } try { const success = await PackageService.delete(params.id) if (!success) { return NextResponse.json( { error: 'Package not found' }, { status: 404 } ) } return NextResponse.json({ success: true }) } catch (error) { console.error('Error deleting package:', error) return NextResponse.json( { error: 'Failed to delete package' }, { status: 500 } ) } }