/**
 * Cancellation Controller
 * Handles HTTP requests for booking cancellations
 */

const cancellationService = require('../services/cancellation.service');
const { catchAsyncErrors } = require('../middlewares/catchAsyncErrors');
const { translateMessage } = require('../utils/translation.utils');

/**
 * POST /api/booking/:id/cancel
 * Cancel a booking with refund calculation
 */
exports.cancelBooking = catchAsyncErrors(async (req, res, next) => {
    const { id: bookingId } = req.params;
    const { reason } = req.body;
    const userId = req.user._id;
    const userRole = req.user.role;

    const result = await cancellationService.cancelBooking(
        bookingId,
        userId,
        userRole,
        reason
    );

    res.status(200).json({
        success: true,
        message: translateMessage('BOOKING_CANCELLED', req.language) || 'Booking cancelled successfully',
        ...result
    });
});

/**
 * GET /api/booking/:id/cancel/preview
 * Preview cancellation refund without actually cancelling
 */
exports.previewCancellation = catchAsyncErrors(async (req, res, next) => {
    const { id: bookingId } = req.params;

    const result = await cancellationService.previewCancellation(bookingId);

    res.status(200).json({
        success: true,
        message: 'Cancellation preview retrieved successfully',
        ...result
    });
});

/**
 * GET /api/vehicle-group/:id/cancellation-policy
 * Get cancellation policy for a vehicle group
 */
exports.getCancellationPolicy = catchAsyncErrors(async (req, res, next) => {
    const { id: vehicleGroupId } = req.params;

    const result = await cancellationService.getCancellationPolicy(vehicleGroupId);

    res.status(200).json({
        success: true,
        message: 'Cancellation policy retrieved successfully',
        ...result
    });
});
