/*
 *                                      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 { 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);
    }
);

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

    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 banner headline is shown",
                element: sitejetBanner.sitejetBannerHeadline,
            },
            {
                title: "Sitejet SVG is shown",
                element: sitejetBanner.sitejetBannerSVG,
            },
            {
                title: "Get Started button is shown",
                element: sitejetBanner.sitejetGetStartedButton,
            },
        ];

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

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

    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.sitejetBannerHeadline,
                },
                {
                    title: "Get Started button is NOT shown",
                    element: sitejetBanner.sitejetGetStartedButton,
                },
            ];

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