// ${VOCAB.brief} API
export const dynamic = 'force-dynamic';

import { NextRequest, NextResponse } from 'next/server';
import {
  generateFamiliarBrief,
  generateBriefHTML,
  sendWeeklyBrief,
} from '@/lib/order-to-cash/familiar-brief';
import { VOCAB } from '@/lib/white-label/constants';

export async function GET() {
  try {
    const brief = await generateFamiliarBrief();
    return NextResponse.json(brief);
  } catch (error) {
    console.error('[Brief API] Error:', error);
    return NextResponse.json({ error: 'Failed to generate brief' }, { status: 500 });
  }
}

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const { action, recipientEmail, format } = body;
    
    switch (action) {
      case 'generate':
        const brief = await generateFamiliarBrief();
        if (format === 'html') {
          const html = generateBriefHTML(brief);
          return new NextResponse(html, {
            headers: { 'Content-Type': 'text/html' },
          });
        }
        return NextResponse.json(brief);
      
      case 'send':
        const sent = await sendWeeklyBrief(recipientEmail);
        return NextResponse.json({
          success: sent,
          message: sent ? 'Brief sent successfully' : 'Failed to send brief',
        });
      
      default:
        return NextResponse.json({ error: 'Invalid action' }, { status: 400 });
    }
  } catch (error) {
    console.error('[Brief API] Error:', error);
    return NextResponse.json({ error: 'Failed to process brief action' }, { status: 500 });
  }
}
