/** @odoo-module **/

import { registry } from "@web/core/registry";
import * as tourUtils from "@website_sale/js/tours/tour_utils";

const PRODUCT_NAME = "Membership Test Product";

registry.category("web_tour.tours").add("website_membership_signup_required_tour", {
    url: "/shop",
    steps: () => [
        // -- Anonymous visitor, add a membership product to cart -----------
        {
            content: "Search the membership product",
            trigger: 'form input[name="search"]',
            run: `edit ${PRODUCT_NAME}`,
        },
        {
            content: "Run the search",
            trigger: 'form:has(input[name="search"]) .oe_search_button',
            run: "click",
            expectUnloadPage: true,
        },
        {
            content: "Open the membership product page",
            trigger: `.oe_product_cart:first a:text(${PRODUCT_NAME})`,
            run: "click",
            expectUnloadPage: true,
        },
        {
            content: "Click Add to Cart as anonymous user",
            trigger: "#add_to_cart",
            run: "click",
            expectUnloadPage: true,
        },
        // -- On /web/signup with firstname / lastname ----------------------
        {
            content: "Signup page shows firstname field (b2c re-enabled by marker)",
            trigger: ".oe_signup_form input[name='firstname']",
            run: "edit Tour",
        },
        {
            trigger: ".oe_signup_form input[name='lastname']",
            run: "edit Member",
        },
        {
            content: "Type a unique email login",
            trigger: ".oe_signup_form input[name='login']",
            run: () => {
                const email = `membership.tour.${Date.now()}@test.example.com`;
                document.querySelector(".oe_signup_form input[name='login']").value = email;
            },
        },
        {
            trigger: ".oe_signup_form input[name='password']",
            run: "edit TourMember1!",
        },
        {
            trigger: ".oe_signup_form input[name='confirm_password']",
            run: "edit TourMember1!",
        },
        {
            content: "Accept the terms & conditions checkbox",
            trigger: ".oe_signup_form input[name='accepted_terms']",
            run: "click",
        },
        {
            content: "Submit the signup form -> redirect straight to cart "
                     + "with the product already added (post-login auto-add)",
            trigger: ".oe_signup_form button[type='submit']",
            run: "click",
            expectUnloadPage: true,
        },
        // -- Lands directly on /shop/cart with the membership line ---------
        {
            content: "We land on /shop/cart with the membership product",
            trigger: `#cart_products td.t-w-employee a:text("${PRODUCT_NAME}")`,
        },
        tourUtils.assertCartContains({ productName: PRODUCT_NAME }),
    ],
});

registry.category("web_tour.tours").add("website_membership_signup_required_non_intercept_tour", {
    url: "/shop",
    steps: () => [
        {
            content: "Search the regular product",
            trigger: 'form input[name="search"]',
            run: "edit Regular Test Product",
        },
        {
            content: "Run the search",
            trigger: 'form:has(input[name="search"]) .oe_search_button',
            run: "click",
            expectUnloadPage: true,
        },
        {
            content: "Open the regular product page",
            trigger: `.oe_product_cart:first a:text("Regular Test Product")`,
            run: "click",
            expectUnloadPage: true,
        },
        {
            content: "Add to cart as anonymous (no signup interception)",
            trigger: "#add_to_cart",
            run: "click",
            expectUnloadPage: true,
        },
        tourUtils.goToCart(),
        tourUtils.assertCartContains({ productName: "Regular Test Product" }),
    ],
});