/**
 * QuickCaptureWidget Component Tests
 * Following TDD principles
 */

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { QuickCaptureWidget } from "../QuickCaptureWidget";

global.fetch = vi.fn() as typeof global.fetch;

describe("QuickCaptureWidget", (): void => {
  beforeEach((): void => {
    vi.clearAllMocks();
  });

  it("should render input field", (): void => {
    render(<QuickCaptureWidget id="quick-capture-1" />);

    expect(screen.getByRole("textbox")).toBeInTheDocument();
  });

  it("should render submit button", (): void => {
    render(<QuickCaptureWidget id="quick-capture-1" />);

    expect(screen.getByRole("button", { name: /add|capture|submit/i })).toBeInTheDocument();
  });

  it("should allow text input", async (): Promise<void> => {
    const user = userEvent.setup();
    render(<QuickCaptureWidget id="quick-capture-1" />);

    const input = screen.getByRole("textbox");
    await user.type(input, "Quick note for later");

    expect(input).toHaveValue("Quick note for later");
  });

  // TODO: Enable when API is implemented
  it.skip("should submit note when button clicked", async (): Promise<void> => {
    const user = userEvent.setup();
    vi.mocked(global.fetch).mockResolvedValueOnce({
      ok: true,
      json: () => Promise.resolve({ success: true }),
    } as unknown as Response);

    render(<QuickCaptureWidget id="quick-capture-1" />);

    const input = screen.getByRole("textbox");
    const button = screen.getByRole("button", { name: /add|capture|submit/i });

    await user.type(input, "New quick note");
    await user.click(button);

    await waitFor(() => {
      expect(global.fetch).toHaveBeenCalledWith(
        expect.stringContaining("/api"),
        expect.objectContaining({
          method: "POST",
        })
      );
    });
  });

  it("should clear input after successful submission", async (): Promise<void> => {
    const user = userEvent.setup();
    vi.mocked(global.fetch).mockResolvedValueOnce({
      ok: true,
      json: () => Promise.resolve({ success: true }),
    } as unknown as Response);

    render(<QuickCaptureWidget id="quick-capture-1" />);

    const input = screen.getByRole("textbox");
    const button = screen.getByRole("button", { name: /add|capture|submit/i });

    await user.type(input, "Test note");
    await user.click(button);

    await waitFor(() => {
      expect(input).toHaveValue("");
    });
  });

  // TODO: Enable when API is implemented
  it.skip("should handle submission errors", async (): Promise<void> => {
    const user = userEvent.setup();
    vi.mocked(global.fetch).mockRejectedValueOnce(new Error("API Error"));

    render(<QuickCaptureWidget id="quick-capture-1" />);

    const input = screen.getByRole("textbox");
    const button = screen.getByRole("button", { name: /add|capture|submit/i });

    await user.type(input, "Test note");
    await user.click(button);

    await waitFor(() => {
      expect(screen.getByText(/error|failed/i)).toBeInTheDocument();
    });
  });

  it("should not submit empty notes", async (): Promise<void> => {
    const user = userEvent.setup();
    render(<QuickCaptureWidget id="quick-capture-1" />);

    const button = screen.getByRole("button", { name: /add|capture|submit/i });
    await user.click(button);

    expect(global.fetch).not.toHaveBeenCalled();
  });

  // TODO: Enable when API is implemented
  it.skip("should support keyboard shortcut (Enter)", async (): Promise<void> => {
    const user = userEvent.setup();
    vi.mocked(global.fetch).mockResolvedValueOnce({
      ok: true,
      json: () => Promise.resolve({ success: true }),
    } as unknown as Response);

    render(<QuickCaptureWidget id="quick-capture-1" />);

    const input = screen.getByRole("textbox");
    await user.type(input, "Quick note{Enter}");

    await waitFor(() => {
      expect(global.fetch).toHaveBeenCalled();
    });
  });

  it("should show success feedback after submission", async (): Promise<void> => {
    const user = userEvent.setup();

    render(<QuickCaptureWidget id="quick-capture-1" />);

    const input = screen.getByRole("textbox");
    const button = screen.getByRole("button", { name: /submit/i });

    await user.type(input, "Test note");
    await user.click(button);

    await waitFor(() => {
      // Check for "Recently captured:" text which shows success
      expect(screen.getByText(/Recently captured/i)).toBeInTheDocument();
      // Check that the note appears in the list
      expect(screen.getByText("Test note")).toBeInTheDocument();
    });
  });
});
