import React from "react";
import { render, screen } from "@testing-library/react";
import { EditLockIndicator } from "@/components/dashboard/EditLockIndicator";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { useEditLockStore } from "@/hooks/useEditLockStore";
import { useAuthStore } from "@/hooks/useAuthStore";

// Mock stores
vi.mock("@/hooks/useEditLockStore", () => ({
  useEditLockStore: vi.fn(),
}));

vi.mock("@/hooks/useAuthStore", () => ({
  useAuthStore: vi.fn(),
}));

describe("EditLockIndicator", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(useAuthStore).mockReturnValue({
      user: { id: 'user-1', name: 'Current User' }
    } as any);
  });

  it("renders nothing if not locked", () => {
    vi.mocked(useEditLockStore).mockReturnValue({
      lockInfo: null,
      isLoading: false,
      checkLock: vi.fn(),
      acquireLock: vi.fn(),
      releaseLock: vi.fn(),
    } as any);

    const { container } = render(<EditLockIndicator entityType="product" entityId="1" />);
    expect(container.firstChild).toBeNull();
  });

  it("renders warning if locked by another user", () => {
    vi.mocked(useEditLockStore).mockReturnValue({
      lockInfo: { userId: 'user-2', userName: 'Other User' },
      isLoading: false,
      checkLock: vi.fn(),
      acquireLock: vi.fn(),
      releaseLock: vi.fn(),
    } as any);

    render(<EditLockIndicator entityType="product" entityId="1" />);
    const alert = screen.getByRole("alert");
    expect(alert).toHaveTextContent(/Currently being edited by Other User/i);
  });

  it("renders info if locked by current user", () => {
    vi.mocked(useEditLockStore).mockReturnValue({
      lockInfo: { userId: 'user-1', userName: 'Current User' },
      isLoading: false,
      checkLock: vi.fn(),
      acquireLock: vi.fn(),
      releaseLock: vi.fn(),
    } as any);

    render(<EditLockIndicator entityType="product" entityId="1" />);
    expect(screen.getByText(/You have an active edit lock/i)).toBeInTheDocument();
  });
});
