<?php

namespace App\Http\Controllers;

use App\Models\Loan;
use App\Models\User;
use App\Models\Book;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Requests\StoreLoanRequest;
use App\Http\Requests\UpdateLoanRequest;

class LoanController extends Controller {

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

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

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

    /**
     * Display a search listing of the resource.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function search(Request $request) {
        $oldSearch = $request->search;
        $loans = null;

        if (Auth::user()->hasRole('Administrator')) {
            $loans = Loan::whereHas('user', function ($query) use ($request) {
                        $query->where('name', 'like', '%' . $request->search . '%')
                                ->orWhere('email', 'like', '%' . $request->search . '%');
                    })->orWhereHas('book', function ($query) use ($request) {
                        $query->where('title', 'like', '%' . $request->search . '%')
                                ->orWhere('authors', 'like', '%' . $request->search . '%')
                                ->orWhere('editorial', 'like', '%' . $request->search . '%');
                    })->paginate(5);
        } else {
            $loans = Loan::whereHas('user', function ($query) {
                        $query->where('id', Auth::user()->id);
                    })->whereHas('book', function ($query) use ($request) {
                        $query->where('title', 'like', '%' . $request->search . '%')
                                ->orWhere('authors', 'like', '%' . $request->search . '%')
                                ->orWhere('editorial', 'like', '%' . $request->search . '%');
                    })->paginate(5);
        }

        return view('loans.index', compact('oldSearch', 'loans'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        $users = User::all();
        $books = Book::whereNull('document')->get();

        return view('loans.create', compact('users', 'books'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreLoanRequest $request) {
        $input = $request->validated();

        Loan::create($input);

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

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Loan  $loan
     * @return \Illuminate\Http\Response
     */
    public function edit(Loan $loan) {
        $users = User::all();
        $books = Book::all();

        return view('loans.edit', compact('loan', 'users', 'books'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Loan  $loan
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateLoanRequest $request, Loan $loan) {
        $input = $request->validated();

        $loan->update($input);

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

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

}
