
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, closeNewsletterPopup, closeWheel, handlePopupDiscount } from "../../src/core/popup";
import { launchBrowserWithISPProxy } from "../../helper/lauch.proxy";

let checkoutPage: CheckoutPage;
let homePage: HomePage;
let productPage: ProductPage;
let cartPage: CartPage;

test.describe("@COF_FR PAYPAL PAYMENT", {
  tag: ["@COF_FR", "@COF_FR_PP"]
}, () => {
  test.beforeEach(async ({ }, testInfo) => {
    const { page } = await launchBrowserWithISPProxy('fr');
    checkoutPage = new CheckoutPage(page);
    homePage = new HomePage(page);
    productPage = new ProductPage(page);
    cartPage = new CartPage(page);
    await homePage.page.goto("https://fr.perifit.co", { waitUntil: 'domcontentloaded' });
    await expect(page).toHaveURL(/fr\.perifit\.co/, { timeout: 15_000 });

    await closeNewsletterPopup(page);
    await handlePopupDiscount(page);
    await allowAllCookies(page);
    await closeWheel(page);
  });

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

    await test.step("Checkout order with PayPal express", async () => {
      // Cart drawer checkout
      await expect(cartPage.cartLoc.xpahtBtnCheckoutCartDrawer).toBeVisible();
      await cartPage.cartLoc.xpahtBtnCheckoutCartDrawer.click();
      const logoDisplayed = await cartPage.buyWithPaypalInCheckout();
      expect(logoDisplayed).toBeTruthy();
    });
  });

  test('FR - PayPal Standard - Checkout with PayPal Standard in Checkout page', {
    annotation: {
      type: 'PP_COP',
      description: 'Checkout with PayPal'
    },
    tag: ["@COF_FR_PP_COP"]
  }, async () => {
    test.setTimeout(180_000);

    await test.step("Add product to cart", async () => {
      const firstProduct = await homePage.page.locator(productPage.xpathFirstProductInCollection).first();
      await firstProduct.click();
      await productPage.clickBtnAddToCart("Ajouter au panier");
    });

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

      // Cart drawer checkout
      await expect(cartPage.cartLoc.xpahtBtnCheckoutCartDrawer).toBeVisible();
      await cartPage.cartLoc.xpahtBtnCheckoutCartDrawer.click();
    });

    await test.step("Fill checkout info", async () => {
      const shippingAddress = {
        email: "playwrightvn@gmail.com",
        countryCode: "FR",
        firstName: "Auto",
        lastName: "Test",
        address1: "10 Rue de Rivoli",
        phone: "0612345678",
        zipcode: "75001"
      }
      // const shippingAddress = checkoutPage.shippingAddress;
      await checkoutPage.fillCheckoutInfo(shippingAddress);
      await expect(checkoutPage.page.locator("//p[contains(.,'Standard - 2 à 4 jours ouvrés')]")).toBeVisible({ timeout: 15_000 });
    });

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