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

/**
 * POST /api/wren/offset-orders
 * Proxy endpoint to create offset orders with Wren API
 * This keeps the WREN_API_TOKEN secure on the server
 *
 * Request body:
 * {
 *   tons: number,
 *   portfolioId: number,
 *   dryRun?: boolean,
 *   source?: string,
 *   note?: string
 * }
 */
export async function POST(request: NextRequest) {
  try {
    const apiToken = process.env.WREN_API_TOKEN;

    if (!apiToken) {
      console.error('WREN_API_TOKEN is not configured');
      return NextResponse.json(
        { error: 'Wren API is not configured' },
        { status: 500 }
      );
    }

    // Parse request body
    const body = await request.json();
    const { tons, portfolioId, dryRun = false, source, note } = body;

    // Validate required fields
    if (!tons || !portfolioId) {
      return NextResponse.json(
        { error: 'Missing required fields: tons and portfolioId' },
        { status: 400 }
      );
    }

    // Create offset order payload
    const orderPayload: any = {
      tons,
      portfolio_id: portfolioId,
      dry_run: dryRun,
    };

    if (source) orderPayload.source = source;
    if (note) orderPayload.note = note;

    // Make request to Wren API
    const response = await fetch('https://www.wren.co/api/offset_orders', {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${apiToken}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(orderPayload),
    });

    if (!response.ok) {
      const errorText = await response.text();
      console.error('Wren API error:', response.status, errorText);
      return NextResponse.json(
        { error: 'Failed to create offset order with Wren' },
        { status: response.status }
      );
    }

    const data = await response.json();
    return NextResponse.json(data);
  } catch (error) {
    console.error('Error creating Wren offset order:', error);
    return NextResponse.json(
      { error: error instanceof Error ? error.message : 'Failed to create offset order' },
      { status: 500 }
    );
  }
}
