const app = require(`../../../ticket/server/server`);
const server = require(`../../../loopback/server/server`);
const ParameterizedSQL = server.loopbackConnector.ParameterizedSQL;

describe('ticket ticketCalculateClon()', () => {
    const today = new Date();
    it('should add the ticket to the order containing the original ticket', async () => {
        let stmts = [];
        let stmt;

        stmts.push('START TRANSACTION');

        let params = {
            clientFk: 101,
            shipped: today,
            warehouseFk: 1,
            companyFk: 442,
            addressFk: 121,
            agencyType: 23,
            routeFk: 1,
            landed: today,
            userId: 21,
            originalTicketId: 11
        };

        stmt = new ParameterizedSQL('CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result)', [
            params.clientFk,
            params.shipped,
            params.warehouseFk,
            params.companyFk,
            params.addressFk,
            params.agencyType,
            params.routeFk,
            params.landed,
            params.userId
        ]);
        stmts.push(stmt);

        stmt = new ParameterizedSQL('CALL vn.ticketCalculateClon(@result, ?)', [params.originalTicketId]);
        stmts.push(stmt);

        let orderIndex = stmts.push(`SELECT * FROM vn.orderTicket WHERE ticketFk = @result`) - 1;
        let angencyHourIndex = stmts.push(`SELECT * FROM tmp.agencyHourGetShipped`) - 1;

        stmts.push('ROLLBACK');

        let sql = ParameterizedSQL.join(stmts, ';');
        let result = await app.models.Ticket.rawStmt(sql);

        let expectedOrder = 11;
        let newestTicketIdInFixtures = 21;

        expect(result[orderIndex][0].orderFk).toEqual(expectedOrder);
        expect(result[orderIndex][0].ticketFk).toBeGreaterThan(newestTicketIdInFixtures);
        expect(result[angencyHourIndex][0].warehouseFk).toEqual(1);
        expect(result[angencyHourIndex][0].shipped).toBeDefined();
        expect(result[angencyHourIndex][0].landed).toBeDefined();
    });

    it('should add the ticket to the order containing the original ticket and generate landed value if it was null', async () => {
        let stmts = [];
        let stmt;

        stmts.push('START TRANSACTION');

        let params = {
            clientFk: 101,
            shipped: today,
            warehouseFk: 1,
            companyFk: 442,
            addressFk: 121,
            agencyType: 23,
            routeFk: 1,
            landed: null,
            userId: 21,
            originalTicketId: 11
        };

        stmt = new ParameterizedSQL('CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result)', [
            params.clientFk,
            params.shipped,
            params.warehouseFk,
            params.companyFk,
            params.addressFk,
            params.agencyType,
            params.routeFk,
            params.landed,
            params.userId
        ]);
        stmts.push(stmt);

        stmt = new ParameterizedSQL('CALL vn.ticketCalculateClon(@result, ?)', [params.originalTicketId]);
        stmts.push(stmt);

        let orderIndex = stmts.push(`SELECT * FROM vn.orderTicket WHERE ticketFk = @result`) - 1;
        let angencyHourIndex = stmts.push(`SELECT * FROM tmp.agencyHourGetShipped`) - 1;

        stmts.push('ROLLBACK');

        let sql = ParameterizedSQL.join(stmts, ';');
        let result = await app.models.Ticket.rawStmt(sql);

        let expectedOrder = 11;
        let newestTicketIdInFixtures = 21;

        expect(result[orderIndex][0].orderFk).toEqual(expectedOrder);
        expect(result[orderIndex][0].ticketFk).toBeGreaterThan(newestTicketIdInFixtures);
        expect(result[angencyHourIndex][0].warehouseFk).toEqual(1);
        expect(result[angencyHourIndex][0].shipped).toBeDefined();
        expect(result[angencyHourIndex][0].landed).toBeDefined();
    });
});