const fs = require("node:fs")
const path = require("node:path")
const config = require("../config.json")

async function uploadSkin(filePath, variant, token) {
    const form = new FormData()
    form.append("variant", variant)
    form.append("file", fs.readFileSync(filePath), path.parse(filePath).base)
    const response = await fetch(`${config.minecraft.services.api.base}${config.minecraft.services.api.endpoints.uploadSkin}`, {
        method: "POST",
        body: form,
        headers: {
            Authorization: `Bearer ${token}`
        }
    })
    if (response.ok) {
        const json = await response.json()
        return json
    } else {
        throw new Error(`Error uploading skin: ${response.status} ${response.statusText}`)
    }
}

module.exports = {
    uploadSkin
}