const axios = require('axios');
const fs = require('fs');

async function create_image(host, access_token, file_name, file_path) {
    try {
        const create = await axios.post(`${host}/api/drive/files/create`, {
            name: file_name,
            file: fs.createReadStream(file_path)
        }, {
            headers: {
                'Content-Type': 'multipart/form-data',
                'Authorization': `Bearer ${access_token}`
            }
        })

        return create.data;
    } catch (error) {
        if (error.response) {
            return error.response.data;
        } else if (error.request) {
            return error.request;
        } else {
            return error.message;
        }
    }
}

async function create_note(host, access_token, text, media_ids, visibility) {
    try {
        const create = await axios.post(`${host}/api/notes/create`, {
            text: text,
            mediaIds: media_ids, 
            visibility: visibility
        }, {
            headers: {
                'Authorization': `Bearer ${access_token}`
            }
        });
    
        return create.data;
    } catch (error) {
        if (error.response) {
            return error.response.data;
        } else if (error.request) {
            return error.request;
        } else {
            return error.message;
        }
    }
}

module.exports.create_image = create_image;
module.exports.create_note = create_note;