<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Book;
use Illuminate\Http\Request;

class CategoryController extends Controller {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        $categories = Category::paginate(10);
        return view('categories.index', compact('categories'));
    }

    /**
     * Display a search listing of the resource.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function search(Request $request) {
        $oldSearch = $request->search;
        $categories = Category::where('name', 'like', '%' . $request->search . '%')
                ->paginate(10);

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

    /**
     * Display a search listing of content of the resource.
     *
     * @param \Illuminate\Http\Request $request
     * @param \App\Models\Category $category
     * @return \Illuminate\Http\Response
     */
    public function searchBooks(Request $request, Category $category) {
        $oldSearch = $request->search;
        $books = Book::where('category_id', $category->id)
                        ->where(function ($query) use($request) {
                            $query->where('title', 'like', '%' . $request->search . '%')
                            ->orWhere('authors', 'like', '%' . $request->search . '%')
                            ->orWhere('editorial', 'like', '%' . $request->search . '%');
                        })->paginate(6);
        return view('categories.show', compact('category', 'oldSearch', 'books'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        return view('categories.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        $input = $request->validate([
            'name' => 'required|string|max:255|unique:categories,name',
            'description' => 'nullable|string|max:1024'
        ]);

        Category::create($input);

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

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function show(Category $category) {
        $books = $category->books()->paginate(6);
        return view('categories.show', compact('category', 'books'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function edit(Category $category) {
        return view('categories.edit', compact('category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Category $category) {
        $input = $request->validate([
            'name' => 'required|string|max:255|unique:categories,name,' . $category->id,
            'description' => 'nullable|string|max:1024'
        ]);

        $category->update($input);

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

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

}
