<?php

namespace App\Modules\GlobalOrder\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Ecommerce\Product\Order\Status\OrderStatus;
use App\Modules\GlobalOrder\Controllers\Requests\GlobalOrderStoreRequest;
use App\Modules\GlobalOrder\Models\GlobalOrder;
use Illuminate\Http\JsonResponse;

class GlobalOrderController extends Controller
{
    /**
     * Store resource
     */
    public function store(GlobalOrderStoreRequest $request): JsonResponse
    {
        $globalOrder = GlobalOrder::create([
            'customer_name' => $request->customer_name,
            'customer_phone' => $request->customer_phone,
            'customer_address' => $request->customer_address,
            'payment_type' => $request->payment_type,
            'notes' => $request->notes,
            'order_website' => $request->order_website,
            'status' => OrderStatus::default(),
            'user_id' => auth()->id(),
        ]);

        if ($request->hasFile('images')) {
            $globalOrder->addMultipleMediaFromRequest(['images'])
                ->each(function ($fileAdder) {
                    $fileAdder->toMediaCollection('uploads');
                });
        }

        return response()->rest([], 201, 'Created successfully');
    }
}
