// @vitest-environment jsdom
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'

// api 클라이언트를 목으로 — 실제 네트워크 없이 분석 데이터를 주입한다.
vi.mock('../api/client', () => ({ default: { get: vi.fn() } }))
import client from '../api/client'
import MentorAnalytics from './MentorAnalytics.jsx'

const SAMPLE = {
  cohort: '전체',
  studentCount: 2,
  pendingReviews: 3,
  cohorts: [{ cohort: '2026', count: 2 }],
  students: [
    {
      userId: 1, name: '가영', track: 'DEV', cohort: '2026',
      coursesDone: 10, quizAttempted: 2, quizAvgPct: 80,
      codingPassed: 5, codingTotal: 10, checklistDone: 3, checklistTotal: 10,
      lastActiveAt: '2026-09-08T00:00:00Z', daysSinceActive: 2,
    },
    {
      userId: 2, name: '나윤', track: 'DEV', cohort: '2026',
      coursesDone: 0, quizAttempted: 0, quizAvgPct: 0,
      codingPassed: 0, codingTotal: 10, checklistDone: 0, checklistTotal: 10,
      lastActiveAt: null, daysSinceActive: null,
    },
  ],
  hardestQuizzes: [{ courseSlug: 'lv1-loops', avgScorePct: 40, attempts: 2 }],
  hardestCoding: [{ problemId: 1, title: '별 찍기', passRatePct: 25, attempts: 4 }],
}

describe('MentorAnalytics', () => {
  beforeEach(() => {
    client.get.mockReset()
    client.get.mockResolvedValue({ data: SAMPLE })
  })

  it('KPI와 리뷰 대기 건수를 보여준다', async () => {
    render(<MentorAnalytics />)
    expect(await screen.findByText('2명')).toBeInTheDocument()   // 학생 수
    expect(screen.getByText('3건')).toBeInTheDocument()          // 리뷰 대기
  })

  it('활동 없는 학생을 "관심 필요"에 "활동 없음"으로 올린다', async () => {
    render(<MentorAnalytics />)
    await waitFor(() => expect(screen.getByText('나윤')).toBeInTheDocument())
    expect(screen.getByText('활동 없음')).toBeInTheDocument()
  })

  it('어려운 퀴즈는 slug를 강좌 제목으로 바꿔 보여준다', async () => {
    render(<MentorAnalytics />)
    // lv1-loops → 카탈로그 제목("반복문...")으로 해석돼야 한다
    expect(await screen.findByText(/반복문/)).toBeInTheDocument()
    expect(screen.getByText('별 찍기')).toBeInTheDocument()      // 어려운 코딩
  })
})
