const { DateTime } = require('luxon')

function dateTime() {
    const brazilTimeZone = 'America/Sao_Paulo'
    const currentDateBrazil = DateTime.now().setZone(brazilTimeZone) // Get current date and time in Brazil timezone

    const formattedDateTime = currentDateBrazil.toFormat("yyyy-MM-dd'T'HH:mm:ssZZ") // Format to ISO 8601

    console.log('FORMATTED DATE TIME: ', formattedDateTime)

    return formattedDateTime

}

function secondsFormat(seconds, format = '') {

    seconds = parseInt(seconds, 10)

    switch (format) {
        case 'hh:mm':
            const hours = Math.floor(seconds / 3600)
            const minutes = Math.floor((seconds % 3600) / 60)
            const formattedHours = String(hours).padStart(2, '0')
            const formattedMinutes = String(minutes).padStart(2, '0')
            console.log(`formate hours: ${formattedHours}:${formattedMinutes}`)
            return `${formattedHours}:${formattedMinutes}`
        case 'milliseconds':
            return seconds * 1000

    }

    return seconds

}

function getPastDateTimeFromSeconds(currentTime, secondsToSubtract) {

    const currentDateTime = DateTime.fromISO(currentTime)

    // Subtract seconds from the current date and time
    const pastDateTime = currentDateTime.minus({ seconds: secondsToSubtract })

    // Extract timezone offset from the current time
    const timezoneOffset = currentDateTime.toFormat('ZZ')

    // Format the past date and time in the desired format manually
    const formattedPastDateTime = pastDateTime.toFormat(`yyyy-MM-dd'T'HH:mm:ss${timezoneOffset}`)

    return formattedPastDateTime
}

function currentYearMonthDay() {
    const originalDate = 'YYYY-MM-DD'

    const parsedDate = new Date(originalDate)

    const today = new Date()

    parsedDate.setFullYear(today.getFullYear())
    parsedDate.setMonth(today.getMonth())
    parsedDate.setDate(today.getDate())

    return parsedDate.toISOString().split('T')[0]  
}



module.exports = { dateTime, secondsFormat, getPastDateTimeFromSeconds, currentYearMonthDay }
