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 DmsContainer = models.DmsContainer;
        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 newDms = await createDms(ctx, uploadedFile, myOptions);
                const pathHash = DmsContainer.getHash(newDms.id);

                const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
                srcFile = path.join(file.client.root, file.container, file.name);

                const dmsContainer = await DmsContainer.container(pathHash);
                const dstFile = path.join(dmsContainer.client.root, pathHash, newDms.file);

                await fs.move(srcFile, dstFile, {
                    overwrite: true
                });

                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;
        }
    };

    async function createDms(ctx, file, myOptions) {
        const models = Self.app.models;
        const myUserId = ctx.req.accessToken.userId;
        const args = ctx.args;

        const newDms = await Self.create({
            workerFk: myUserId,
            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 = models.DmsContainer.getFileExtension(fileName);
        fileName = `${newDms.id}.${extension}`;

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