<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Models\BusinessInformation;
use App\Models\Order;


class APIController extends Controller
{
    public function index(){
        $users = User::all();
        return response()->json(["users" => $users], 200);        
    }
    public function business_information(){
        $businessinformation = BusinessInformation::select('client_id')->get();
        $clientIds = $businessinformation->pluck('client_id')->toArray();

        // Fetch data from Order table using the client IDs
        $orderDetail = Order::whereIn('orders.client_id', $clientIds)
        ->join('business_information','business_information.client_id','=','orders.client_id')
         ->join('products','products.id','=','orders.product_id')
         ->join('users','users.id','=','orders.client_id')
         ->join('closers','closers.id','=','business_information.closerid')
        ->select('*')
         ->get();  

        return response()->json(["businessinformation" => $orderDetail], 200);        
    }

    public function single_order_detail(Request $request){
        $order_number = Order::select('client_id')
                                ->where('ordernumber',$request->ocn)
                                ->get();
        
        // Fetch data from Order table using the client IDs
        $orderDetail = Order::whereIn('orders.client_id', $order_number)
        ->join('business_information','business_information.client_id','=','orders.client_id')
         ->join('products','products.id','=','orders.product_id')
         ->join('users','users.id','=','orders.client_id')
         ->join('closers','closers.id','=','business_information.closerid')
        ->select('*')
         ->get();  

        return response()->json(["single-order-detail" => $orderDetail], 200);  
    }
}
