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

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 storageConnector = Self.app.dataSources.storage.connector;
        const models = Self.app.models;
        const fileOptions = {};
        const args = ctx.args;

        let tx;
        let myOptions = {};

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

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

        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 getContainer('temp');
            const uploaded = await models.Container.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
            const files = Object.values(uploaded.files).map(file => {
                return file[0];
            });

            const addedDms = [];
            for (const file of files) {
                const newDms = await createDms(ctx, file, myOptions);
                const pathHash = storageConnector.getPathHash(newDms.id);
                const container = await getContainer(pathHash);

                const originPath = `${tempContainer.client.root}/${tempContainer.name}/${file.name}`;
                const destinationPath = `${container.client.root}/${pathHash}/${newDms.file}`;

                await fs.rename(originPath, destinationPath);

                addedDms.push(newDms);
            }

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

    async function createDms(ctx, file, myOptions) {
        const models = Self.app.models;
        const storageConnector = Self.app.dataSources.storage.connector;
        const myUserId = ctx.req.accessToken.userId;
        const myWorker = await models.Worker.findOne({where: {userFk: myUserId}}, myOptions);
        const args = ctx.args;

        const newDms = await Self.create({
            workerFk: myWorker.id,
            dmsTypeFk: args.dmsTypeId,
            companyFk: args.companyId,
            warehouseFk: args.warehouseId,
            reference: args.reference,
            description: args.description,
            contentType: file.type,
            hasFile: args.hasFile
        }, myOptions);

        let fileName = file.name;
        const extension = storageConnector.getFileExtension(fileName);
        fileName = `${newDms.id}.${extension}`;

        return newDms.updateAttribute('file', fileName, myOptions);
    }


    /**
     * Returns a container instance
     * If doesn't exists creates a new one
     *
     * @param {String} name Container name
     * @return {Object} Container instance
     */
    async function getContainer(name) {
        const models = Self.app.models;
        let container;
        try {
            container = await models.Container.getContainer(name);
        } catch (err) {
            if (err.code === 'ENOENT') {
                container = await models.Container.createContainer({
                    name: name
                });
            } else throw err;
        }

        return container;
    }
};