<?php

namespace App\Modules\Stock;

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

class StockGroupsControllerREST extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        $query = StockGroup::query()->select('*');

        // Conditional statements based on GET parameters
        if ($request->input('name')) {
            $query = $query->where('name', 'like', $request->input('name').'%');
        }

        // Search on all columns
        if ($request->input('query')) {
            $query = $query
                        ->where('name', 'like', '%'.$request->input('query').'%');
        }

        // Apply limits and execute the query
        $query = $query->orderBy('name')->paginate($request->input('limit', 20));

        $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)
    {
        $stock = new StockGroup();

        $stock->fill($request->all());

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

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

        if ($group) {
            return response()->json($group->toArray());
        } else {
            return response()->json(['error' => 'Not found.'], 404);
        }
    }

    /**
     * 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, Request $request)
    {
        $group = StockGroup::find($id);

        if (! $group) {
            return response()->json(['error' => 'Not found.'], 404);
        }

        $group->fill($request->all());

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        Stock::query()->where('group_id', $id)->update(['group_id' => null]);
        if (! StockGroup::find($id)->delete()) {
            return response()->json(['error' => 'Unable to delete category'], 400);
        }
    }
}
