<?php
namespace App\Http\Controllers\API\Customer;

use App\Http\Controllers\Controller;
use App\Models\Order;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CustomerOrderController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $orders = Order::with(['vendor:id,store_name,slug', 'items.product:id,name,thumbnail'])
            ->where('user_id', $request->user()->id)
            ->when($request->status, fn($q) => $q->where('status', $request->status))
            ->latest()->paginate(10);
        return response()->json($orders);
    }

    public function show(Request $request, int $id): JsonResponse
    {
        $order = Order::with(['vendor', 'items.product', 'coupon'])
            ->where('user_id', $request->user()->id)->findOrFail($id);
        return response()->json(['order' => $order]);
    }
}
