// @vitest-environment jsdom
import { describe, it, expect, vi, beforeEach } from 'vitest';
import '@testing-library/jest-dom/vitest';
import { render, screen, fireEvent, cleanup, waitFor } from '@testing-library/react';
import React from 'react';

const { mockListConflicts, mockResolveConflict } = vi.hoisted(() => ({
  mockListConflicts: vi.fn(),
  mockResolveConflict: vi.fn()
}));

vi.mock('../../src/renderer/inbox/api.js', () => ({
  inboxApi: { listConflicts: mockListConflicts, resolveConflict: mockResolveConflict }
}));

import { ConflictModal } from '../../src/renderer/inbox/components/ConflictModal';

describe('ConflictModal', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    cleanup();
    mockListConflicts.mockResolvedValue([
      { path: 'notes/n1.md', localText: 'local A', remoteText: 'remote A' },
      { path: 'notes/n2.md', localText: 'local B', remoteText: 'remote B' }
    ]);
    mockResolveConflict.mockResolvedValue({ ok: true });
  });

  it('open 시 listConflicts 호출 + 양 conflict preview 표시', async () => {
    render(<ConflictModal onClose={() => {}} onResolved={() => {}} />);
    await waitFor(() => screen.getByText(/local A/));
    expect(screen.getByText(/local A/)).toBeInTheDocument();
    expect(screen.getByText(/remote A/)).toBeInTheDocument();
    expect(screen.getByText(/local B/)).toBeInTheDocument();
    // path 가 표시됨 (Cut E final review fix — noteId → path)
    expect(screen.getByText('notes/n1.md')).toBeInTheDocument();
  });

  it('내 것 사용 클릭 → resolveConflict(path, "local") 호출', async () => {
    render(<ConflictModal onClose={() => {}} onResolved={() => {}} />);
    await waitFor(() => screen.getByText(/local A/));
    const buttons = screen.getAllByRole('button', { name: /내 것 사용/ });
    fireEvent.click(buttons[0]!);
    await waitFor(() => {
      expect(mockResolveConflict).toHaveBeenCalledWith('notes/n1.md', 'local');
    });
  });

  it('마지막 conflict 해결 → onResolved + onClose 호출', async () => {
    mockListConflicts.mockResolvedValueOnce([{ path: 'notes/n1.md', localText: 'a', remoteText: 'b' }]);
    const onResolved = vi.fn();
    const onClose = vi.fn();
    render(<ConflictModal onClose={onClose} onResolved={onResolved} />);
    await waitFor(() => screen.getByRole('button', { name: /원격 사용/ }));
    fireEvent.click(screen.getByRole('button', { name: /원격 사용/ }));
    await waitFor(() => {
      expect(onResolved).toHaveBeenCalled();
      expect(onClose).toHaveBeenCalled();
    });
  });
});
