<?php

namespace App\Modules\Bookings;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\BaseController;

class DeliveryControllerRESTv1 extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        $query = Repair::select('tasks.*')->repairs()
                    ->where('needs_delivery', '=', 1)
                    ->where('delivered', '=', 0)
                    ->with('delivery_user', 'user');

        if ($request->get('results')) {
            switch ($request->get('results')) {
                case 'all':
                    // $query = $query->where('assigned_to', '=', Auth::user()->id)->where('status_id', '!=', 10)->where('status_id', '!=', 11);
                    break;
                case 'own':
                    $query = $query->where('delivered_by', '=', $request->get('user_id', Auth::user()->id));
            }
        }

        if ($request->get('completed')) {
            $query->completed();
        }

        // Sort results based on priority
        $query->join('severities', 'severities.id', '=', 'tasks.severity_id', 'left')
              ->orderBy('tasks.client_id', 'asc')
              ->orderBy('severities.priority', 'desc')
              ->orderBy('tasks.client_type', 'desc')
              ->orderBy('tasks.id', 'asc');

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

        foreach ($query as $value) {
            // Load all the clients
            // unfortuately eager loading is not available
            // for polymorphic relationships at the moment..
            if ($request->get('client') && $request->get('client') != 'false') {
                $value->client;
            }
        }

        $response = [
            'success'      => true,
            'data'         => $query->getCollection()->toArray(),
            '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)
    {
    }

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

        $task->delivered = $request->get('delivered');

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

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