<?php

namespace App\Http\Controllers\Admin\Quiz;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Quiz\QuizCategoryRequest;
use App\Models\QuizCategory;
use App\Services\ModalIndexQuey;
use Illuminate\Support\Facades\Cache;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        adminUserHasPermission(permission: 'read');

        $buttons = [
            [
                'label' => __('Add New Quiz Category'),
                'icon' => 'ph ph-plus-bold',
                'type' => 'modal',
                'id' => 'quiz-category-create',
                'href' => route('admin.quiz-categories.store'),
            ],
        ];

        $quiz_categories = ModalIndexQuey::get(model: QuizCategory::query());

        $columns = [
            [
                'label' => __('Title'),
                'key' => 'title',
                'header_class' => 'lg:w-[350px]',
            ],
            [
                'label' => __('Icon'),
                'key' => 'icon',
                'render' => fn ($quiz_category) => '<img class="w-8 h-8" src="'.$quiz_category->icon.'"/>',
            ],
            [
                'label' => __('Action'),
                'render' => function ($quiz_category) {
                    $action_buttons = [
                        [
                            'label' => __('Edit'),
                            'icon' => 'ph ph-pencil',
                            'type' => 'modal',
                            'id' => 'quiz-category-edit',
                            'row' => $quiz_category,
                            'href' => route('admin.quiz-categories.update', $quiz_category->slug),
                        ],
                        [
                            'label' => __('Delete'),
                            'icon' => 'ph ph-trash',
                            'type' => 'delete',
                            'href' => route('admin.quiz-categories.destroy', $quiz_category->slug),
                        ],
                    ];

                    return view('admin.components.table-action', compact('action_buttons'))->render();
                },
            ],
        ];

        return view('admin.pages.quiz.categories.index', compact('quiz_categories', 'columns', 'buttons'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(QuizCategoryRequest $request)
    {
        adminUserHasPermission(permission: 'create');

        QuizCategory::create($request->validated());

        Cache::forget('quiz_categories');

        return response()->json([
            'message' => __('Category was successfully created'),
            'redirect' => route('admin.quiz-categories.index'),
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(QuizCategoryRequest $request, QuizCategory $quizCategory)
    {
        adminUserHasPermission(permission: 'edit');

        $quizCategory->update($request->validated());

        Cache::forget('quiz_categories');

        return response()->json([
            'message' => __('Category was successfully updated'),
            'reload' => true,
        ]);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(QuizCategory $quizCategory)
    {
        adminUserHasPermission(permission: 'delete');

        $quizCategory->delete();

        Cache::forget('quiz_categories');

        return response()->json([
            'message' => __('Category was successfully deleted'),
            'reload' => true,
        ]);
    }
}
