<?php
    include_once __dir__ . '/../core/LogEvent.php';

    class QuizzQuestion extends Core {

        function save() {

            $data       = $_POST;
            $description=  $data['description'] ?? '';
            $type       = $data['type'] ?? 'text';
            $options    = $data['options'] ?? [];
            $company_id = $data['companyId'];
            $quiz_id    = $data['quizId'] ?? null;

            if(is_null($quiz_id)) {
                http_response_code(400);
                echo json_encode(['error' => 'Quiz ID is required']);
                return;
            }

            if (empty($description)) {
                http_response_code(400);
                echo json_encode(['error' => 'Description is required']);
                return;
            }

            if ($type == 'choice' && empty($options)) {
                http_response_code(400);
                echo json_encode(['error' => 'Options are required for choice type quizzes']);
                return;
            }            

            $get_quiz_id = "SELECT id FROM quizzes WHERE company_id = :company_id AND id = :quiz_id ORDER BY id DESC LIMIT 1";
            $stmt = parent::$conn->prepare($get_quiz_id);
            $stmt->execute([
                'company_id' => $company_id, 
                'quiz_id' => $quiz_id
            ]);
            $quiz_result = $stmt->fetch(PDO::FETCH_ASSOC);
            $quiz_id = $quiz_result['id'] ?? null;
            
            $last_question_query = "SELECT MAX(question_order) as last_order FROM quiz_questions WHERE quiz_id = :quiz_id";
            $stmt = parent::$conn->prepare($last_question_query);
            $stmt->execute(['quiz_id' => $quiz_id]);
            $result = $stmt->fetch(PDO::FETCH_ASSOC);
            $question_order = ($result['last_order'] ?? 0) + 1;

            $question_insert = "INSERT INTO quiz_questions (quiz_id, question_order, question_text, question_type, options_json) 
                VALUES (:quiz_id, :question_order, :description, :type, :options)";
            $stmt = parent::$conn->prepare($question_insert);
            $stmt->execute([
                'quiz_id' => $quiz_id, 
                'question_order' => $question_order,
                'description' => $description,
                'type' => $type, 
                'options' => $options
            ]);

            $sql = "UPDATE quizzes SET total_questions = (total_questions + 1) WHERE id = :quiz_id";
            $stmt = parent::$conn->prepare($sql);
            $stmt->execute([
                'quiz_id' => $quiz_id
            ]);

            return ['type' => 'OK', 'data' => 'Quiz saved successfully'];

        }

        function list() {

            $company_id = $_GET['companyId'] ?? null;
            $quizId = $_GET['quizId'] ?? null;

            $sql_quizzes = "SELECT qq.id, qq.question_order, qq.question_text, qq.question_type, qq.options_json, qq.quiz_id
                FROM quiz_questions qq
                JOIN quizzes q ON qq.quiz_id = q.id
                WHERE q.company_id = :company_id AND q.id = :quizId
                ORDER BY qq.question_order ASC";

            $stmt = parent::$conn->prepare($sql_quizzes);
            $stmt->execute(['company_id' => $company_id, 'quizId' => $quizId]);
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);            

            return ['type' => 'OK', 'data' => $result];

        }

        function delete() {
            
            $id = $_GET['id'] ?? null;
            $quiz_id = $_GET['quizId'] ?? null;

            if (is_null($id)) {
                http_response_code(400);
                echo json_encode(['error' => 'Question ID is required']);
                return;
            }

            $delete_query = "DELETE FROM quiz_questions WHERE id = :id";
            $stmt = parent::$conn->prepare($delete_query);
            $stmt->execute(['id' => $id]);

            $sql_select = "UPDATE quizzes SET total_questions = (SELECT count(*) FROM quiz_questions WHERE quiz_id = :quiz_id) WHERE id = :quiz_id";
            $stmt = parent::$conn->prepare($sql_select);
            $stmt->execute(['quiz_id' => $quiz_id]);

            return ['type' => 'OK', 'data' => 'Question deleted successfully'];
        }

        function update() {

            $data = $_POST;
            $id = $data['id'] ?? null;
            $description = $data['description'] ?? null;
            $type = $data['type'] ?? null;
            $options = $data['options'] ?? null;

            if (is_null($id)) {
                http_response_code(400);
                echo json_encode(['error' => 'Question ID is required']);
                return;
            }

            if (is_null($description)) {
                http_response_code(400);
                echo json_encode(['error' => 'Description is required']);
                return;
            }

            if($type == 'choice' && is_null($options)) {
                http_response_code(400);
                echo json_encode(['error' => 'Options are required for choice type questions']);
                return;
            }

            $update_query = "UPDATE quiz_questions 
                SET question_text = :description, question_type = :type, options_json = :options
                WHERE id = :id";

            $stmt = parent::$conn->prepare($update_query);
            $stmt->execute([
                'description' => $description,
                'type' => $type,
                'options' => $options,
                'id' => $id
            ]);

            return ['type' => 'OK', 'data' => 'Question updated successfully'];

        }

    }