<?php

namespace App\Http\Controllers\Admin\Contest;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Contest\ContestCategoryRequest;
use App\Models\ContestCategory;
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 Contest Category'),
                'icon' => 'ph ph-plus-bold',
                'type' => 'modal',
                'id' => 'contest-category-create',
                'href' => route('admin.contest-categories.store'),
            ],
        ];

        $contest_categories = ModalIndexQuey::get(model: ContestCategory::query());

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

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

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

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

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

        Cache::forget('contest_categories');

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

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

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

        Cache::forget('contest_categories');

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

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

        $contestCategory->delete();

        Cache::forget('contest_categories');

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