import { NextRequest, NextResponse } from 'next/server';
import { getAutotaskClient } from '@/lib/services/autotask-factory';

export async function GET(request: NextRequest) {
  try {
    const client = getAutotaskClient();
    const searchParams = request.nextUrl.searchParams;
    const email = searchParams.get('email');

    if (email) {
      const resource = await client.getResourceByEmail(email);
      return NextResponse.json({ resource });
    }

    const resources = await client.getAllResources();
    return NextResponse.json({ resources });
  } catch (error) {
    console.error('Error fetching resources:', error);
    return NextResponse.json(
      { error: error instanceof Error ? error.message : 'Failed to fetch resources' },
      { status: 500 }
    );
  }
}
