const asyncHandler = require('../middlewares/asyncHandler');
const {
  getAllRpnLikelihood,
  createRpnLikelihood,
  updateRpnLikelihood,
  deleteRpnLikelihood,
  getAllRpnSeverity,
  createRpnSeverity,
  updateRpnSeverity,
  deleteRpnSeverity
} = require('../repositories/rpnRepository');

// Get all RPN likelihood of harm
exports.getAllRpnLikelihood = asyncHandler(async (req, res) => {
    const rows = await getAllRpnLikelihood();
    res.status(200).json({ success: true, data: rows });
});

// Create RPN likelihood of harm
exports.createRpnLikelihood = asyncHandler(async (req, res) => {
    const { likelihood_of_harm, value } = req.body;
    
    if (!likelihood_of_harm || value === undefined) {
        return res.status(400).json({ success: false, message: 'likelihood_of_harm and value are required' });
    }
    
    await createRpnLikelihood(likelihood_of_harm, value);
    res.status(201).json({ success: true, message: 'RPN likelihood created successfully' });
});

// Update RPN likelihood of harm
exports.updateRpnLikelihood = asyncHandler(async (req, res) => {
    const { likelihood_of_harm, value } = req.body;
    
    if (value === undefined) {
        return res.status(400).json({ success: false, message: 'value is required' });
    }
    
    const updated = await updateRpnLikelihood(likelihood_of_harm, value);
    
    if (!updated) {
        return res.status(404).json({ success: false, message: 'RPN likelihood not found' });
    }
    
    res.status(200).json({ success: true, message: 'RPN likelihood updated successfully' });
});

// Delete RPN likelihood of harm
exports.deleteRpnLikelihood = asyncHandler(async (req, res) => {
    const { likelihood_of_harm } = req.params;
    const deleted = await deleteRpnLikelihood(likelihood_of_harm);
    
    if (!deleted) {
        return res.status(404).json({ success: false, message: 'RPN likelihood not found' });
    }
    
    res.status(200).json({ success: true, message: 'RPN likelihood deleted successfully' });
});

// Get all RPN severity of harm
exports.getAllRpnSeverity = asyncHandler(async (req, res) => {
    const rows = await getAllRpnSeverity();
    res.status(200).json({ success: true, data: rows });
});

// Create RPN severity of harm
exports.createRpnSeverity = asyncHandler(async (req, res) => {
    const { severity_of_harm, value } = req.body;
    
    if (!severity_of_harm || value === undefined) {
        return res.status(400).json({ success: false, message: 'severity_of_harm and value are required' });
    }
    
    await createRpnSeverity(severity_of_harm, value);
    res.status(201).json({ success: true, message: 'RPN severity created successfully' });
});

// Update RPN severity of harm
exports.updateRpnSeverity = asyncHandler(async (req, res) => {
    const { severity_of_harm, value } = req.body;
    
    if (value === undefined) {
        return res.status(400).json({ success: false, message: 'value is required' });
    }
    
    const updated = await updateRpnSeverity(severity_of_harm, value);
    
    if (!updated) {
        return res.status(404).json({ success: false, message: 'RPN severity not found' });
    }
    
    res.status(200).json({ success: true, message: 'RPN severity updated successfully' });
});

// Delete RPN severity of harm
exports.deleteRpnSeverity = asyncHandler(async (req, res) => {
    const { severity_of_harm } = req.params;
    const deleted = await deleteRpnSeverity(severity_of_harm);
    
    if (!deleted) {
        return res.status(404).json({ success: false, message: 'RPN severity not found' });
    }
    
    res.status(200).json({ success: true, message: 'RPN severity deleted successfully' });
});

