import { NextRequest, NextResponse } from 'next/server'; import admin from '@/lib/firebase/admin'; /** * Update work item state (draft/final) */ export async function PATCH( request: NextRequest, { params }: { params: Promise<{ projectId: string; itemId: string }> } ) { try { const { projectId, itemId } = await params; const { state } = await request.json(); if (!state || !['draft', 'final'].includes(state)) { return NextResponse.json( { error: 'Invalid state. Must be "draft" or "final"' }, { status: 400 } ); } const db = admin.firestore(); // Update state in work item // For now, store in a separate collection since work items are generated from MVP checklist await db .collection('projects') .doc(projectId) .collection('workItemStates') .doc(itemId) .set( { state, updatedAt: admin.firestore.FieldValue.serverTimestamp(), }, { merge: true } ); return NextResponse.json({ success: true, state, }); } catch (error) { console.error('Error updating work item state:', error); return NextResponse.json( { error: 'Failed to update state', details: error instanceof Error ? error.message : String(error), }, { status: 500 } ); } } /** * Get work item state */ export async function GET( request: NextRequest, { params }: { params: Promise<{ projectId: string; itemId: string }> } ) { try { const { projectId, itemId } = await params; const db = admin.firestore(); const stateDoc = await db .collection('projects') .doc(projectId) .collection('workItemStates') .doc(itemId) .get(); if (!stateDoc.exists) { return NextResponse.json({ state: 'draft', // Default state }); } return NextResponse.json({ state: stateDoc.data()?.state || 'draft', updatedAt: stateDoc.data()?.updatedAt, }); } catch (error) { console.error('Error fetching work item state:', error); return NextResponse.json( { error: 'Failed to fetch state', details: error instanceof Error ? error.message : String(error), }, { status: 500 } ); } }