const { GymClass } = require('../models');

// @desc    Get all classes
// @route   GET /api/classes
// @access  Public
exports.getClasses = async (req, res) => {
  try {
    const classes = await GymClass.findAll();
    
    res.status(200).json(classes);
  } catch (error) {
    console.error('Error in getClasses:', error);
    res.status(500).json({
      message: 'Error retrieving classes',
      error: error.message
    });
  }
};

// @desc    Get single class
// @route   GET /api/classes/:id
// @access  Public
exports.getClass = async (req, res) => {
  try {
    const gymClass = await GymClass.findByPk(req.params.id);
    
    if (!gymClass) {
      return res.status(404).json({
        message: 'Class not found'
      });
    }
    
    res.status(200).json(gymClass);
  } catch (error) {
    console.error('Error in getClass:', error);
    res.status(500).json({
      message: 'Error retrieving class',
      error: error.message
    });
  }
};

// @desc    Create new class
// @route   POST /api/classes
// @access  Public
exports.createClass = async (req, res) => {
  try {
    const gymClass = await GymClass.create(req.body);
    
    res.status(201).json(gymClass);
  } catch (error) {
    console.error('Error in createClass:', error);
    res.status(500).json({
      message: 'Error creating class',
      error: error.message
    });
  }
};

// @desc    Update class
// @route   PUT /api/classes/:id
// @access  Public
exports.updateClass = async (req, res) => {
  try {
    const gymClass = await GymClass.findByPk(req.params.id);
    
    if (!gymClass) {
      return res.status(404).json({
        message: 'Class not found'
      });
    }
    
    await gymClass.update(req.body);
    
    res.status(200).json(gymClass);
  } catch (error) {
    console.error('Error in updateClass:', error);
    res.status(500).json({
      message: 'Error updating class',
      error: error.message
    });
  }
};

// @desc    Delete class
// @route   DELETE /api/classes/:id
// @access  Public
exports.deleteClass = async (req, res) => {
  try {
    const gymClass = await GymClass.findByPk(req.params.id);
    
    if (!gymClass) {
      return res.status(404).json({
        message: 'Class not found'
      });
    }
    
    await gymClass.destroy();
    
    res.status(204).send();
  } catch (error) {
    console.error('Error in deleteClass:', error);
    res.status(500).json({
      message: 'Error deleting class',
      error: error.message
    });
  }
};