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

vi.mock('../../src/renderer/inbox/api.js', () => ({
  inboxApi: {
    listNotes: vi.fn(async () => []),
    listTrash: vi.fn(async () => []),
    getTrashCount: vi.fn(async () => 0),
    getContinuity: vi.fn(async () => ({ weekStart: '', weekCount: 0, weekTarget: 7, consecutiveCompleteWeeks: 0, showRecoveryToast: false, lastNoteAt: null })),
    getPendingCount: vi.fn(async () => 0),
    getOllamaStatus: vi.fn(async () => ({ ok: true })),
    getTodayCount: vi.fn(async () => 0),
    listExpired: vi.fn(async () => []),
    getFailedCount: vi.fn(async () => 0),
    listRecallCandidate: vi.fn(async () => null),
    countsByStatus: vi.fn(async () => ({ active: 0, completed: 0, archived: 0, trashed: 0 })),
    getSettings: vi.fn(async () => ({ ai_enabled: true })),
    listByStatus: vi.fn(async () => [])
  },
  notebookApi: {
    list: vi.fn(async () => [
      { id: 'nb-1', name: '기본', color: null, createdAt: 't', updatedAt: 't', noteCount: 3 },
      { id: 'nb-2', name: '회사', color: '#0a4b80', createdAt: 't', updatedAt: 't', noteCount: 1 }
    ]),
    create: vi.fn(async (i: { name: string; color?: string }) => ({ ok: true as const, notebook: { id: 'nb-new', name: i.name, color: i.color ?? null, createdAt: 't', updatedAt: 't', noteCount: 0 } })),
    delete: vi.fn(async () => ({ ok: true as const })),
    rename: vi.fn(async () => ({ ok: true as const })),
    setColor: vi.fn(async () => ({ ok: true as const })),
    moveNote: vi.fn(async () => ({ ok: true as const }))
  }
}));

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

describe('store notebooks', () => {
  beforeEach(() => {
    useInbox.setState({ notebooks: [], selectedNotebookId: null, sidebarVisible: false, sidebarWidth: 240 } as never);
  });

  it('loadNotebooks 가 notebookApi.list 결과 반영', async () => {
    await useInbox.getState().loadNotebooks();
    expect(useInbox.getState().notebooks).toHaveLength(2);
  });

  it('loadNotebooks: selectedNotebookId null 이면 첫 notebook 으로 자동 설정', async () => {
    await useInbox.getState().loadNotebooks();
    expect(useInbox.getState().selectedNotebookId).toBe('nb-1');
  });

  it('selectNotebook 가 selectedNotebookId 설정', () => {
    useInbox.getState().selectNotebook('nb-X');
    expect(useInbox.getState().selectedNotebookId).toBe('nb-X');
  });

  it('createNotebook 성공 시 notebooks 에 추가', async () => {
    await useInbox.getState().createNotebook('학습', '#ccc');
    expect(useInbox.getState().notebooks.some((n) => n.name === '학습')).toBe(true);
  });

  it('toggleSidebar 가 sidebarVisible 반전', () => {
    expect(useInbox.getState().sidebarVisible).toBe(false);
    useInbox.getState().toggleSidebar();
    expect(useInbox.getState().sidebarVisible).toBe(true);
  });

  it('deleteNotebook 성공 시 notebooks 에서 제거 + selected 였으면 첫 notebook 으로', async () => {
    useInbox.setState({
      notebooks: [
        { id: 'nb-1', name: '기본', color: null, createdAt: 't', updatedAt: 't', noteCount: 0 },
        { id: 'nb-2', name: '회사', color: null, createdAt: 't', updatedAt: 't', noteCount: 0 }
      ],
      selectedNotebookId: 'nb-2'
    } as never);
    await useInbox.getState().deleteNotebook('nb-2');
    expect(useInbox.getState().notebooks.map((n) => n.id)).toEqual(['nb-1']);
    expect(useInbox.getState().selectedNotebookId).toBe('nb-1');
  });
});
