<?php

namespace Modules\Order\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Order\Interfaces\OrderRepositoryInterface;
use Modules\Order\Models\Order;
use Modules\Order\Models\OrderProduct;
use Modules\Order\Models\OrderTrack;
use Modules\Order\Services\CartService;
use Modules\Product\Models\ProductAttribute;
use Modules\User\Services\AccountService;

class OrderRepository implements OrderRepositoryInterface
{
    public function get(array $request)
    {
        $orders = Order::with(['products','coupon','way','tracks'])->orderBy('id', 'DESC');

        return $orders;
    }

    public function getItem(int $id)
    {
        return Order::with(['products','coupon','way','tracks'])->find($id) ;
    }

    public function getItemTrashed(int $id)
    {
        return Order::withTrashed()->find($id) ;
    }

    public function checkItem(int $id)
    {
        return Order::find($id) ;
    }

    public function tracks(int $id)
    {
        return OrderTrack::where('order_id',$id)->get();
    }
    public function getProduct(int $id)
    {
        return OrderProduct::find($id);
    }
    public function productsStore(int $store_id,int $order_id)
    {
        return OrderProduct::where('store_id',$store_id)->where('order_id',$order_id)->get();
    }
    //
    public function create(array $request): Order
    {
        return Order::create($request);
    }
    public function update(array $request,Order $order): Order
    {
        $order->update($request);
        return $order;
    }
    public function createTrack(array $request)
    {
        OrderTrack::create($request);
    }

    public function saveData(array $request) :void
    {
        OrderProduct::create($request);
    }

    /**
     * Remove the specified client from storage.
     * @param Request $request
     * @param int $id
     * @return array
     */
    public function destroy(Order $order) :void
    {
        $order->delete();
    }

    /**
     * Remove several products by IDs.
     *
     * @param array $request
     * @return void
     */
    public function destroyAll(array $ids) :void
    {
        Order::whereIn('id', $ids)->delete();
    }

    /**
     * Restore the specified Order from storage.
     *
     * @param Order $order
     * @return void
     */
    public function restore(Order $order) :void
    {
        $order->restore();
    }

    /**
     * Restore several Orders by IDs.
     *
     * @param array $request
     * @return void
     */
    public function restoreAll(array $ids) :void
    {
        Order::whereIn('id', $ids)->onlyTrashed()->restore();
    }

    public function getItemByToken(string $token)
    {
        $order = Order::withTrashed()->where('token',$token)->first();
        return $order;
    }

}
