import "@johnlindquist/kit"
const apiToken = await env("HABITICA_API_TOKEN");
const userID = await env("HABITICA_USER_ID");
const actionType = await arg("What would you like to do?", ["Browse", "Create new tasks"])
if(actionType == "Browse"){
const requestType = await arg("What would you like to browse?", ["Todos", "Dailies", "Habits"])
if(requestType == "Todos"){
    const todos = await getUserData("todos")
    
    const todoID = await arg("Which todo would you like to mark as done?", todos.data.map(({_id, text, checklist, updatedAt}) => ({
        name: text,
        value: _id,
        description: `Updated at ${updatedAt}`,
        preview: `
        <h3>${text}</h3>
        <p>${checklist?.map(({text}) => text) ?? ""}</p>
        `
    })))
    
    await markTaskDone(todoID)
    
    
    
}
else if(requestType == "Dailies"){
    const dailies = await getUserData("dailys")
    const dailyID = await arg("Which daily would you like to mark as done?", dailies.data.map(({_id, text, checklist, updatedAt}) => ({ 
        name: text,
        value: _id,
        description: `Updated at ${updatedAt}`,
        preview: `
        <h3>${text}</h3>
        <p>${checklist?.map(({text}) => text) ?? ""}</p>
        `
    })))
    await markTaskDone(dailyID)
}
else if(requestType == "Habits"){
    const habits = await getUserData("habits")
    const habitID = await arg("Which habit would you like to mark as done?", habits.data.map(({_id, text,  updatedAt}) => ({ 
        name: text,
        value: _id,
        description: `Updated at ${updatedAt}`,
        
        
        
        
    })))
    await markTaskDone(habitID)
}
}
else if(actionType == "Create new tasks"){
    const type = await arg("What type of task would you like to create?", ["Todo", "Daily", "Habit"])
    const text = await arg("What would you like the task to say?")
    if (type == "Habit"){
        const upOrDown = await arg("Would you like to create a positive or negative habit?", ["Positive", "Negative"])
        const upOrDownBool = upOrDown == "Positive" ? true : false
        await createTask(type, text, upOrDownBool)
    }
    else {
        await createTask(type, text)
    }
}
async function getUserData(type) {
    const response = await get(`https://habitica.com/api/v3/tasks/user?type=${type}`, {headers: {
        'x-client': `${userID}`,
        'x-api-user': `${userID}`,
        'x-api-key': `${apiToken}`
    }})
    return response.data
}
async function markTaskDone(id) {
    
    const response = await post(`https://habitica.com/api/v3/tasks/${id}/score/up`, {}, {headers: {
        'x-client': `${userID}`,
        'x-api-user': `${userID}`,
        'x-api-key': `${apiToken}`}})
    
}
async function createTask(type, text, habitDirection = undefined) {
    if (habitDirection == true){
        const response = await post(`https://habitica.com/api/v3/tasks/user?text=${text}&type=${type}&up=true`, {}, {headers: {
            'x-client': `${userID}`,
            'x-api-user': `${userID}`,
            'x-api-key': `${apiToken}`
        }})
    }
    else if(habitDirection == false){
        const response = await post(`https://habitica.com/api/v3/tasks/user?text=${encodeURI(text)}&type=${type}&down=true`, {}, {headers: {
            'x-client': `${userID}`,
            'x-api-user': `${userID}`,
            'x-api-key': `${apiToken}`
        }})
    }
    else{
        const response = await post(`https://habitica.com/api/v3/tasks/user?text=${encodeURI(text)}&type=${type}`, {}, {headers: {
            'x-client': `${userID}`,
            'x-api-user': `${userID}`,
            'x-api-key': `${apiToken}`
        }})
    }
}