const UserError = require('vn-loopback/util/user-error');
const fs = require('fs-extra');
const path = require('path');

module.exports = Self => {
    Self.remoteMethodCtx('uploadFile', {
        description: 'Uploads a file and inserts into dms model',
        accessType: 'WRITE',
        accepts: [
            {
                arg: 'warehouseId',
                type: 'number',
                description: 'The warehouse id',
                required: true
            }, {
                arg: 'companyId',
                type: 'number',
                description: 'The company id',
                required: true
            }, {
                arg: 'dmsTypeId',
                type: 'number',
                description: 'The dms type id',
                required: true
            }, {
                arg: 'reference',
                type: 'string',
                required: true
            }, {
                arg: 'description',
                type: 'string',
                required: true
            }, {
                arg: 'hasFile',
                type: 'boolean',
                description: 'True if has an attached file',
                required: true
            }],
        returns: {
            type: 'object',
            root: true
        },
        http: {
            path: `/uploadFile`,
            verb: 'POST'
        }
    });

    Self.uploadFile = async(ctx, options) => {
        const models = Self.app.models;
        const TempContainer = models.TempContainer;
        const fileOptions = {};
        const args = ctx.args;

        let tx;
        const myOptions = {};

        if (typeof options == 'object')
            Object.assign(myOptions, options);

        if (!myOptions.transaction) {
            tx = await Self.beginTransaction({});
            myOptions.transaction = tx;
        }

        let srcFile;
        try {
            const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId, myOptions);
            if (!hasWriteRole)
                throw new UserError(`You don't have enough privileges`);

            // Upload file to temporary path
            const tempContainer = await TempContainer.container('dms');
            const uploaded = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
            const files = Object.values(uploaded.files).map(file => {
                return file[0];
            });

            const addedDms = [];
            for (const uploadedFile of files) {
                const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
                srcFile = path.join(file.client.root, file.container, file.name);

                const data = {
                    workerFk: ctx.req.accessToken.userId,
                    dmsTypeFk: args.dmsTypeId,
                    companyFk: args.companyId,
                    warehouseFk: args.warehouseId,
                    reference: args.reference,
                    description: args.description,
                    contentType: uploadedFile.type,
                    hasFile: args.hasFile
                };
                const extension = await models.DmsContainer.getFileExtension(uploadedFile.name);
                const newDms = await Self.createFromFile(data, extension, srcFile, myOptions);
                addedDms.push(newDms);
            }

            if (tx) await tx.commit();

            return addedDms;
        } catch (e) {
            if (tx) await tx.rollback();

            if (fs.existsSync(srcFile))
                await fs.unlink(srcFile);

            throw e;
        }
    };
};