import { test, expect } from "../../../fixtures/index";
import { LoginPage } from "../../../pom/login.page";
import { ContactPage } from "../../../pom/contact/contact.page";

test.describe("UI tests for contact", async () => {
    let loginPage: LoginPage;
    let contactPage: ContactPage;

    test.beforeEach(async ({ page, conf }) => {
        loginPage = new LoginPage(page);
        contactPage = new ContactPage(page);
        contactPage.loadBusinessNames(
            conf.data.business_singular,
            conf.data.business_plural
        );

        await test.step("Navigate to login page", async () => {
            await loginPage.open();
            await page.waitForLoadState('networkidle');
        });

        await test.step("Perform login authentication", async () => {
            await loginPage.login(conf.data.username, conf.data.password);
            await expect(loginPage.baseLoc.dashboardContainer).toBeVisible({ timeout: 15_000 });
        });

        // Conditional navigation to contact page
        if (conf.data.not_redirect) {
            return;
        }

        await test.step("Navigate to contact page", async () => {
            await contactPage.open();
        });
    });

    test("CONTACT_001 - Verify UI when empty state", {
        tag: ["@CONTACT_001", "@contact", "@ui"],
    }, async ({ conf }) => {
        await test.step("Verify heading and create button", async () => {
            await expect(contactPage.dashboardLoc.root).toMatchAriaSnapshot(contactPage.contactSnapshot.headingAndCreateButton);
        });

        await test.step("Verify empty state", async () => {
            await expect(contactPage.dashboardLoc.root).toMatchAriaSnapshot(contactPage.contactSnapshot.emptyState);
        });
    });

    test("CONTACT_002 - Verify UI when have contact", {
        tag: ["@CONTACT_002", "@contact", "@ui"],
    }, async () => {
        await test.step("Reset to default column", async () => {
            await contactPage.resetToDefaultColumn(contactPage.contactProps.customizedColumns);
        });

        await test.step("Verify table: headings, footers", async () => {
            await expect(contactPage.dashboardLoc.root).toMatchAriaSnapshot(contactPage.contactSnapshot.haveRecord);
        });

        await test.step("Verify have 10 records for the first page", async () => {
            expect(async () => {
                const rowCount = await contactPage.getTableRowCount();
                expect(rowCount).toBe(10);
            }).toPass();
        });
    });

    test("CONTACT_011 - Verify UI form Create new contact", {
        tag: ["@CONTACT_011", "@contact", "@ui", "@PRODUCTION_READ_ONLY"],
    }, async () => {
        await test.step("Verify heading modal create", async () => {
            await contactPage.clickCreatePage();
            await expect(contactPage.dashboardLoc.root).toMatchAriaSnapshot(contactPage.contactSnapshot.createModalHeader);
        });

        await test.step("Verify body modal create", async () => {
            await expect(contactPage.dashboardLoc.root).toMatchAriaSnapshot(contactPage.contactSnapshot.createModalBody);
        });

        await test.step("Verify footer modal create", async () => {
            await expect(contactPage.dashboardLoc.root).toMatchAriaSnapshot(contactPage.contactSnapshot.createModalFooter);
        });
    });

    test("CONTACT_017 - Verify UI form update Contact Detail", {
        tag: ["@CONTACT_017", "@contact", "@ui", "@PRODUCTION_READ_ONLY"],
    }, async ({ conf }) => {
        await test.step("Verify data already exist", async () => {
            await contactPage.contactLoc.searchInput.fill(conf.data.email_already_exist);
            await expect(contactPage.contactLoc.emailAlreadyExist(conf.data.email_already_exist)).toBeVisible();
        });

        await test.step("Verify UI Detail contact", async () => {
            await contactPage.waitForSecond(1);
            
            await contactPage.contactLoc.emailAlreadyExist(conf.data.email_already_exist).click();
            await expect(contactPage.contactLoc.root).toMatchAriaSnapshot(contactPage.contactSnapshot.contactDetailView);
        });

        await test.step("Verify UI update contact", async () => {
            await contactPage.dashboardLoc.buttonUpdate.click();

            await expect(contactPage.contactLoc.root).toMatchAriaSnapshot(contactPage.contactSnapshot.updateModalHeader);
            await expect(contactPage.contactLoc.root).toMatchAriaSnapshot(contactPage.contactSnapshot.updateModalBody);
            await expect(contactPage.contactLoc.root).toMatchAriaSnapshot(contactPage.contactSnapshot.updateModalFooter);
        });
    });
});
