<?php

namespace App\Http\Controllers\API\Lesson;

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

class KnowladgeController extends Controller
{
    public function index()
    {
        $knowladgeTest = KnowladgeTest::all();
        return [
            'data' => $knowladgeTest,
            'status' => 200,
            'message' => 'success',
        ];
    }
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'question' => 'required',
            'option1' => 'required',
            'option2' => 'required',
            'option3' => 'required',
            'option4' => 'required',

        ]);

        if ($validator->fails()) {
            return response()->json([
                'errors' => $validator->errors(),
                'status' => 401,
            ]);
        }


        $knowladgeTest = KnowladgeTest::create([
            'question' => $request->question,
            'option1' => $request->option1,
            'option2' => $request->option2,
            'option3' => $request->option3,
            'option4' => $request->option4,
            'day_id' => $request->day_id

        ]);
        return [
            'data' => $knowladgeTest,
            'status' => 200,
            'message' => 'success',
        ];
    }

    public function edit($id)
    {
        $knowladgeTest = KnowladgeTest::find($id);
        return [
            'data' => $knowladgeTest,
            'status' => 200,
            'message' => 'success',
        ];
    }

    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'question' => 'required',
            'option1' => 'required',
            'option2' => 'required',
            'option3' => 'required',
            'option4' => 'required',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'errors' => $validator->errors(),
                'status' => 401,
            ]);
        }


        $knowladgeTest = KnowladgeTest::find($id)->update([
            'question' => $request->question,
            'option1' => $request->option1,
            'option2' => $request->option2,
            'option3' => $request->option3,
            'option4' => $request->option4,
            'day_id' => $request->day_id

        ]);
        return [
            'data' => $knowladgeTest,
            'status' => 200,
            'message' => 'success',
        ];
    }
    public function destroy($id)
    {
        $knowladgeTest = KnowladgeTest::find($id)->delete();
        return [
            'data' => $knowladgeTest,
            'status' => 200,
            'message' => 'success',
        ];
    }
}
