/*
 *                                      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 logger from "@lib/winston";
import { test, expect } from "@fixtures/plugins/sitejet/sitejet_banner_ai";
import { SitejetBannerAIClass } from "@classes/plugins/sitejet/banner_ai";
import { type CpanelServiceAccount } from "@page/cpanel/cpanel";
import { type SitejetBannerAI } from "@page/plugins/sitejet/sitejet_banner_ai";
import { getCompanyId } from "@fixtures/plugins/common/company_id";

let originalCID: number;
// Helper banner object
let sitejetBannerClass: SitejetBannerAIClass;

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

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

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

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: SitejetBannerAI
): Promise<void> => {
    await expect(
        sitejetBanner.sitejetBannerElement,
        "Sitejet banner is not present"
    ).not.toBeAttached();
};

test.describe("Sitejet Banner Tests", () => {
    test.beforeAll(async ({ server }) => {
        await server.changeCompanyIDset(7);
    });
    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.sitejetBannerHeadline,
                "Sitejet AI Headline is shown"
            ).toBeVisible();
            await expect(
                sitejetBanner.featurelist,
                "Sitejet AI Feature list is shown"
            ).toBeVisible();

            await expect(
                sitejetBanner.sitejetBannerSVG,
                "Sitejet SVG is shown"
            ).toBeVisible();

            /**
            Since this is a test for the sitejet banner, we will not test
            the entire re-routing logic. This is a new domain and it does not
            have a published website, so the button will redirect to the CMS
      */
            await sitejetBanner.sitejetGetStartedButton.click();
            await expect(
                sitejetBanner.page,
                "Let's go button takes you to the Sitejet page"
            ).toHaveURL(/cms\.sitehub\.io/);
        });

        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 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.sitejetBannerElement,
                "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.sitejetBannerElement,
                "Sitejet banner is present when re-enabled by Tweak Setting"
            ).toBeAttached();
            await expect(
                sitejetBanner.sitejetBannerHeadline,
                "Sitejet title is shown"
            ).toBeVisible();
        });

        /**
         * Test to verify the Sitejet banner is not displayed after being closed.
         *
         * !!WARNING!!: This test will reset the page settings bringing back the welcome modal.
         * Keep it at the end of the test suite.
         */
        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);
        });
    });
});
