<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Topic;

class TopicController extends Controller
{ public function index()
    {
        $topics = Topic::all();
        return view('admin.quiz.index', compact('topics'));

    }
    public function update(Request $request, $id)
    {
        $request->validate([
          'title' => 'required|string|max:255',
          'per_q_mark' => 'required|numeric|min:0',
          'timer' => 'nullable|numeric|min:0',
        ]);

        $topic = Topic::findOrFail($id);
        $input = $request->all();
        $topic->update($input);
        return back()->with('updated', 'Topic has been updated');
    }

    public function store(Request $request)
    {
        $request->validate([
          'title' => 'required|string|max:255',
          'per_q_mark' => 'required|numeric|min:0',
          'timer' => 'nullable|numeric|min:0',
        ]);

        $input = $request->all();
        Topic::create($input);
        return back()->with('added', 'Topic has been added');
    }
    public function destroy($id)
    {
        $topic = Topic::findOrFail($id);
        $topic->delete();
        return back()->with('deleted', 'Topic has been deleted');
    }

}