<?php

namespace App\Http\Controllers;

use App\Models\Contact;
use App\Models\ContactNote;
use Illuminate\Http\Request;

class ContactNoteController extends Controller
{
    public function store(Request $request, Contact $contact)
    {
        $validated = $request->validate([
            'content' => 'required|string',
        ]);

        $contact->notes()->create([
            'content'    => $validated['content'],
            'created_by' => auth()->user()->name ?? 'Unknown',
        ]);

        $contact->logActivity('note_added', 'Note added');

        return back()->with('success', 'Note added.');
    }

    public function destroy(Contact $contact, ContactNote $note)
    {
        abort_unless($note->contact_id === $contact->id, 404);
        $note->delete();
        return back()->with('success', 'Note deleted.');
    }
}
