// M33: Admin Malware tab E2E.
//
// Read-only happy path — verifies the new Malware tab renders the 7
// sub-tab cards and the Status / Settings APIs respond. Quarantine
// restore + manual scan are NOT exercised here because they would
// touch real maldet on the test VM (irreversible side effects).
//
// Required env:
//   E2E_BASE_URL  https://jabali-panel.local
//   E2E_USERNAME  admin@example.com
//   E2E_PASSWORD  ...
//
// Skipped automatically when env not provided.

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

const SKIP_REASON =
  !process.env.E2E_USERNAME || !process.env.E2E_PASSWORD
    ? "E2E_USERNAME / E2E_PASSWORD not set"
    : "";

if (SKIP_REASON) {
  test.skip(() => true, `M33 E2E skipped: ${SKIP_REASON}`);
} else {
  test.describe("M33: Admin Malware tab", () => {
    test.setTimeout(60_000);

    test.use({
      ignoreHTTPSErrors: true,
      baseURL: process.env.E2E_BASE_URL || "https://jabali-panel.local",
    });

    const username = process.env.E2E_USERNAME || "";
    const password = process.env.E2E_PASSWORD || "";

    async function login(page: Page) {
      await page.goto("/login");
      await page.getByLabel(/email/i).fill(username);
      await page.getByLabel(/password/i).fill(password);
      await page.getByRole("button", { name: /sign in/i }).click();
      await page.waitForURL(/\/jabali-(admin|panel)/, { timeout: 30_000 });
    }

    test("Malware tab renders 7 sub-tabs + status loads", async ({ page }) => {
      await login(page);
      await page.goto("/jabali-admin/security?tab=malware");

      // Top-level tab present
      await expect(page.getByText(/Malware/i).first()).toBeVisible();

      // 7 sub-tabs visible
      for (const label of ["Overview", "Quarantine", "Events", "Manual scan", "YARA rules", "Exec audit", "Settings"]) {
        await expect(page.getByRole("tab", { name: label })).toBeVisible();
      }
    });

    test("Overview shows malware scan status", async ({ page }) => {
      await login(page);
      await page.goto("/jabali-admin/security?tab=malware");
      // Overview is the default sub-tab
      await expect(page.getByText("Realtime monitor")).toBeVisible({ timeout: 15_000 });
    });

    test("Settings tab loads + lists retention / threshold / switches", async ({ page }) => {
      await login(page);
      await page.goto("/jabali-admin/security?tab=malware");
      await page.getByRole("tab", { name: "Settings" }).click();
      await expect(page.getByText("Quarantine retention (days)")).toBeVisible({ timeout: 15_000 });
      await expect(page.getByText("Notification severity threshold")).toBeVisible();
    });

    test("Exec audit tab renders status card", async ({ page }) => {
      await login(page);
      await page.goto("/jabali-admin/security?tab=malware");
      await page.waitForLoadState("networkidle");
      const execAuditTab = page.getByRole("tab", { name: "Exec audit" });
      await expect(execAuditTab).toBeVisible({ timeout: 15_000 });
      await execAuditTab.click();
      await page.waitForTimeout(2_000);
      // The exec-audit tab should show the real-time scanning status card
      await expect(page.locator(".ant-alert").filter({ hasText: /Real-time scanning/i }).first()).toBeVisible({ timeout: 10_000 });
    });
  });
}
