<?php

namespace App\Modules\Stock;

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

class StockItemControllerRESTv1 extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        $query = StockItem::join('stock', 'stock_items.stock_id', '=', 'stock.id')->select('stock_items.*');

        // Conditional statements based on GET parameters
        if ($request->input('query')) {
            $query = $query->where(function ($query) {
                $query->where('stock.manufacturer_code', 'like', $request->input('query').'%')->orWhere('stock.name', 'like', '%'.request()->get('query').'%');
            });
        }
        if ($request->input('service')) {
            $query = $query->service();
        }
        if ($request->input('supplier')) {
            $query = $query->with('supplier');
        }
        if ($request->input('category') && $request->input('category') != 'false') {
            $query->where('stock.group_id', $request->input('category'));
        }

        // Request a booking service item
        if ($request->input('booking')) {
            $query = $query->service()->where('stock.name', 'like', '%booking%');
        }
        $query = $query->with('stock', 'stock.group');

        // Apply limits and execute the query
        // $query = $query->take(Input::get('limit', 20))->skip(Input::get('skip', 0))->get();
        $query = $query->paginate(request()->get('limit', 20));

        foreach ($query as $value) {
            $value->price = [
                'client'   => $value->cost + ($value->cost * $value->business_margin / 100),
                'customer' => $value->cost + ($value->cost * $value->margin / 100),
            ];
        }

        $response = [
            'success'      => true,
            'data'         => $query->getCollection()->toArray(),
            'total'        => $query->total(),
            'current_page' => $query->currentPage(),
            'last_page'    => $query->lastPage(),
            'from'         => $query->firstItem(),
            'to'           => $query->lastItem(),
        ];

        return response()->json($response);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(Request $request)
    {
        if (! $request->input('stock_id')) {
            return response()->json(['error' => 'Could not find stock item.'], 400);
        }

        $item = StockItem::create($request->all());

        if ($item->id) {
            return response()->json($item->toArray());
        } else {
            return response()->json($item->errors, 400);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function show($id)
    {
        $item = StockItem::find($id);

        if (! $item) {
            return response()->json(['error' => 'Stock item not found'], 404);
        }

        $item->price = [
            'client'   => $item->cost + ($item->cost * $item->business_margin / 100),
            'customer' => $item->cost + ($item->cost * $item->margin / 100),
        ];

        $item->load('stock', 'supplier');

        return response()->json($item->toArray());
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function update($id)
    {
        $item = StockItem::find($id);
        $item->fill(request()->all());

        if ($item->save()) {
            return response()->json($item->toArray());
        } else {
            return response()->json(['errors' => $item->errors->all()], 400);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}
