import { GrowthInterceptApis } from '@growth-lib/api-intercept';
import { LoginSelfSell } from '@support-growth/login-self-sell';
import { SignUp } from '@growth-lib/sign-up';
import { AmplitudeEvents } from '@growth-lib/amplitude-events';
import { CashbackPages } from '@growth-lib/cashback-pages';
import { LoginPage } from '@admin-lib/login';
import { stagingUsers } from '@fixtures/staging-users';
import { OnboardByApi } from '@growth-lib/onboard-by-api';
import { GrowthRegistrationUrls } from '@growth-lib/model';
import { SignupReason } from '@growth-lib/persona';
import { OnboardingGuide } from '@growth-lib/onboarding-guide';

const growthUserCashback = stagingUsers.growth_cashback;
const authUserPassword = Cypress.env('newUser').NEW_USER_PASSWORD;

describe('Affiliate: new company on-boarding for cashback user', () => {
  let token: string;
  beforeEach(() => {
    GrowthInterceptApis.interceptApis();
    LoginSelfSell.createNewSelfSellLead({
      tag: 'growth-cashback-user',
      registrationUrl: GrowthRegistrationUrls.travel
    }).then((login) => {
      token = login.token;
    });
  });

  it(
    'C8357371: Web onboard as self sell user for cashback user and admin claim',
    { tags: ['@tier1'] },
    () => {
      OnboardByApi.onboardSelfSellAdmin(token, 'MARKETING_PAGE');
      OnboardByApi.loginWithOnboardedUser(true);
      OnboardingGuide.verifyOnboardingGuideOpen();
      OnboardingGuide.closeOnboardingGuide();
      CashbackPages.verifyHomePageShownWithCashback();
      CashbackPages.checkCashbackPillAmount(false);
      CashbackPages.clickOnCashbackPill();
      CashbackPages.verifyCashbackBalancePageShown();
      CashbackPages.verifyEmptyStateForNewUser();
      AmplitudeEvents.expectAmplitudeEventsToBeSent();
      OnboardByApi.deleteLastOnboardedCompany();
    }
  );

  it(
    'C9516516: Web onboard as self sell user cashback lead',
    { tags: ['@tier1'] },
    () => {
      SignUp.signupWithSkipAdmin(token, SignupReason.BOOK_TRIP, false);
      CashbackPages.verifyHomePageShownWithCashbackForRouge();
    }
  );

  it(
    'C9516517: Web growth verify cashback user with activity this data in pages',
    { tags: ['@tier1'] },
    () => {
      LoginPage.clearExistingLoginSession();
      LoginPage.visitTALoginPage();
      LoginPage.loginWithCredentials(growthUserCashback.email, authUserPassword);
      CashbackPages.verifyHomePageShownWithCashback();
      CashbackPages.navigateToAdminDashboard();
      CashbackPages.verifyAdminDashboard();
      CashbackPages.verifyCashbackBalancePageShown();
      CashbackPages.verifyCashbackBalancePageFunctionality();
    }
  );

  // in case this test fails, please check if the user has cashback balance
  // how to add balance:
  // https://navancorp.atlassian.net/wiki/spaces/RND/pages/3593371736/How+to+add+earned+cashback+manually
  // another reason can be this user is overloaded with too many redeems
  // in that case we might need to create new user or clean up this user's redeems
  it(
    'C9729259: Web growth check redeem cashback process and page refresh',
    { tags: ['@tier1'] },
    () => {
      LoginPage.clearExistingLoginSession();
      LoginPage.visitTALoginPage();
      LoginPage.loginWithCredentials(growthUserCashback.email, authUserPassword);
      CashbackPages.checkCashbackPillAmount(true);
      CashbackPages.clickOnCashbackPill();
      CashbackPages.verifyCashbackBalancePageShown();
      CashbackPages.doRedeemCashbackAndVerify();
    }
  );
});
