const asyncHandler = require('../middlewares/asyncHandler');
const {
  getAllQuestionTypes,
  getQuestionTypeById,
  createQuestionType,
  updateQuestionType,
  deleteQuestionType
} = require('../repositories/questionTypeRepository');

// Get all question types
exports.getAllQuestionTypes = asyncHandler(async (req, res) => {
    const rows = await getAllQuestionTypes();
    res.status(200).json({ success: true, data: rows });
});

// Get question type by ID
exports.getQuestionTypeById = asyncHandler(async (req, res) => {
    const { id } = req.params;
    const type = await getQuestionTypeById(id);
    
    if (!type) {
        return res.status(404).json({ success: false, message: 'Question type not found' });
    }
    
    res.status(200).json({ success: true, data: type });
});

// Create question type
exports.createQuestionType = asyncHandler(async (req, res) => {
    const { question_type_id, sat_question_type } = req.body;
    
    if (!question_type_id || !sat_question_type) {
        return res.status(400).json({ success: false, message: 'question_type_id and sat_question_type are required' });
    }
    
    await createQuestionType(question_type_id, sat_question_type);
    res.status(201).json({ success: true, message: 'Question type created successfully' });
});

// Update question type
exports.updateQuestionType = asyncHandler(async (req, res) => {
    const { id } = req.params;
    const { sat_question_type } = req.body;
    
    if (!sat_question_type) {
        return res.status(400).json({ success: false, message: 'sat_question_type is required' });
    }
    
    const updated = await updateQuestionType(id, sat_question_type);
    
    if (!updated) {
        return res.status(404).json({ success: false, message: 'Question type not found' });
    }
    
    res.status(200).json({ success: true, message: 'Question type updated successfully' });
});

// Delete question type
exports.deleteQuestionType = asyncHandler(async (req, res) => {
    const { id } = req.params;
    const deleted = await deleteQuestionType(id);
    
    if (!deleted) {
        return res.status(404).json({ success: false, message: 'Question type not found' });
    }
    
    res.status(200).json({ success: true, message: 'Question type deleted successfully' });
});

