const { format, toZonedTime } = require('date-fns-tz');
const { id } = require('date-fns/locale');

/**
 * Mengubah format tanggal & waktu lengkap ke format Indonesia (DD MMMM YYYY HH:mm:ss)
 * Contoh: "27 Juni 2025 21:35:46"
 */
const formatFullDateTime = (dateInput) => {
    if (!dateInput) return null;

    const timeZone = 'Asia/Jakarta';
    const zonedDate = toZonedTime(new Date(dateInput), timeZone);

    return format(zonedDate, 'dd MMMM yyyy HH:mm:ss', { locale: id });
};

/**
 * Mengubah hanya tanggal (tanpa jam) ke format YYYY-MM-DD
 * Contoh: "2025-06-27"
 */
const formatDateOnly = (dateInput) => {
    if (!dateInput) return null;

    const timeZone = 'Asia/Jakarta';
    const zonedDate = toZonedTime(new Date(dateInput), timeZone);

    return format(zonedDate, 'yyyy-MM-dd');
};

/**
 * Mengubah timestamp ISO ke format ISO tanpa zona waktu
 * Contoh: dari "2025-06-27T14:35:46.376Z" → "2025-06-27 21:35:46"
 */
const formatISOWithoutTimezone = (dateInput) => {
    if (!dateInput) return null;

    const timeZone = 'Asia/Jakarta';
    const zonedDate = toZonedTime(new Date(dateInput), timeZone);

    return format(zonedDate, "yyyy-MM-dd HH:mm:ss");
};

module.exports = {
    formatFullDateTime,
    formatDateOnly,
    formatISOWithoutTimezone,
};
