import { expect, test } from "@playwright/test";

test.describe("Estimates", () => {
  test.beforeEach(async ({ page }) => {
    await page.goto("/auth/signin");
    await page.fill('input[type="email"]', "admin@capakraken.dev");
    await page.fill('input[type="password"]', "admin123");
    await page.click('button[type="submit"]');
    await expect(page).toHaveURL(/\/(dashboard|resources)/);
    await page.goto("/estimates");
  });

  test("estimate list loads", async ({ page }) => {
    await page.waitForLoadState("networkidle");
    await expect(
      page.locator("h1").filter({ hasText: /Estimates/i }),
    ).toBeVisible({ timeout: 10000 });
    // Should show either a table/list or an empty state
    await expect(
      page.locator("table").or(page.locator("text=No estimates")).or(page.locator("[data-estimate-id]")),
    ).toBeVisible({ timeout: 10000 });
  });

  test("new estimate wizard opens with setup step", async ({ page }) => {
    await page.waitForLoadState("networkidle");
    const newBtn = page.locator("button", { hasText: /New Estimate/i });
    await expect(newBtn).toBeVisible({ timeout: 10000 });
    await newBtn.click();
    // Wizard step 1 should appear: "Setup"
    await expect(
      page.locator("text=Setup").or(page.locator("text=Estimate Name")),
    ).toBeVisible({ timeout: 5000 });
  });

  test("estimate wizard navigates through steps", async ({ page }) => {
    await page.waitForLoadState("networkidle");
    await page.locator("button", { hasText: /New Estimate/i }).click();

    // Step 1: Setup — fill a name
    await expect(page.locator("text=Setup")).toBeVisible({ timeout: 5000 });
    const nameInput = page.locator('input[placeholder*="name"]').or(page.locator('input[name="name"]')).first();
    if ((await nameInput.count()) > 0) {
      await nameInput.fill(`E2E Estimate ${Date.now()}`);
    }

    // Click Next to go to step 2
    const nextBtn = page.locator("button", { hasText: "Next" });
    if ((await nextBtn.count()) > 0) {
      await nextBtn.click();
      await page.waitForTimeout(500);
      // Step 2: Assumptions
      await expect(
        page.locator("text=Assumptions").or(page.locator("text=Scope")),
      ).toBeVisible({ timeout: 5000 });
    }

    // Close the wizard without completing
    const cancelBtn = page.locator("button", { hasText: /Cancel|Close/i }).first();
    if ((await cancelBtn.count()) > 0) {
      await cancelBtn.click();
    } else {
      await page.keyboard.press("Escape");
    }
  });

  test("estimate wizard shows all step labels", async ({ page }) => {
    await page.waitForLoadState("networkidle");
    await page.locator("button", { hasText: /New Estimate/i }).click();

    // The wizard header should show step labels
    const steps = ["Setup", "Assumptions", "Scope", "Staffing", "Review"];
    for (const step of steps) {
      await expect(page.locator(`text=${step}`).first()).toBeVisible({ timeout: 5000 });
    }

    await page.keyboard.press("Escape");
  });
});
