module.exports = function(Self) {
    Self.remoteMethod('getCard', {
        description: 'Get client basic data and debt',
        accepts: {
            arg: 'id',
            type: 'number',
            required: true,
            description: 'The client id',
            http: {source: 'path'}
        },
        returns: {
            type: 'Object',
            root: true
        },
        http: {
            verb: 'GET',
            path: '/:id/getCard'
        }
    });

    Self.getCard = async function(id) {
        let client = await Self.findOne({
            where: {
                id: id
            },
            include: [
                {
                    relation: 'salesPerson',
                    scope: {
                        fields: ['id', 'firstName', 'name']
                    }
                }, {
                    relation: 'contactChannel',
                    scope: {
                        fields: ['id', 'name']
                    }
                }, {
                    relation: 'province',
                    scope: {
                        fields: ['id', 'name']
                    }
                }, {
                    relation: 'country',
                    scope: {
                        fields: ['id', 'country']
                    }
                }, {
                    relation: 'payMethod',
                    scope: {
                        fields: ['id', 'name']
                    }
                }, {
                    relation: 'account',
                    scope: {
                        fields: ['id', 'name', 'active']
                    }
                }
            ]
        });

        let query = `SELECT vn.clientGetDebt(?, CURDATE()) AS debt`;
        client.debt = (await Self.rawSql(query, [id]))[0].debt;

        return client;
    };
};