<?php

namespace App\Http\Controllers\Manager;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Note;
use Illuminate\Support\Facades\Auth;

class ManagerNote extends Controller
{
    /**
     * Get all notes created by the authenticated user.
     */
    public function index()
    {
        // Fetch notes only created by the authenticated user
        $notes = Note::where('author_id', Auth::id())->get();
        return view('manager.notes.index', compact('notes'));
    }

    /**
     * Store or update a note.
     */
    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'required|string',
        ]);

        // If editing, update the note
        if ($request->id) {
            // Get all note IDs first, then filter by the user's ownership
            $note = Note::find($request->id);
            if (!$this->isNoteOwnedByUser($note->id)) {
                abort(403);
            }

            $note->update([
                'title' => $request->title,
                'description' => $request->description,
            ]);
        } else {
            // Otherwise, create a new note
            Note::create([
                'title' => $request->title,
                'description' => $request->description,
                'author_id' => Auth::id(),
            ]);
        }

        return response()->json(['success' => 'Note saved successfully.']);
    }

    /**
     * Show a specific note.
     */
    public function show($id)
    {
        // First, find the note by its ID in the database
        $note = Note::find($id);
    
        // If the note doesn't exist or the authenticated user isn't the author, return an error
        if (!$note || $note->author_id !== Auth::id()) {
            return response()->json([
                'message' => 'You do not have access to this note.',
            ], 403); // Return a 403 Forbidden error if unauthorized
        }
    
        // If the user owns the note, return the note details
        return response()->json($note);
    }
    

    /**
     * Update a note.
     */
    public function update(Request $request, $id)
    {
        // Find the note by its ID
        $note = Note::find($id);
    
        // Check if the note exists and belongs to the authenticated user
        if (!$note || $note->author_id !== Auth::id()) {
            return response()->json([
                'message' => 'You do not have access to update this note.',
            ], 403); // Return a 403 Forbidden error if unauthorized
        }
    
        // Validate the incoming request
        $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'required|string',
        ]);
    
        // Update the note if it belongs to the user
        $note->update([
            'title' => $request->title,
            'description' => $request->description,
        ]);
    
        // Return success message
        return response()->json(['success' => 'Note updated successfully.']);
    }
    

    /**
     * Delete a note.
     */
    public function destroy($id)
    {
        // Find the note by its ID
        $note = Note::find($id);
    
        // Check if the note exists and belongs to the authenticated user
        if (!$note || $note->author_id !== Auth::id()) {
            return response()->json([
                'message' => 'You do not have access to delete this note.',
            ], 403); // Return a 403 Forbidden error if unauthorized
        }
    
        // Delete the note if it belongs to the user
        $note->delete();
    
        // Return success message
        return response()->json(['success' => 'Note deleted successfully.']);
    }
    

    /**
     * Helper method to check if the note is owned by the authenticated user.
     */
    private function isNoteOwnedByUser($noteId)
    {
        $note = Note::find($noteId);

        // Check if the note exists and belongs to the authenticated user
        return $note && $note->author_id === Auth::id();
    }
}
