import { describe, it, expect, vi, beforeEach } from 'vitest';

vi.mock('../../src/renderer/inbox/api.js', () => ({
  inboxApi: {
    search: vi.fn(),
    reviewAggregate: vi.fn(),
    listNotes: vi.fn(() => []),
    getContinuity: vi.fn(() => ({ weekStart: '', weekCount: 0, weekTarget: 7, consecutiveCompleteWeeks: 0, showRecoveryToast: false, lastNoteAt: null })),
    getPendingCount: vi.fn(() => 0),
    getOllamaStatus: vi.fn(() => ({ ok: true })),
    getTodayCount: vi.fn(() => 0),
    getTrashCount: vi.fn(() => 0),
    listExpired: vi.fn(() => []),
    getFailedCount: vi.fn(() => 0),
    listRecallCandidate: vi.fn(() => null),
    countsByStatus: vi.fn(() => ({ active: 0, completed: 0, archived: 0, trashed: 0 })),
    getSettings: vi.fn(() => ({ ai_enabled: true })),
    listByStatus: vi.fn(() => [])
  }
}));

import { useInbox } from '../../src/renderer/inbox/store';
import { inboxApi } from '../../src/renderer/inbox/api.js';

describe('store — searchNotes', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    useInbox.setState({ searchQuery: '', searchResults: null, view: 'inbox' });
  });

  it('빈 query → searchResults null + IPC 미호출', async () => {
    await useInbox.getState().searchNotes('   ');
    expect(useInbox.getState().searchResults).toBeNull();
    expect(inboxApi.search).not.toHaveBeenCalled();
  });

  it('keyword query → IPC 호출 + searchResults set', async () => {
    (inboxApi.search as ReturnType<typeof vi.fn>).mockResolvedValue([{ id: 'a' }]);
    await useInbox.getState().searchNotes('회의');
    expect(inboxApi.search).toHaveBeenCalledWith('회의', { status: 'active' });
    expect(useInbox.getState().searchResults).toEqual([{ id: 'a' }]);
  });

  it('clearSearch — query + results 모두 초기화', () => {
    useInbox.setState({ searchQuery: '회의', searchResults: [{ id: 'a' } as never] });
    useInbox.getState().clearSearch();
    expect(useInbox.getState().searchQuery).toBe('');
    expect(useInbox.getState().searchResults).toBeNull();
  });
});
