<?php

namespace App\Http\Controllers;

use App\Models\TaskTemplate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class TaskTemplateController extends Controller
{
    public function index()
    {
        $templates = Auth::user()->taskTemplates()->get();
        return response()->json($templates);
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'title' => 'nullable|string|max:255',
            'description' => 'nullable|string',
            'priority' => 'nullable|string',
            'tags_ids' => 'nullable|array',
        ]);

        $template = Auth::user()->taskTemplates()->create($validated);

        return response()->json($template);
    }

    public function destroy(TaskTemplate $template)
    {
        if ($template->user_id !== Auth::id()) abort(403);
        $template->delete();
        return response()->json(['success' => true]);
    }
}
