/**
 * @vitest-environment jsdom
 */
// #region ValidationFindingsPanelUxTests:Module [TYPE Function]
// @SEMANTICS: dataset-review, findings, severity, jump, ux-tests
// @PURPOSE: Verify grouped findings visibility, empty state, and remediation jump behavior for US1.
// @LAYER UI
// @RELATION DEPENDS_ON -> [ValidationFindingsPanel]
// @UX_STATE: Blocking -> Blocking findings dominate the panel and expose remediation jumps.
// @UX_STATE: Warning -> Warning findings remain visible with explicit resolution state.
// @UX_STATE: Informational -> Informational notes stay readable without competing with blockers.
// @TEST_CONTRACT: FindingsPanelProps -> ObservableFindingsUX
// @TEST_SCENARIO: blocking_warning_info_groups_render -> Findings render in the proper severity groups with counts.
// @TEST_SCENARIO: jump_action_maps_area_to_workspace_target -> Jump action emits normalized remediation target.
// @TEST_SCENARIO: empty_findings_show_success_state -> Empty list shows ready feedback.
// @TEST_EDGE: missing_field -> Missing optional fields do not crash rendering.
// @TEST_EDGE: invalid_type -> Unknown severity falls back to informational grouping.
// @TEST_EDGE: external_fail -> Jump callback remains optional.
// @TEST_INVARIANT: findings_groups_remain_actionable -> VERIFIED_BY: [blocking_warning_info_groups_render, jump_action_maps_area_to_workspace_target, empty_findings_show_success_state]

import { describe, it, expect, vi } from "vitest";
import { fireEvent, render, screen } from "@testing-library/svelte";
import ValidationFindingsPanel from "../ValidationFindingsPanel.svelte";

vi.mock('$lib/i18n/index.svelte.js', () => ({
  t: {
    subscribe: (fn) => {
      fn({
        dataset_review: {
          findings: {
            eyebrow: "Validation findings",
            title: "Findings",
            description: "Review validation results",
            next_action_label: "Next action",
            empty: "No findings",
            blocking_title: "Blocking",
            blocking_empty: "No blocking findings",
            warning_title: "Warnings",
            warning_empty: "No warning findings",
            informational_title: "Info",
            informational_empty: "No informational findings",
            resolution_label: "Resolution",
            reference_label: "Reference",
            jump_action: "Jump to area",
            resolution: {
              open: "Open",
              approved: "Approved",
            },
            areas: {
              source_intake: "Source intake",
              dataset_profile: "Dataset summary",
              semantic_enrichment: "Semantics",
            },
          },
          workspace: {
            actions: {
              review_documentation: "Review documentation",
              import_from_superset: "Import from Superset",
            },
          },
        },
      });
      return () => {};
    },
  },
}));

describe("ValidationFindingsPanel UX Contract", () => {
  const findings = [
    {
      finding_id: "f-1",
      severity: "blocking",
      code: "REQ_ENV",
      area: "source_intake",
      title: "Environment required",
      message: "Select environment",
      resolution_state: "open",
    },
    {
      finding_id: "f-2",
      severity: "warning",
      code: "PARTIAL_RECOVERY",
      area: "dataset_profile",
      title: "Partial recovery",
      message: "Some metadata needs review",
      resolution_state: "approved",
      caused_by_ref: "dashboard:42",
    },
    {
      finding_id: "f-3",
      severity: "unexpected",
      code: "INFO_NOTE",
      area: "semantic_enrichment",
      title: "Dictionary note",
      message: "Trusted source used",
      resolution_state: "open",
    },
  ];

  it("blocking_warning_info_groups_render", () => {
    render(ValidationFindingsPanel, {
      findings,
      recommendedAction: "review_documentation",
    });

    expect(screen.getByText("Blocking")).toBeDefined();
    expect(screen.getByText("Warnings")).toBeDefined();
    expect(screen.getByText("Info")).toBeDefined();
    expect(screen.getByText("Environment required")).toBeDefined();
    expect(screen.getByText("Partial recovery")).toBeDefined();
    expect(screen.getByText("Dictionary note")).toBeDefined();
    expect(screen.getByText("Review documentation")).toBeDefined();
  });

  it("jump_action_maps_area_to_workspace_target", async () => {
    const onjump = vi.fn();
    render(ValidationFindingsPanel, {
      findings: [findings[0]],
      recommendedAction: "import_from_superset",
      onjump,
    });

    await fireEvent.click(screen.getByRole("button", { name: "Jump to area" }));

    expect(onjump).toHaveBeenCalledWith({
      target: "intake",
      finding: findings[0],
    });
  });

  it("empty_findings_show_success_state", () => {
    render(ValidationFindingsPanel, {
      findings: [],
      recommendedAction: "review_documentation",
    });

    expect(screen.getByText("No findings")).toBeDefined();
  });
});
// #endregion ValidationFindingsPanelUxTests:Module
