<?php

namespace App\Http\Controllers\Api\V1\Entrepreneur;

use App\Http\Controllers\Controller;
use App\Http\Resources\Api\V1\Vendor\Order\VendorOrderIndexResource;
use App\Http\Resources\Api\V1\Vendor\Order\VendorOrderShowResource;
use App\Models\Ecommerce\Product\Order\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class EntrepreneurOrderController extends Controller
{
    public function index(Request $request)
    {
        $order_ids = DB::table('order_items')
            ->where('channel_id', auth()->user()->channel()->id)
            ->distinct()
            ->pluck('order_id');

        $perPage = $request->input('perPage') ?? 20;
        $page = $request->input('page') ?? 1;

        return response()->rest_paginate(
            VendorOrderIndexResource::collection(
                Order::query()
                    ->whereIntegerInRaw('orders.id', $order_ids)
                    ->with('paymentType')
                    ->latest()
                    ->paginate($perPage, ['*'], 'page', $page)
            )
        );
    }

    public function show(Order $order)
    {
        $order->load(['items' => ['product' => ['media']]]);

        return new VendorOrderShowResource($order);
    }
}
