// src/app/api/orders/[id]/route.ts
import { NextRequest, NextResponse } from 'next/server';

export async function GET(
  request: NextRequest,
  { params }:  { params: Promise<{ id: string }> }
) {
  
  try {
    const { id } = await params;
    
    // Make API request to backend
    const response = await fetch(`${process.env.BACKEND_URL }/api/v1/orders/${id}`, {
      method: 'GET',
      headers: {
        'Accept': 'application/json',
        // Add authorization if needed
        // 'Authorization': `Bearer ${token}`,
      },
    });
    
    // Check if the request was successful
    if (!response.ok) {
      const errorData = await response.json();
      return NextResponse.json(
        { message: errorData.message || 'Failed to fetch order' },
        { status: response.status }
      );
    }
    
    // Return the response data
    const data = await response.json();
    return NextResponse.json(data);
    
  } catch (error) {
    console.error('Error fetching order:', error);
    return NextResponse.json(
      { message: error instanceof Error ? error.message : 'An error occurred while fetching the order' },
      { status: 500 }
    );
  }
}

export async function PUT(
  request: NextRequest,
  { params }:  { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    const body = await request.json();
    
    // Make API request to backend
    const response = await fetch(`${process.env.BACKEND_URL }/api/v1/orders/${id}`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        // Add authorization if needed
        // 'Authorization': `Bearer ${token}`,
      },
      body: JSON.stringify(body),
    });
    
    // Check if the request was successful
    if (!response.ok) {
      const errorData = await response.json();
      return NextResponse.json(
        { message: errorData.message || 'Failed to update order' },
        { status: response.status }
      );
    }
    
    // Return the response data
    const data = await response.json();
    return NextResponse.json(data);
    
  } catch (error) {
    console.error('Error updating order:', error);
    return NextResponse.json(
      { message: error instanceof Error ? error.message : 'An error occurred while updating the order' },
      { status: 500 }
    );
  }
}

export async function DELETE(
  request: NextRequest,
  { params }:  { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    
    // Make API request to backend
    const response = await fetch(`${process.env.BACKEND_URL }/api/v1/orders/${id}`, {
      method: 'DELETE',
      headers: {
        'Accept': 'application/json',
        // Add authorization if needed
        // 'Authorization': `Bearer ${token}`,
      },
    });
    
    // Check if the request was successful
    if (!response.ok) {
      const errorData = await response.json();
      return NextResponse.json(
        { message: errorData.message || 'Failed to delete order' },
        { status: response.status }
      );
    }
    
    // Return the response data
    const data = await response.json();
    return NextResponse.json(data);
    
  } catch (error) {
    console.error('Error deleting order:', error);
    return NextResponse.json(
      { message: error instanceof Error ? error.message : 'An error occurred while deleting the order' },
      { status: 500 }
    );
  }
}
