/*
 *                                      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 "/usr/local/cpanel/t/qa/playwright/lib/winston";
import { test, expect } from "../../../fixtures/plugins/sitejet/sitejet_banner";
import { SitejetBannerClass } from "../../../classes/plugins/sitejet/banner";
import { type CpanelServiceAccount } from "@page/cpanel/cpanel";
import { CompanyId } from "@classes/plugins/companyid";

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 }) => {
    // Preserve the test server's initial company ID
    const companyId = new CompanyId(rootSSH);
    originalCID = await companyId.getCompanyID();

    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);
    }
);

// Testing
test.describe("Sitejet Banner is shown for non-beta company", () => {
    test.beforeAll(async ({ server }) => {
        await server.changeCompanyIDset(999);
    });

    test("Sitejet banner is shown (Tools page on first login) for allowed company ", async ({
        sitejetBanner,
    }) => {
        const elementsToCheck = [
            {
                title: "Sitejet banner photo is shown",
                element: sitejetBanner.sitejetBannerPhoto,
            },
            {
                title: "Sitejet title is shown",
                element: sitejetBanner.sitejetBannerTitle,
            },
            {
                title: "Sitejet SVG is shown",
                element: sitejetBanner.sitejetBannerSVG,
            },
            {
                title: "Get Started button is shown",
                element: sitejetBanner.sitejetGetStartedButton,
            },
            ...sitejetBanner.sitejetBannerBulletPoints.map(
                (bulletPoint, index) => ({
                    title: `Bullet point ${index + 1} is shown with the expected text`,
                    element: bulletPoint,
                })
            ),
        ];

        for (const { element, title } of elementsToCheck) {
            await expect(element, title).toBeVisible();
        }
    });
});

test.describe("Sitejet Banner is NOT shown for beta company", () => {
    test.beforeAll(async ({ server }) => {
        await server.changeCompanyIDset(7);
    });

    test.describe("Display Tests", () => {
        test("Sitejet banner is NOT shown (Tools page on first login) for restricted company ", async ({
            sitejetBanner,
        }) => {
            const elementsToCheck = [
                {
                    title: "Sitejet banner photo is NOT shown",
                    element: sitejetBanner.sitejetBannerPhoto,
                },
                {
                    title: "Sitejet title is NOT shown",
                    element: sitejetBanner.sitejetBannerTitle,
                },
                {
                    title: "Get Started button is NOT shown",
                    element: sitejetBanner.sitejetGetStartedButton,
                },
            ];

            for (const { element, title } of elementsToCheck) {
                await expect(element, title).toBeHidden();
            }
        });
    });
});
