<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Template;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class TemplateController extends Controller
{
    /**
     * Display a listing of personal templates.
     */
    public function index()
    {
        $templates = Template::with('creator')
            ->where('created_by', auth()->id())
            ->latest()
            ->paginate(15);

        return response()->json([
            'status' => 'success',
            'data' => $templates
        ]);
    }

    /**
     * Store a newly created template in storage.
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255|unique:templates,name',
            'categories' => 'required|array',
            'categories.*' => 'integer|exists:categories,id'
        ]);

        if ($validator->fails()) {
            return response()->json([
                'status' => 'error',
                'message' => 'Validation Error',
                'errors' => $validator->errors()
            ], 422);
        }

        $template = Template::create([
            'name' => $request->name,
            'created_by' => auth()->id(),
        ]);

        $template->categories()->sync($request->categories);

        return response()->json([
            'status' => 'success',
            'message' => 'Template created successfully',
            'data' => $template->load('categories')
        ], 210);
    }

    /**
     * Display the specified template.
     */
    public function show(Template $template)
    {
        if ($template->created_by !== auth()->id()) {
            return response()->json([
                'status' => 'error',
                'message' => 'Unauthorized access'
            ], 403);
        }

        return response()->json([
            'status' => 'success',
            'data' => $template->load('categories')
        ]);
    }

    /**
     * Update the specified template in storage.
     */
    public function update(Request $request, Template $template)
    {
        if ($template->created_by !== auth()->id()) {
            return response()->json([
                'status' => 'error',
                'message' => 'Unauthorized access'
            ], 403);
        }
// dd($request->all());
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255|unique:templates,name,' . $template->id,
            'categories' => 'required|array',
            'categories.*' => 'integer|exists:categories,id'
        ]);

        if ($validator->fails()) {
            return response()->json([
                'status' => 'error',
                'message' => 'Validation Error',
                'errors' => $validator->errors()
            ], 422);
        }

        $template->update([
            'name' => $request->name,
        ]);

        $template->categories()->sync($request->categories);

        return response()->json([
            'status' => 'success',
            'message' => 'Template updated successfully',
            'data' => $template->load('categories')
        ]);
    }

    /**
     * Remove the specified template from storage.
     */
    public function destroy(Template $template)
    {
        if ($template->created_by !== auth()->id()) {
            return response()->json([
                'status' => 'error',
                'message' => 'Unauthorized access'
            ], 403);
        }

        $template->delete();

        return response()->json([
            'status' => 'success',
            'message' => 'Template deleted successfully'
        ]);
    }
}
