module.exports = Self => {
    Self.remoteMethod('threeLastActive', {
        description: 'Returns the last three tickets of a client that have the alertLevel at 0 and the shiped day is gt today',
        accessType: '',
        accepts: [{
            arg: 'filter',
            type: 'object',
            required: true,
            description: 'client id, ticketFk'
        }],
        returns: {
            type: [this.modelName],
            root: true
        },
        http: {
            path: `/threeLastActive`,
            verb: 'GET'
        }
    });

    Self.threeLastActive = async params => {
        let query = `
            SELECT t.id,t.shipped,a.name AS agencyName,w.name AS warehouseName
                FROM vn.ticket t
                    JOIN vn.ticketState ts ON t.id = ts.ticketFk
                    JOIN vn.agencyMode a ON t.agencyModeFk = a.id
                    JOIN vn.warehouse w ON t.warehouseFk = w.id
                WHERE t.shipped > CURDATE() AND t.clientFk = ? AND ts.alertLevel = 0 AND t.id <> ?
                ORDER BY t.shipped
                LIMIT 3`;
        let tickets = await Self.rawSql(query, [params.clientFk, params.ticketFk]);
        return tickets;
    };
};