const asyncHandler = require('../middlewares/asyncHandler');
const {
  getAllTicketsTypes,
  getTicketsTypeById,
  createTicketsType,
  updateTicketsType,
  deleteTicketsType
} = require('../repositories/ticketsTypeRepository');

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

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

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

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

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

