const {Email} = require('vn-print');

module.exports = Self => {
    Self.remoteMethodCtx('receiptEmail', {
        description: 'Returns the receipt pdf',
        accepts: [
            {
                arg: 'id',
                type: 'number',
                required: true,
                description: 'The claim id',
                http: {source: 'path'}
            },
            {
                arg: 'recipient',
                type: 'string',
                description: 'The recipient email',
                required: true,
            }
        ],
        returns: [
            {
                arg: 'body',
                type: 'file',
                root: true
            }, {
                arg: 'Content-Type',
                type: 'String',
                http: {target: 'header'}
            }, {
                arg: 'Content-Disposition',
                type: 'String',
                http: {target: 'header'}
            }
        ],
        http: {
            path: '/:id/receipt-email',
            verb: 'POST'
        }
    });

    Self.receiptEmail = async(ctx, id) => {
        const args = Object.assign({}, ctx.args);
        const params = {
            recipient: args.recipient,
            lang: ctx.req.getLocale()
        };

        delete args.ctx;
        for (const param in args)
            params[param] = args[param];

        const email = new Email('receipt', params);

        return email.send();
    };
};