<?php

namespace App\Http\Controllers\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Repositories\Core\User\ShopCart;
use App\Repositories\Shop\Offers;
use App\Repositories\Shop\Orders;
use App\Repositories\Users;

class BasketController extends Controller
{
    public function getPrice(Request $request)
    {
        $offer_id = $request->input('offer_id');
        $quantity = $request->input('quantity') ?? 1;
        $price = Offers::getPrice($offer_id, $quantity)->price_taxed;
        return number_format($quantity * $price, 2);
    }

    public function addBasket(Request $request)
    {
        $offer_id = $request->input('offer_id');
        $quantity = $request->input('quantity') ?? 1;
        $update = $request->input('update') ?? false;
        return Offers::addBasket($offer_id, $quantity, $update);
    }
 
    public function modalBasket($offer_id, $quantity)
    {
        $data['offer'] = Offers::getFull($offer_id)->toArray();
        $data['basket'] = Offers::addBasket($offer_id, $quantity);
        return view('Shop.Baskets.partials.modalBasket', $data);
    }

    public function basket()
    {
        $data['basket'] = Offers::getBasket();
        return view('Shop.Baskets.basket', $data);
    }

    public function getBasket()
    {
        $data = ShopCart::getContent();
        return response()->json(['data' => $data, 'code' => '200']);
    }

    public function countBasket()
    {
        return ShopCart::count();
    }

    public function getSummary()
    {
        $data = ShopCart::getSummary();
        return response()->json(['data' => $data, 'code' => '200']);
    }

    public function order(Request $request)
    {
        ShopCart::clear();
        $data = $request->all();
        unset($data['_token']);
        $data['user_id'] = Users::getId();
        Orders::newOrder($data);
        return response()->json(['code' => '200']);
    }

    public function clearBasket()
    {
        return ShopCart::clear();
    }
}
