const asyncHandler = require('../middlewares/asyncHandler');
const {
  getAllFormsTypes,
  getFormsTypeById,
  createFormsType,
  updateFormsType,
  deleteFormsType
} = require('../repositories/formsTypeRepository');

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

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

// Create form type
exports.createFormsType = asyncHandler(async (req, res) => {
    const { form_type_id, form_type } = req.body;
    
    if (!form_type_id || !form_type) {
        return res.status(400).json({ success: false, message: 'form_type_id and form_type are required' });
    }
    
    await createFormsType(form_type_id, form_type);
    res.status(201).json({ success: true, message: 'Form type created successfully' });
});

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

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

