/* eslint-disable */

import { expect, test } from "@playwright/test";
import { acceptEulaIfPresent, login } from "./utils/login";

async function openCommissionCutHarness(page) {
  await page.goto("/manage/_test?fixture=commission-cut");
  await acceptEulaIfPresent(page);
  await expect(page.getByRole("heading", { name: "Commission Cut Test Harness" })).toBeVisible();
}

test.describe("Commission-based cut", () => {
  test.skip(!process.env.TEST_USERNAME || !process.env.TEST_PASSWORD, "Requires TEST_USERNAME and TEST_PASSWORD.");

  test("renders payout previews and completes Pay All from the commission-cut harness", async ({ page }) => {
    let calculateLaborCalls = 0;
    let payAllCalls = 0;

    await login(page, {
      email: process.env.TEST_USERNAME,
      password: process.env.TEST_PASSWORD
    });

    await page.route("**/payroll/calculatelabor", async (route) => {
      calculateLaborCalls += 1;

      await route.fulfill({
        status: 200,
        contentType: "application/json",
        body: JSON.stringify([
          {
            employeeid: "emp-1",
            mod_lbr_ty: "LAA",
            expectedHours: 4,
            claimedHours: 1
          },
          {
            employeeid: "emp-2",
            mod_lbr_ty: "LAB",
            expectedHours: 2,
            claimedHours: 1
          }
        ])
      });
    });

    await page.route("**/payroll/payall", async (route) => {
      payAllCalls += 1;

      await route.fulfill({
        status: 200,
        contentType: "application/json",
        body: JSON.stringify([{ id: "tt-1" }])
      });
    });

    await openCommissionCutHarness(page);
    await expect(page.getByText("Claim Task Preview")).toBeVisible();
    await expect(page.getByRole("cell", { name: "Commission" })).toBeVisible();
    await expect(page.getByRole("cell", { name: "Hourly" })).toBeVisible();
    await expect(
      page.getByText(
        "There are currently 1.25 hours of repair lines that are unassigned. These hours are not including in the above calculations and must be paid manually."
      )
    ).toBeVisible();

    await expect(page.getByRole("button", { name: "Pay All" })).toBeVisible();
    await page.getByRole("button", { name: "Pay All" }).click();

    await expect.poll(() => calculateLaborCalls).toBeGreaterThan(0);
    await expect.poll(() => payAllCalls).toBe(1);
    await expect(page.getByText("All hours paid out successfully.")).toBeVisible();
  });

  test("shows the backend error when Pay All is rejected", async ({ page }) => {
    await login(page, {
      email: process.env.TEST_USERNAME,
      password: process.env.TEST_PASSWORD
    });

    await page.route("**/payroll/calculatelabor", async (route) => {
      await route.fulfill({
        status: 200,
        contentType: "application/json",
        body: JSON.stringify([
          {
            employeeid: "emp-1",
            mod_lbr_ty: "LAA",
            expectedHours: 4,
            claimedHours: 1
          }
        ])
      });
    });

    await page.route("**/payroll/payall", async (route) => {
      await route.fulfill({
        status: 200,
        contentType: "application/json",
        body: JSON.stringify({
          success: false,
          error: "Not all hours have been assigned."
        })
      });
    });

    await openCommissionCutHarness(page);
    await page.getByRole("button", { name: "Pay All" }).click();

    await expect(page.getByText("Error flagging hours. Not all hours have been assigned.")).toBeVisible();
  });

  test("shows a negative labor difference when previously claimed hours exceed the current expected hours", async ({
    page
  }) => {
    await login(page, {
      email: process.env.TEST_USERNAME,
      password: process.env.TEST_PASSWORD
    });

    await page.route("**/payroll/calculatelabor", async (route) => {
      await route.fulfill({
        status: 200,
        contentType: "application/json",
        body: JSON.stringify([
          {
            employeeid: "emp-1",
            mod_lbr_ty: "LAA",
            expectedHours: 2,
            claimedHours: 5
          }
        ])
      });
    });

    await openCommissionCutHarness(page);

    await expect(page.locator("strong").filter({ hasText: "-3" }).first()).toBeVisible();
  });
});
