/**
 * @fileoverview Tests for chatbot hooks
 */

import React from "react";
import { renderHook, act } from "@testing-library/react";
import { useErrorHandler, useTheme } from "../hooks";
import type { ChatbotError } from "../core/contexts/ChatbotContext";

// Mock chatbot context
jest.mock("../hooks/useChatbot", () => ({
  useChatbot: () => ({
    lastError: null,
    clearError: jest.fn(),
  }),
}));

describe("Chatbot Hooks", () => {
  describe("useErrorHandler", () => {
    it("should initialize with no error", () => {
      const { result } = renderHook(() => useErrorHandler());

      expect(result.current.error).toBeNull();
      expect(result.current.hasError).toBe(false);
    });

    it("should handle new errors", () => {
      const { result } = renderHook(() => useErrorHandler());

      act(() => {
        result.current.handleError("Test error");
      });

      expect(result.current.hasError).toBe(true);
      expect(result.current.getErrorMessage()).toBe("Test error");
    });

    it("should clear errors", () => {
      const { result } = renderHook(() => useErrorHandler());

      act(() => {
        result.current.handleError("Test error");
      });

      expect(result.current.hasError).toBe(true);

      act(() => {
        result.current.clearError();
      });

      expect(result.current.hasError).toBe(false);
    });

    it("should handle ChatbotError objects", () => {
      const { result } = renderHook(() => useErrorHandler());

      const chatbotError: ChatbotError = {
        code: "TEST_ERROR",
        message: "Test chatbot error",
        timestamp: new Date(),
      };

      act(() => {
        result.current.handleError(chatbotError);
      });

      expect(result.current.isErrorType("TEST_ERROR")).toBe(true);
      expect(result.current.getErrorMessage()).toBe("Test chatbot error");
    });
  });

  describe("useTheme", () => {
    it("should initialize with default theme", () => {
      const { result } = renderHook(() => useTheme());

      expect(result.current.theme.mode).toBe("light");
      expect(result.current.theme.primaryColor).toBe("#007bff");
    });

    it("should toggle theme mode", () => {
      const { result } = renderHook(() => useTheme());

      expect(result.current.theme.mode).toBe("light");

      act(() => {
        result.current.toggleMode();
      });

      expect(result.current.theme.mode).toBe("dark");
    });

    it("should update theme properties", () => {
      const { result } = renderHook(() => useTheme());

      act(() => {
        result.current.setTheme({ primaryColor: "#ff0000" });
      });

      expect(result.current.theme.primaryColor).toBe("#ff0000");
    });

    it("should reset theme to defaults", () => {
      const { result } = renderHook(() => useTheme());

      act(() => {
        result.current.setTheme({ primaryColor: "#ff0000" });
      });

      expect(result.current.theme.primaryColor).toBe("#ff0000");

      act(() => {
        result.current.resetTheme();
      });

      expect(result.current.theme.primaryColor).toBe("#007bff");
    });
  });
});
