import * as ics from 'ics';

interface IcsEventProps {
    title: string;
    description: string;
    start: Date;
    duration: { hours: number; minutes: number };
    location?: string;
    url?: string;
    organizer: { name: string; email: string };
    sequence?: number;
}

/**
 * Generates an iCalendar (.ics) file content string.
 */
export async function generateIcs(props: IcsEventProps): Promise<string> {
    const { title, description, start, duration, location, url, organizer } = props;

    const event: ics.EventAttributes = {
        start: [
            start.getFullYear(),
            start.getMonth() + 1,
            start.getDate(),
            start.getHours(),
            start.getMinutes()
        ],
        duration,
        title,
        description,
        location,
        url,
        status: 'CONFIRMED',
        busyStatus: 'BUSY',
        organizer,
        sequence: props.sequence
    };

    return new Promise((resolve, reject) => {
        ics.createEvent(event, (error, value) => {
            if (error) {
                return reject(error);
            }
            resolve(value);
        });
    });
}
