/**
 * POST /api/revalidate?secret=<REVALIDATE_SECRET>
 *
 * Called by the Django backend after any content save.
 * Instantly clears the Next.js ISR cache for the homepage.
 *
 * Set REVALIDATE_SECRET in both .env.local and backend .env
 */
import { revalidatePath } from 'next/cache'
import { NextRequest, NextResponse } from 'next/server'

const SECRET = process.env.REVALIDATE_SECRET ?? 'nxtlab-revalidate-2025'

export async function POST(req: NextRequest) {
  const secret = req.nextUrl.searchParams.get('secret')

  if (secret !== SECRET) {
    return NextResponse.json({ error: 'Invalid secret' }, { status: 401 })
  }

  try {
    revalidatePath('/')
    return NextResponse.json({ revalidated: true, timestamp: new Date().toISOString() })
  } catch (err) {
    return NextResponse.json({ error: 'Revalidation failed' }, { status: 500 })
  }
}
