import { LoginPage } from '@admin-lib/index';
import {
  TeamEventsInterceptApis,
  CreateEvent,
  EventDetails,
  EventsWorkflow,
  Invitations,
  EventWithTravelWindowsParams
} from '@group-events-lib/index';
import { Provider_Type, User_Type } from '@support-onboard/index';
import {
  Flights,
  FlightTripType,
  FlightCheckout,
  FlightInterceptApis
} from '@flight-lib/index';
import { FlightBookingUtility, FlightUtility } from '@flight-modal/index';

describe('GME: Team event: Book a flight with travel windows via team event', () => {
  beforeEach(() => {
    LoginPage.createNewCompanyAndLoginWithUserByType({
      providers: [Provider_Type.SABRE_FLIGHT],
      users: [User_Type.ADMIN]
    });

    // create second traveler to be able to send invites
    cy.task('getDataFromCache', 'userResponse').then((response: any) => {
      cy.addNewTraveler(response.companyDomain);
      cy.clearLocalStorage();
      LoginPage.loginWithAuthToken(
        response.email,
        Cypress.env('newUser').NEW_USER_PASSWORD,
        true
      );
    });

    TeamEventsInterceptApis.teamEventIntercept();
    FlightInterceptApis.interceptFlightsApis();
  });

  it(
    'C1153823: Book a roundtrip flight with travel windows',
    { tags: ['@tier2'] },
    () => {
      const params: EventWithTravelWindowsParams = {
        location: 'Zurich',
        eventName: 'Zurich event',
        eventStartDate: '12',
        eventEndDate: '15',
        departureDate: '10',
        returnDate: '17',
        latestArrivalTime: '8:00 AM',
        earliestReturningTime: '7:00 PM'
      };

      const flightBookingParams = FlightBookingUtility.initFlightBookingParams({
        locations: ['AMS - Schiphol Airport, Amsterdam, Netherlands'],
        flightTripType: FlightTripType.ROUNDTRIP,
        isItEventSearch: true
      });

      EventsWorkflow.createEventWithTravelWindows(params);
      EventDetails.visitHomePage();
      CreateEvent.openSearchPage();
      Invitations.verifyInvitationName(params.eventName);
      Invitations.verifyLatestArrivalTime(params.latestArrivalTime);
      Invitations.verifyEarliestReturningTime(params.earliestReturningTime);
      Invitations.verifyOnlyTravelDatesAreEnabled('9', '18');
      Flights.setOrigin(flightBookingParams.locations[0]);
      Invitations.selectTravelWindowsDates(params.departureDate, params.returnDate);
      Flights.searchFlight(true);
      FlightUtility.runChooseResultFlow(flightBookingParams);
      FlightCheckout.saveTripInformation();
      FlightCheckout.checkCancellationPolicy();
      FlightCheckout.updateContactDetails();
      FlightCheckout.checkIn('Skip');
      FlightCheckout.bookAndPay(flightBookingParams);
    }
  );
});
