import * as serviceService from '../services/service.service.js';

/**
 * Get all services
 * GET /api/services
 */
export const getAllServices = async (req, res, next) => {
  try {
    const services = await serviceService.getAllServices();

    res.status(200).json({
      success: true,
      count: services.length,
      data: services,
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Get service by ID
 * GET /api/services/:serviceId
 */
export const getServiceById = async (req, res, next) => {
  try {
    const { serviceId } = req.params;

    const service = await serviceService.getServiceById(serviceId);

    res.status(200).json({
      success: true,
      data: service,
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Create new service
 * POST /api/services
 * Body: { name, price, description }
 */
export const createService = async (req, res, next) => {
  try {
    const userId = req.userId;
    const data = req.body;

    const service = await serviceService.createService(userId, data);

    res.status(201).json({
      success: true,
      data: service,
      message: 'Služba bola vytvorená',
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Update service
 * PUT /api/services/:serviceId
 * Body: { name, price, description }
 */
export const updateService = async (req, res, next) => {
  try {
    const { serviceId } = req.params;
    const data = req.body;

    const service = await serviceService.updateService(serviceId, data);

    res.status(200).json({
      success: true,
      data: service,
      message: 'Služba bola aktualizovaná',
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Delete service
 * DELETE /api/services/:serviceId
 */
export const deleteService = async (req, res, next) => {
  try {
    const { serviceId } = req.params;

    const result = await serviceService.deleteService(serviceId);

    res.status(200).json({
      success: true,
      message: result.message,
    });
  } catch (error) {
    next(error);
  }
};
