const asyncHandler = require('../middlewares/asyncHandler');
const {
  getAllAuditStatuses,
  getAuditStatusById,
  createAuditStatus,
  updateAuditStatus,
  deleteAuditStatus
} = require('../repositories/auditStatusRepository');

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

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

// Create audit status
exports.createAuditStatus = asyncHandler(async (req, res) => {
    const { audit_status_id, audit_status_name } = req.body;
    
    if (!audit_status_id || !audit_status_name) {
        return res.status(400).json({ success: false, message: 'audit_status_id and audit_status_name are required' });
    }
    
    await createAuditStatus(audit_status_id, audit_status_name);
    res.status(201).json({ success: true, message: 'Audit status created successfully' });
});

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

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

