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

export async function GET(request: NextRequest) {
  try {
    const searchParams = request.nextUrl.searchParams;
    const status = searchParams.get('status');
    const size = searchParams.get('size') ? parseInt(searchParams.get('size')!) : 10;

    let quotes;
    
    if (status === 'open') {
      quotes = await salesBldrClient.getOpenQuotes(size);
    } else if (status === 'approved') {
      const approvedQuotes = await salesBldrClient.getApprovedQuotes(size);
      quotes = { results: approvedQuotes, total: approvedQuotes.length };
    } else {
      quotes = await salesBldrClient.getQuotes({
        size,
        sort: '-createdAt'
      });
    }

    return NextResponse.json(quotes);
  } catch (error) {
    console.error('Error fetching SalesBldr quotes:', error);
    return NextResponse.json(
      { error: 'Failed to fetch quotes', details: error instanceof Error ? error.message : 'Unknown error' },
      { status: 500 }
    );
  }
}
