<?php

namespace App\Http\Controllers;

use App\Models\Penalty;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class PenaltyController extends Controller {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        $penalties = null;

        if (Auth::user()->hasRole('Administrator')) {
            $penalties = Penalty::paginate(5);
        } else {
            $penalties = Penalty::whereHas('user', function ($query) {
                        $query->where('id', Auth::user()->id);
                    })->paginate(5);
        }

        return view('penalties.index', compact('penalties'));
    }

    /**
     * Display a search listing of the resource.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response or void
     */
    public function search(Request $request) {
        if (Auth::user()->hasRole('Administrator')) {
            $oldSearch = $request->search;
            $penalties = Penalty::whereHas('users', function ($user) use ($request) {
                        $user->where('name', 'like', '%' . $request->search . '%')
                                ->orWhere('email', 'like', '%' . $request->search . '%');
                    })->paginate(5);

            return view('penalties.index', compact('oldSearch', 'penalties'));
        } else {
            abort(403);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        $users = User::all();
        return view('penalties.create', compact('users'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        $input = $request->validate([
            'user_id' => 'required|numeric|integer',
            'end' => 'required|date'
        ]);

        Penalty::create($input);

        return redirect()->route('penalties.index')
                        ->with('success', 'Penalty added successfully.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Penalty  $penalty
     * @return \Illuminate\Http\Response
     */
    public function show(Penalty $penalty) {
        if (Auth::user()->hasRole('Administrator') 
                || $penalty->user_id === Auth::user()->id) {
            return view('penalties.show', compact('penalty'));
        } else {
            abort(401);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Penalty  $penalty
     * @return \Illuminate\Http\Response
     */
    public function edit(Penalty $penalty) {
        $users = User::all();
        return view('penalties.edit', compact('penalty', 'users'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Penalty  $penalty
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Penalty $penalty) {
        $input = $request->validate([
            'user_id' => 'required|numeric|integer|exists:users,id',
            'end' => 'required|date'
        ]);

        $penalty->update($input);

        return redirect()->route('penalties.index')
                        ->with('success', 'Penalty modified successfully.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Penalty  $penalty
     * @return \Illuminate\Http\Response
     */
    public function destroy(Penalty $penalty) {
        $penalty->delete();
        return redirect()->route('penalties.index')
                        ->with('success', 'Penalty removed successfully.');
    }

}
