import { NextRequest, NextResponse } from 'next/server';
import { getMimecastClient } from '@/lib/services/mimecast-client';

export async function GET(req: NextRequest) {
  const emailAddress = req.nextUrl.searchParams.get('emailAddress');
  const domain = req.nextUrl.searchParams.get('domain');

  if (!emailAddress || !domain) {
    return NextResponse.json({ error: 'emailAddress and domain are required' }, { status: 400 });
  }

  try {
    const client = getMimecastClient();
    const user = await client.getCloudUser(emailAddress, domain);
    if (!user) {
      return NextResponse.json({ found: false });
    }
    return NextResponse.json({ found: true, user });
  } catch (err: any) {
    return NextResponse.json({ error: err.message }, { status: 500 });
  }
}
