import { NextRequest, NextResponse } from 'next/server';

const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8001';

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const { url, business_name, business_address, rating_snapshot, total_reviews_snapshot, scraper_version, session_id, browser_fingerprint, geolocation } = body;

    if (!url) {
      return NextResponse.json({ error: 'URL is required' }, { status: 400 });
    }

    // Build metadata object
    const metadata: Record<string, unknown> = {
      business_name,
      business_address,
      rating_snapshot,
      total_reviews_snapshot,
      scraper_version,  // Store in metadata for job tracking
    };

    // Include session_id for browser reuse (session handoff from validation)
    if (session_id) {
      metadata.session_id = session_id;
    }

    // Include browser fingerprint if provided
    if (browser_fingerprint) {
      metadata.browser_fingerprint = browser_fingerprint;
    }
    if (geolocation) {
      metadata.geolocation = geolocation;
    }

    // Call the containerized scraper API with business metadata and version
    const response = await fetch(`${API_BASE_URL}/scrape`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        url,
        scraper_version,  // Pass version to backend for routing
        session_id,  // Pass session_id for browser reuse
        metadata,
      }),
    });

    const data = await response.json();

    if (!response.ok) {
      return NextResponse.json(
        { error: data.detail || 'Failed to start scraping' },
        { status: response.status }
      );
    }

    return NextResponse.json(data);
  } catch (error) {
    console.error('Scrape API error:', error);
    return NextResponse.json(
      { error: 'Failed to connect to scraper API' },
      { status: 500 }
    );
  }
}
