const asyncHandler = require('../middlewares/asyncHandler');
const {
  getAllAccountStatuses,
  getAccountStatusById,
  createAccountStatus,
  updateAccountStatus,
  deleteAccountStatus
} = require('../repositories/accountStatusRepository');

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

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

// Create account status
exports.createAccountStatus = asyncHandler(async (req, res) => {
    const { status_id, status_name } = req.body;
    
    if (!status_id || !status_name) {
        return res.status(400).json({ success: false, message: 'status_id and status_name are required' });
    }
    
    await createAccountStatus(status_id, status_name);
    res.status(201).json({ success: true, message: 'Account status created successfully' });
});

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

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

