<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\Note;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;

class NoteController extends Controller
{
    use BaseApiResponse;

    public function create(Request $request)
    {
        $validated = $request->validate([
            'art_id' => 'required',
            'note' => 'required',
            'color_code' =>  'nullable'
        ]);

        Note::query()->create(
            [
                'user_id' => auth()->user()->id,
                'art_id' => $validated['art_id'],
                'note' => $validated['note'],
                'color_code' => $validated['color_code'],

            ]
        );

        return $this->success([], 'successfully created.');
    }

    public function update($id, Request $request)
    {
        $validated = $request->validate([
            'note' => 'required',
            'color_code' =>  'nullable'
        ]);

        $note = Note::findOrFail($id);
        $note->update([
            'note' => $validated['note'],
            'color_code' => $validated['color_code'],
        ]);

        return $this->success([], 'successfully updated.');
    }

    public function destroy($id)
    {
        $note = Note::findOrFail($id);
        $note->delete();

        return $this->success([], 'successfully deleted.');
    }
}
