const asyncHandler = require('../middlewares/asyncHandler');
const {
  getAllTicketsStatuses,
  getTicketsStatusById,
  createTicketsStatus,
  updateTicketsStatus,
  deleteTicketsStatus
} = require('../repositories/ticketsStatusRepository');

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

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

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

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

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

