const Timeline = require('../models/timeline.model');

/**
 * Add an entry to the timeline
 * @param {string} referenceId - The ID of the object (e.g., Booking ID)
 * @param {Object} data - { title, status, description, referenceModel }
 */
exports.addTimelineEntry = async (referenceId, { title, status, description, referenceModel = 'Booking' }) => {
    try {
        const entry = new Timeline({
            referenceId,
            referenceModel,
            title,
            status,
            description
        });
        await entry.save();
        return entry;
    } catch (error) {
        console.error('Error adding timeline entry:', error);
        // We don't necessarily want to throw and break the main flow if timeline fails
        // but for now let's keep it simple.
        throw error;
    }
};

/**
 * Get all timeline entries for a reference ID
 * @param {string} referenceId - The ID of the object
 */
exports.getTimeline = async (referenceId) => {
    return await Timeline.find({ referenceId }).sort({ timestamp: 1 });
};
