import { NextRequest, NextResponse } from 'next/server';
import { syncProgressTracker } from '@/lib/services/sync-progress-tracker';

/**
 * GET /api/sync/progress
 * Get sync progress for a specific sync or entity type
 */
export async function GET(request: NextRequest) {
  try {
    const searchParams = request.nextUrl.searchParams;
    const syncId = searchParams.get('syncId');
    const entityType = searchParams.get('entityType');

    if (syncId) {
      // Get specific sync progress
      const progress = syncProgressTracker.getProgress(syncId);
      if (!progress) {
        return NextResponse.json(
          { error: 'Sync not found' },
          { status: 404 }
        );
      }
      return NextResponse.json({ progress });
    }

    if (entityType) {
      // Get latest sync for entity type
      const progress = syncProgressTracker.getLatestSync(entityType);
      if (!progress) {
        return NextResponse.json(
          { error: 'No sync found for entity type' },
          { status: 404 }
        );
      }
      return NextResponse.json({ progress });
    }

    // Get all active syncs
    const activeSyncs = syncProgressTracker.getActiveSyncs();
    return NextResponse.json({ activeSyncs });

  } catch (error) {
    console.error('Error fetching sync progress:', error);
    return NextResponse.json(
      { error: 'Failed to fetch sync progress' },
      { status: 500 }
    );
  }
}
