<?php

namespace App\Http\Controllers\api\v1\User;

use App\Http\Controllers\Controller;
use App\Service\User\UserOrderService;
use App\Traits\JsonResponseTrait;
use Illuminate\Http\Request;

class UserOrderController extends Controller
{
    use JsonResponseTrait;
    public function __construct(protected UserOrderService $userOrderService) {}

    /**
     * Get the authenticated user's profile details.
     *
     * @return JsonResponseTrait
     */
    public function placeOrder(Request $request)
    {
        $authUser = $request->auth_user;

        // Get all order data from the request
        $orderData = $request->all();

        // Attach the authenticated user's ID
        $orderData['user_id'] = $authUser->user_id;

        // Remove the full auth_user object to keep payload clean
        unset($orderData['auth_user']);

        // Pass the cleaned data to the service
        $order = $this->userOrderService->placeOrder($orderData);

        // Return JSON response
        return response()->json([
            'status' => true,
            'message' => 'Order placed successfully',
            'data' => $order,
        ]);
    }
}
