<?php

namespace App\Http\Controllers;

use App\Models\Note;

use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;

class NoteController extends Controller
{

    public function __construct() {
        $this->middleware('can:note.index')->only('index','show');
        $this->middleware('can:note.create')->only('create','store');
        $this->middleware('can:note.edit')->only('edit','update');
        $this->middleware('can:note.destroy')->only('destroy');
    }

    public function index(): View
    {
        $notes = Note::all();
        return view('note.index',compact('notes'));
    }

    public function create(): View
    {
        return view('note.create');
    }

    public function store(Request $request)
    {
        $user = Auth::user();
        $request['user_id']=$user->id;
        $note= Note::create($request->all());
        return redirect()->route('note.index',compact('note'))->with('info','Se creó la nota correctamente');
    }

    public function edit(Note $note)
    {
        return view('note.edit',compact('note'));
    }

    public function update(Request $request,Note $note)
    {
        $note->update($request->all());
        return redirect()->route('note.edit',compact('note'))->with('info','Se creó la nota correctamente');
    }

    public function show(Note $note)
    {
        return view('note.show',compact('note'));
    }

    public function destroy(Note $note)
    {
        $note->delete();
        return redirect()->route('note.index');
    }

}
