import { test } from "../../../../setup";
import { ProductCreation } from "../../../../pages/product";
import { CreateRules } from "../../../../pages/rules";

test.beforeEach("should create simple product", async ({ adminPage }) => {
    const productCreation = new ProductCreation(adminPage);

    await productCreation.createProduct({
        type: "simple",
        sku: `SKU-${Date.now()}`,
        name: `Simple-${Date.now()}`,
        shortDescription: "Short desc",
        description: "Full desc",
        price: 199,
        weight: 1,
        inventory: 100,
    });
});

test.afterEach(
    "should delete the created product and rule",
    async ({ adminPage }) => {
        const createRules = new CreateRules(adminPage);
        await createRules.deleteCatalogRuleAndProduct();
    },
);

test.describe("catalog rules", () => {
    test.describe("product attribute conditions", () => {
        test("should apply coupon when price condition is -> is equal to", async ({
            page,
        }) => {
            const createRules = new CreateRules(page);
            await createRules.adminlogin();
            await createRules.catalogRuleCreationFlow();
            await createRules.addCondition({
                attribute: "product|price",
                operator: "==",
                value: "199",
            });
            await createRules.saveCatalogRule();
            await createRules.verifyCatalogRule();
        });

        test("should apply coupon when price condition is -> is not equal to", async ({
            page,
        }) => {
            const createRules = new CreateRules(page);
            await createRules.adminlogin();
            await createRules.catalogRuleCreationFlow();
            await createRules.addCondition({
                attribute: "product|price",
                operator: "!=",
                value: "100",
            });
            await createRules.saveCatalogRule();
            await createRules.verifyCatalogRule();
        });

        test("should apply coupon when price condition is -> equals or greater then", async ({
            page,
        }) => {
            const createRules = new CreateRules(page);
            await createRules.adminlogin();
            await createRules.catalogRuleCreationFlow();
            await createRules.addCondition({
                attribute: "product|price",
                operator: ">=",
                value: "199",
            });
            await createRules.saveCatalogRule();
            await createRules.verifyCatalogRule();
        });

        test("should apply coupon when price condition is -> equals or less than", async ({
            page,
        }) => {
            const createRules = new CreateRules(page);
            await createRules.adminlogin();
            await createRules.catalogRuleCreationFlow();
            await createRules.addCondition({
                attribute: "product|price",
                operator: "<=",
                value: "200",
            });
            await createRules.saveCatalogRule();
            await createRules.verifyCatalogRule();
        });

        test("should apply coupon when price condition is -> greater than", async ({
            page,
        }) => {
            const createRules = new CreateRules(page);
            await createRules.adminlogin();
            await createRules.catalogRuleCreationFlow();
            await createRules.addCondition({
                attribute: "product|price",
                operator: ">",
                value: "198",
            });
            await createRules.saveCatalogRule();
            await createRules.verifyCatalogRule();
        });

        test("should apply coupon when price condition is -> less than", async ({
            page,
        }) => {
            const createRules = new CreateRules(page);
            await createRules.adminlogin();
            await createRules.catalogRuleCreationFlow();
            await createRules.addCondition({
                attribute: "product|price",
                operator: "<",
                value: "200",
            });
            await createRules.saveCatalogRule();
            await createRules.verifyCatalogRule();
        });
    });
});
