<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\QuestionBankForm;
use App\Models\QuestionBankSection;
use App\Models\QuestionBankQuestion;
use Illuminate\Support\Facades\Validator;

class QuestionBankController extends Controller
{
    // ============ Forms ============
    
    public function getForms()
    {
        $forms = QuestionBankForm::with(['sections.questions' => function ($query) {
            $query->orderBy('order');
        }])->get();
        return response()->json($forms);
    }

    public function storeForm(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
        ]);

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

        $form = QuestionBankForm::create($validator->validated());
        
        return response()->json([
            'message' => 'Form created successfully',
            'form' => $form
        ], 201);
    }

    public function updateForm(Request $request, $id)
    {
        $form = QuestionBankForm::findOrFail($id);
        
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
        ]);

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

        $form->update($validator->validated());
        
        return response()->json([
            'message' => 'Form updated successfully',
            'form' => $form
        ]);
    }

    public function deleteForm($id)
    {
        $form = QuestionBankForm::findOrFail($id);
        $form->delete();
        
        return response()->json(['message' => 'Form deleted successfully']);
    }

    // ============ Sections ============
    
    public function getSections()
    {
        $sections = QuestionBankSection::with(['form', 'questions'])->get();
        return response()->json($sections);
    }

    public function storeSection(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'form_id' => 'required|exists:question_bank_forms,id',
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'order' => 'nullable|integer',
        ]);

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

        $section = QuestionBankSection::create($validator->validated());
        
        return response()->json([
            'message' => 'Section created successfully',
            'section' => $section->load('form')
        ], 201);
    }

    public function updateSection(Request $request, $id)
    {
        $section = QuestionBankSection::findOrFail($id);
        
        $validator = Validator::make($request->all(), [
            'form_id' => 'required|exists:question_bank_forms,id',
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'order' => 'nullable|integer',
        ]);

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

        $section->update($validator->validated());
        
        return response()->json([
            'message' => 'Section updated successfully',
            'section' => $section->load('form')
        ]);
    }

    public function deleteSection($id)
    {
        $section = QuestionBankSection::findOrFail($id);
        $section->delete();
        
        return response()->json(['message' => 'Section deleted successfully']);
    }

    // ============ Questions ============
    
    public function getQuestions()
    {
        $questions = QuestionBankQuestion::with('section.form')->get();
        return response()->json($questions);
    }

    public function storeQuestion(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'section_id' => 'required|exists:question_bank_sections,id',
            'parent_question_id' => 'nullable|exists:question_bank_questions,id',
            'condition_value' => 'nullable|string',
            'display_order' => 'nullable|integer',
            'question_text' => 'required|string',
            'question_type' => 'required|string',
            'options' => 'nullable|array',
            'required' => 'boolean',
            'order' => 'nullable|integer',
        ]);

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

        $question = QuestionBankQuestion::create($validator->validated());
        
        return response()->json([
            'message' => 'Question created successfully',
            'question' => $question->load('section.form')
        ], 201);
    }

    public function updateQuestion(Request $request, $id)
    {
        $question = QuestionBankQuestion::findOrFail($id);
        
        $validator = Validator::make($request->all(), [
            'section_id' => 'required|exists:question_bank_sections,id',
            'parent_question_id' => 'nullable|exists:question_bank_questions,id',
            'condition_value' => 'nullable|string',
            'display_order' => 'nullable|integer',
            'question_text' => 'required|string',
            'question_type' => 'required|string',
            'options' => 'nullable|array',
            'required' => 'boolean',
            'order' => 'nullable|integer',
        ]);

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

        $question->update($validator->validated());
        
        return response()->json([
            'message' => 'Question updated successfully',
            'question' => $question->load('section.form')
        ]);
    }

    public function deleteQuestion($id)
    {
        $question = QuestionBankQuestion::findOrFail($id);
        $question->delete();
        
        return response()->json(['message' => 'Question deleted successfully']);
    }
}
