import React from "react";
import { render, fireEvent, screen } from "@testing-library/react";
import configureStore from "redux-mock-store";
import { Provider } from "react-redux";
import "@testing-library/jest-dom";
import Kue705FO from "../../../components/main/kabelueberwachung/kue705FO/Kue705FO";

// Mocks für externe Abhängigkeiten
jest.mock("chart.js/auto", () => ({
  default: {
    register: jest.fn(),
  },
  Chart: jest.fn().mockImplementation(() => ({
    destroy: jest.fn(),
    update: jest.fn(),
  })),
}));

jest.mock("chartjs-plugin-zoom", () => ({}));

// Initialzustand für Redux
const mockStore = configureStore([]);
const initialState = {
  variables: {
    kuePSTmMinus96V: [0],
    kueCableBreak: [0],
    kueGroundFault: [0],
    kueAlarm1: [0],
    kueAlarm2: [0],
    kueOverflow: [0],
    kueVersion: [419],
    tdrActive: [1],
  },
  auth: {
    isAdminLoggedIn: true, // Füge dies hinzu
  },
};

// Standard-Props
const defaultProps = {
  isolationswert: 200,
  schleifenwiderstand: 5.6,
  modulName: "TestModul",
  kueOnline: 1,
  slotIndex: 0,
  tdrLocation: [2.5],
};

describe("Kue705FO Integration Tests", () => {
  let store: ReturnType<typeof mockStore>;

  beforeEach(() => {
    store = mockStore(initialState);
  });

  it("should render correctly with default props", () => {
    render(
      <Provider store={store}>
        <Kue705FO {...defaultProps} />
      </Provider>
    );

    // Überprüfen, ob die Basis-Darstellung korrekt ist
    expect(screen.getByText("KÜ705-FO")).toBeInTheDocument();
    expect(screen.getByText("TestModul")).toBeInTheDocument();
  });

  it("should toggle between TDR and Schleife modes", () => {
    render(
      <Provider store={store}>
        <Kue705FO {...defaultProps} />
      </Provider>
    );

    // Überprüfen, ob Schleife aktiv ist
    expect(screen.getByText("Schleifenwiderstand [kOhm]")).toBeInTheDocument();
    expect(screen.getByText("5.6 KOhm")).toBeInTheDocument();

    // TDR-Button klicken
    fireEvent.click(screen.getByText("TDR"));

    // Überprüfen, ob TDR aktiv ist
    expect(screen.getByText("Entfernung [Km]")).toBeInTheDocument();
    expect(screen.getByText("2.5 Km")).toBeInTheDocument();

    // Zurück zu Schleife wechseln
    fireEvent.click(screen.getByText("Schleife"));
    expect(screen.getByText("Schleifenwiderstand [kOhm]")).toBeInTheDocument();
  });

  it("should open and close the settings modal", () => {
    render(
      <Provider store={store}>
        <Kue705FO {...defaultProps} />
      </Provider>
    );

    // Modal öffnen
    fireEvent.click(screen.getByText("⚙"));
    expect(screen.getByText("KUE Einstellung - Slot 1")).toBeInTheDocument();

    // Modal schließen
    fireEvent.click(screen.getByRole("button", { name: /x/i }));
    expect(
      screen.queryByText("KUE Einstellung - Slot 1")
    ).not.toBeInTheDocument();
  });

  it("should disable TDR button when tdrActive is 0", () => {
    // Zustand aktualisieren
    store = mockStore({
      ...initialState,
      variables: {
        ...initialState.variables,
        tdrActive: [0],
      },
    });

    render(
      <Provider store={store}>
        <Kue705FO {...defaultProps} />
      </Provider>
    );

    // TDR-Button sollte deaktiviert sein
    const tdrButton = screen.getByText("TDR");
    expect(tdrButton).toBeDisabled();
  });
});
