<?php

namespace App\Http\Controllers\Admin\Settings\Quiz;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class UserLevelSettingController extends Controller
{
    public function index()
    {
        adminUserHasPermission('read');

        $buttons = [
            [
                'label' => __('Add New Ranking Level'),
                'icon' => 'ph ph-plus',
                'type' => 'modal',
                'id' => 'create-quiz-ranking-level-modal',
                'href' => route('admin.settings.quiz-ranking-settings.store'),
            ],
        ];

        $levels = config('application_info.quiz_ranking_levels');

        $levels = array_map(function ($level, $key) {
            return [
                'id' => $key + 1,
                ...$level,
            ];
        }, $levels, array_keys($levels));

        $columns = [
            [
                'label' => __('Title'),
                'key' => 'title',
            ],
            [
                'label' => __('Minimum Score'),
                'key' => 'min_score',
            ],
            [
                'label' => __('Maximum Score'),
                'key' => 'max_score',
            ],
            ['label' => __('Action'), 'render' => function ($item) {
                $action_buttons = [
                    [
                        'label' => __('Edit'),
                        'icon' => 'ph ph-pencil',
                        'id' => 'edit_quiz_ranking_level',
                        'row' => json_encode($item),
                        'type' => 'modal',
                        'href' => route('admin.settings.quiz-ranking-settings.update', $item['id']),
                    ],
                    [
                        'label' => __('Delete'),
                        'icon' => 'ph ph-trash',
                        'type' => 'delete',
                        'href' => route('admin.settings.quiz-ranking-settings.destroy', $item['id']),
                    ],
                ];

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

        ];

        return view('admin.pages.settings.quiz.labels.index', compact('columns', 'levels', 'buttons'));
    }

    public function store(Request $request)
    {
        adminUserHasPermission('edit');

        $validated = $request->validate([
            'min_score' => 'required|numeric|min:0',
            'max_score' => 'required|numeric|min:'.$request->min_score + 1,
            'title' => 'required|string|max:255',
        ]);

        $levels = config('application_info.quiz_ranking_levels');
        $levels = [...$levels, $validated];

        storeOption([
            'quiz_ranking_levels' => $levels,
        ]);

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

    public function update(Request $request, $key)
    {
        adminUserHasPermission('edit');

        $validated = $request->validate([
            'min_score' => 'required|numeric|min:0',
            'max_score' => 'required|numeric|min:'.$request->min_score + 1,
            'title' => 'required|string|max:255',
        ]);

        $levels = config('application_info.quiz_ranking_levels');
        $levels[$key - 1] = $validated;

        storeOption([
            'quiz_ranking_levels' => $levels,
        ]);

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

    public function destroy($key)
    {
        adminUserHasPermission('edit');

        $levels = config('application_info.quiz_ranking_levels');
        unset($levels[$key - 1]);

        storeOption([
            'quiz_ranking_levels' => $levels,
        ]);

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