
import { test, expect } from "@playwright/test";
import { CheckoutPage } from "../../page/checkout-page";
import { CartPage } from "../../page/cart-page";
import { ProductPage } from "../../page/product-page";
import { HomePage } from "../../page/home-page";
import { allowAllCookies, authorizedShoppayPopup, closeWheel } from "../../src/core/popup";
import { launchBrowserWithISPProxy } from "../../helper/lauch.proxy";
import { attachGeoLocation, captureAllRequests } from "../../helper/attach.geo";

let checkoutPage: CheckoutPage;
let homePage: HomePage;
let productPage: ProductPage;
let cartPage: CartPage;
let requestCapture: Awaited<ReturnType<typeof captureAllRequests>>;

test.describe("@COF_NZ PAYPAL PAYMENT", {
  tag: ["@COF_NZ", "@COF_NZ_PP"]
}, () => {
  test.beforeEach(async ({ }, testInfo) => {
    const { page } = await launchBrowserWithISPProxy('nz');
    checkoutPage = new CheckoutPage(page);
    homePage = new HomePage(page);
    productPage = new ProductPage(page);
    cartPage = new CartPage(page);

    await homePage.goToHomePage("https://nz.perifit.co");
    await expect(page).toHaveURL(/nz\.perifit\.co/);

    await allowAllCookies(page);
    await closeWheel(page);
    await authorizedShoppayPopup(page);
    await homePage.page.locator(homePage.xpathAnnouncementBar).click();

    //Attach geo location to the report
    requestCapture = await captureAllRequests(page, testInfo);
    // await attachGeoLocation(page, testInfo);
  });

  test.afterEach(async () => {
    await requestCapture.attach();
  });

  test('NZ - PayPal express - Checkout with PayPal express in Cart page', {
    annotation: {
      type: 'PPE_CRT',
      description: 'Checkout with PayPal express in Cart page'
    },
    tag: ["@COF_NZ_PPE_CRT"]
  }, async () => {
    test.setTimeout(150_000);
    await test.step("Add product to cart", async () => {
      const firstProduct = homePage.page.locator(productPage.xpathFirstProductInCollection).first();
      await firstProduct.click();
      await productPage.clickBtnAddToCart();
    });

    await test.step("Checkout order with PayPal express", async () => {
      const logoDisplayed = await cartPage.buyWithPaypalInCheckout();
      expect(logoDisplayed).toBeTruthy();
      await cartPage.page.close();
    });
  });

  test('NZ - PayPal express - Checkout with PayPal express in Checkout page', {
    annotation: {
      type: 'PPE_COP',
      description: 'Checkout with PayPal express in Checkout page'
    },
    tag: ["@COF_NZ_PPE_COP"]
  }, async () => {
    test.setTimeout(150_000);
    await test.step("Add product to cart", async () => {
      const firstProduct = homePage.page.locator(productPage.xpathFirstProductInCollection).first();
      await firstProduct.click();
      await productPage.clickBtnAddToCart();
    });

    await test.step("Click checkout", async () => {
      await cartPage.clickBtnCheckout();
    });

    await test.step("Checkout order with PayPal Express", async () => {
      const logoDisplayed = await checkoutPage.checkoutWithPayPalExpress();
      expect(logoDisplayed).toBeTruthy();
      await checkoutPage.page.close();
    });
  });

  test('NZ - PayPal Standard - Checkout with PayPal Standard in Checkout page', {
    annotation: {
      type: 'PP_COP',
      description: 'Checkout with PayPal'
    },
    tag: ["@COF_NZ_PP_COP"]
  }, async () => {
    test.setTimeout(150_000);
    await test.step("Add product to cart", async () => {
      const firstProduct = homePage.page.locator(productPage.xpathFirstProductInCollection).first();
      await firstProduct.click();
      await productPage.clickBtnAddToCart();
    });

    await test.step("Click checkout", async () => {
      await cartPage.clickBtnCheckout();
    });

    await test.step("Fill checkout info", async () => {
      const shippingAddress = checkoutPage.shippingAddress;
      await checkoutPage.fillCheckoutInfo(shippingAddress);
    });

    await test.step("Complete order with PayPal", async () => {
      await checkoutPage.page.waitForSelector(checkoutPage.xpathHeadingPayment);
      const logoDisplayed = await checkoutPage.checkoutWithPaypal();
      expect(logoDisplayed).toBeTruthy();
    });
  });
});