← back to Goodquestion
src/app/api/sections/route.ts
85 lines
import { NextRequest, NextResponse } from 'next/server'
import {
historicalWorkflow,
startupCostsWorkflow,
marketAnalysisWorkflow,
actionPlanWorkflow
} from '@/lib/ai-workflows'
import { z } from 'zod'
const SectionRequestSchema = z.object({
section: z.enum(['historical', 'startup', 'market', 'action']),
businessType: z.string().min(1),
location: z.string().min(1),
radiusMiles: z.number().optional().default(10),
})
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const validatedData = SectionRequestSchema.parse(body)
let result
switch (validatedData.section) {
case 'historical':
result = await historicalWorkflow.generateHistoricalContext(
validatedData.location
)
break
case 'startup':
result = await startupCostsWorkflow.generateStartupCosts(
validatedData.businessType,
validatedData.location
)
break
case 'market':
result = await marketAnalysisWorkflow.generateMarketAnalysis(
validatedData.businessType,
validatedData.location,
validatedData.radiusMiles
)
break
case 'action':
result = await actionPlanWorkflow.generateActionPlan(
validatedData.businessType,
validatedData.location
)
break
default:
throw new Error('Invalid section')
}
return NextResponse.json({
success: true,
data: result,
})
} catch (error) {
console.error(`Section generation error:`, error)
if (error instanceof z.ZodError) {
return NextResponse.json(
{
success: false,
error: 'Invalid request data',
details: error.errors,
},
{ status: 400 }
)
}
return NextResponse.json(
{
success: false,
error: 'Failed to generate section',
message: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
)
}
}