<?php

namespace App\Http\Controllers\Api\Admin;

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

class TopicQuestionTypeController extends Controller
{
    /**
     * Display All Types of Topic Question
    */
    public function index()
    {
        try {
            $questionTypes = TopicQuestionType::select('id','type')->latest()->get();
            if ($questionTypes->isEmpty()) {
                return response()->json([
                    // 'validation_errors' => ['not_found' => ['No Data Found!.']],
                    'response_status' => 400
                ], 200);
            }
            $data = ['questionTypes' => $questionTypes];
            return response()->json([
                'data' => $data,
                'response_status' => 200
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
    * Create Types of Topic Question
    */
    public function store(Request $request)
    {
        try {
            $validation = Validator::make($request->all(), [
                'type' => 'required|string',
            ]);

            if ($validation->fails()) {
                return response()->json([
                    'validation_errors' => $validation->messages(),
                    'response_status' => 400,
                ], 200);
            }

            $questionType = TopicQuestionType::create([
                'type' => $request->type,
            ]);

            $data = ['questionType' => $questionType];
            return response([
                'response_status' => 200,
                'response_message' => ['success' => ['Question Type Created Successfully!']],
                'data' => $data
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Display Single Types of Topic Question
    */
    public function show($id)
    {
        try {
            $questionType = TopicQuestionType::select('id','type')->find($id);
            if(!$questionType){
                return response()->json([
                    'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }
            $data = ['questionType' => $questionType];
            return response()->json([
                'data' => $data,
                'response_status' => 200
        ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Update Types of Topic Question.
    */
    public function update(Request $request, string $id)
    {
        try {
            $validation = Validator::make($request->all(), [
                'type' => 'required|string',
            ]);

            if ($validation->fails()) {
                return response()->json([
                    'validation_errors' => $validation->messages(),
                    'response_status' => 400,
                ], 200);
            }

            $questionType = TopicQuestionType::find($id);
            if(!$questionType){
                return response()->json([
                   'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }

            $questionType->type = $request->type;
            $questionType->save();

            $data = ['questionType' => $questionType];
            return response([
                'response_status' => 200,
                'response_message' => ['success' => ['Question Type Updated Successfully!']],
                'data' => $data
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
    * Remove Types of Topic Question.
    */
    public function destroy(string $id)
    {
        try {
            $questionType = TopicQuestionType::find($id);
            if(!$questionType){
                return response()->json([
                    'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }

            $questionType->delete();

            return response()->json([
                'response_message' => ['success' => ['Question Type Deleted Successfully!']],
                'response_status' => 200
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }
}
