import { addCompanyPaymentMethod, addDirectBill } from './factory/direct_bill';
import { getTripData, getBookingData, getExistingBookingDetails } from './factory/trip';

const newUserCreds = Cypress.env('newUser').NEW_USER_PASSWORD;

Cypress.Commands.add(
  'createDirectBillPaymentMethod',
  (rentalCarOperator: string, directBillNumber: string) => {
    cy.task('getDataFromCache', 'userResponse').then((response: any) => {
      addCompanyPaymentMethod(response).then(() =>
        addDirectBill(rentalCarOperator, directBillNumber)
      );
    });
  }
);

function loginAndRetrieveData(): Cypress.Chainable<string> {
  cy.clearLocalStorage();
  cy.clearCookies();
  return cy.task<string>('getDataFromCache', 'userEmail').then((email: string) => {
    return cy.loginWithAuthToken({ email: email, password: newUserCreds, isSA: true });
  });
}

Cypress.Commands.add('expectAutomaticTransmissionISPresentInBooking', () => {
  loginAndRetrieveData().then(() => getTripData());
});

Cypress.Commands.add('expectDirectBillBookingIsSuccessful', (directBillName: string) => {
  loginAndRetrieveData().then(() => getBookingData(directBillName));
});

Cypress.Commands.add('validateExistingBookingDetails', () => {
  loginAndRetrieveData().then(() => getExistingBookingDetails());
});
