<?php

namespace App\Modules\Bookings;

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

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

        if ($request->input('name')) {
            $query = $query->where('model_number', 'like', '%'.$request->input('name').'%');
        }

        if ($request->has('manufacturer_id')) {
            $query = $query->where('manufacturer_id', '=', $request->input('manufacturer_id'));
        }

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

        $results = $query->getCollection()->toArray();

        // If this is a query then we will add the requested item to the results
        if ($request->input('name')) {
            // Make sure the exact name doesn't exist in the given array
            $found = false;
            foreach ($results as $key => $value) {
                if (strcasecmp($value['model_number'], $request->input('name')) == 0) {
                    $found = true;
                    break;
                }
            }

            if (! $found) {
                array_unshift($results, ['model_number' => $request->input('name'), 'id' => null]);
            }
        }

        $response = [
            'success'      => true,
            'data'         => $results,
            'total'        => $query->total(),
            'current_page' => $query->currentPage(),
            'last_page'    => $query->lastPage(),
        ];

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

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function show($id)
    {
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function update($id)
    {
    }

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