import { NextResponse } from 'next/server';

import { withAuth } from '@/lib/api/helpers';
import { getEmailDraftResult } from '@/lib/services/email-draft.service';
import { errorResponse } from '@/lib/errors';

export const GET = withAuth(async (_req, _ctx, params) => {
  try {
    const { jobId } = params;
    if (!jobId) {
      return NextResponse.json({ error: 'jobId is required' }, { status: 400 });
    }

    const result = await getEmailDraftResult(jobId);

    if (result === null) {
      return NextResponse.json({ status: 'processing' });
    }

    return NextResponse.json({ status: 'complete', data: result });
  } catch (error) {
    return errorResponse(error);
  }
});
