/*
 *                                      Copyright 2025 WebPros International, LLC
 *                                                           All rights reserved.
 * copyright@cpanel.net                                         http://cpanel.net
 * This code is subject to the cPanel license. Unauthorized copying is prohibited.
 */

import { type CpanelServiceAccount } from "@page/cpanel/cpanel";
import { test, expect } from "@fixtures/plugins/sitejet/sitejet_banner";
import { SitejetBannerClass } from "@classes/plugins/sitejet/banner";
import { getCompanyId } from "@fixtures/plugins/common/company_id";
import logger from "@lib/winston";

let originalCID: number;

// Helper banner object
let sitejetBannerClass: SitejetBannerClass;

/**
 * We must use the SitejetBannerClass here because 'page' and  'context'
 * fixtures are not allowed in beforeAll() blocks.
 */
test.beforeAll(async ({ rootSSH, workerCpanelAccount }) => {
    originalCID = await getCompanyId(rootSSH);

    sitejetBannerClass = new SitejetBannerClass(workerCpanelAccount);
    await sitejetBannerClass.resetBannerSettings();
});

test.beforeEach(async ({ sitejetBanner }) => {
    await sitejetBanner.account.toggleWelcomePanel();
    await (
        sitejetBanner.account as CpanelServiceAccount
    ).toggleAnalyticsPersonhood();
});

test.afterEach(async () => {
    await sitejetBannerClass.resetBannerSettings();
});

test.afterAll(
    "Set server companyId back to how it was found",
    async ({ server }) => {
        logger.debug(`Setting server company ID back to: ${originalCID}`);
        await server.changeCompanyIDset(originalCID);
    }
);

const verifyBannerNotVisible = async (sitejetBanner): Promise<void> => {
    await expect(
        sitejetBanner.sitejetBannerContainer,
        "Sitejet banner is not present"
    ).not.toBeAttached();
};

test.describe("Sitejet Banner Tests", () => {
    test.beforeAll(async ({ server }) => {
        await server.changeCompanyIDset(999);
    });
    test.describe("Display Tests", () => {
        // Test to verify the Sitejet banner is displayed on the Tools page on first login
        test("Displays Sitejet banner on Tools page on first login", async ({
            sitejetBanner,
        }) => {
            await expect(
                sitejetBanner.sitejetBannerPhoto,
                "Sitejet banner photo is shown"
            ).toBeVisible();
            await expect(
                sitejetBanner.sitejetBannerTitle,
                "Sitejet title is shown"
            ).toBeVisible();
            await expect(
                sitejetBanner.sitejetBannerSVG,
                "Sitejet SVG is shown"
            ).toBeVisible();

            for (const bulletPoint of sitejetBanner.sitejetBannerBulletPoints) {
                await expect(
                    bulletPoint,
                    "bullet point is shown with the expected text"
                ).toBeVisible();
            }

            await expect(
                sitejetBanner.sitejetGetStartedButton,
                "Get Started button is shown"
            ).toBeVisible();
        });

        test.describe("Logging in with French locale", () => {
            test.beforeAll(async ({ workerCpanelAccount }) => {
                await workerCpanelAccount.setLocale("fr");
            });
            test.afterAll(async ({ workerCpanelAccount }) => {
                await workerCpanelAccount.setLocale("en");
            });
            test("Displays Sitejet banner on Tools page on first login", async ({
                sitejetBanner,
            }) => {
                await expect(
                    sitejetBanner.sitejetBannerTitleFrench,
                    "Sitejet title is shown in French"
                ).toBeVisible();
            });
        });

        // Test to verify the Sitejet banner is not displayed on other feature pages
        test("Does not display Sitejet banner on other feature pages", async ({
            sitejetBanner,
        }) => {
            await sitejetBanner.gotoFeature({ name: "Domains" });
            await verifyBannerNotVisible(sitejetBanner);

            await sitejetBanner.page.goto(sitejetBanner.baseURL);
            await expect(
                sitejetBanner.sitejetBannerSVG,
                "Sitejet banner is shown"
            ).toBeVisible();
        });

        // Test to verify the Sitejet banner is not displayed after being closed
        test("Does not display Sitejet banner after being closed", async ({
            sitejetBanner,
        }) => {
            await sitejetBanner.sitejetBannerCloseButton.click();
            await verifyBannerNotVisible(sitejetBanner);

            await sitejetBanner.page.reload();
            await verifyBannerNotVisible(sitejetBanner);

            await sitejetBanner.resetPageSettings();
            await verifyBannerNotVisible(sitejetBanner);
        });

        // Test to verify the Sitejet banner is not present when disabling promotions in Tweak Settings
        test("Does not display Sitejet banner when promotions are disabled in Tweak Settings", async ({
            sitejetBanner,
            cpanelconfig,
        }) => {
            await expect(
                sitejetBanner.sitejetBannerContainer,
                "Sitejet banner is present at first login"
            ).toBeAttached();

            await sitejetBanner.logout();
            await cpanelconfig.set({ display_cpanel_promotions: "0" });
            await sitejetBanner.login();
            await verifyBannerNotVisible(sitejetBanner);

            await cpanelconfig.set({ display_cpanel_promotions: "1" });
            await sitejetBanner.page.reload();
            await expect(
                sitejetBanner.sitejetBannerContainer,
                "Sitejet banner is present when re-enabled by Tweak Setting"
            ).toBeAttached();
            await expect(
                sitejetBanner.sitejetBannerTitle,
                "Sitejet title is shown"
            ).toBeVisible();
        });
    });
});
