import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { translate } from '@/i18n/useI18n'

const mockUpdateSetting = vi.fn()
let mockSettingsState: Record<string, unknown> = {}

vi.mock('@/stores/useSettingsStore', () => ({
  useSettingsStore: Object.assign(
    (selector?: (state: unknown) => unknown) => {
      return selector ? selector(mockSettingsState) : mockSettingsState
    },
    {
      getState: () => mockSettingsState
    }
  )
}))

vi.mock('@/stores/useThemeStore', () => ({
  useThemeStore: Object.assign(
    (selector?: (state: unknown) => unknown) => {
      const state = { setTheme: vi.fn(), themeId: 'default' }
      return selector ? selector(state) : state
    },
    {
      getState: () => ({ setTheme: vi.fn(), themeId: 'default' })
    }
  )
}))

vi.mock('@/stores/useShortcutStore', () => ({
  useShortcutStore: Object.assign(
    (selector?: (state: unknown) => unknown) => {
      const state = { resetToDefaults: vi.fn() }
      return selector ? selector(state) : state
    },
    {
      getState: () => ({ resetToDefaults: vi.fn() })
    }
  )
}))

vi.mock('@/hooks', () => ({
  useGhosttySuppression: vi.fn()
}))

vi.mock('@/lib/themes', () => ({
  DEFAULT_THEME_ID: 'default',
  THEME_PRESETS: [
    {
      id: 'default-dark',
      name: 'Default Dark',
      type: 'dark',
      colors: {
        background: '#000000',
        sidebar: '#111111',
        primary: '#222222',
        'muted-foreground': '#333333'
      }
    },
    {
      id: 'default-light',
      name: 'Default Light',
      type: 'light',
      colors: {
        background: '#ffffff',
        sidebar: '#eeeeee',
        primary: '#dddddd',
        'muted-foreground': '#cccccc'
      }
    }
  ]
}))

vi.mock('@/lib/toast', () => ({
  toast: {
    success: vi.fn(),
    error: vi.fn()
  }
}))

describe('Settings i18n', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    mockSettingsState = {
      locale: 'zh-CN',
      autoStartSession: true,
      breedType: 'dogs',
      vimModeEnabled: false,
      showModelIcons: false,
      showModelProvider: false,
      showUsageIndicator: true,
      keepAwakeEnabled: true,
      defaultAgentSdk: 'opencode',
      updateChannel: 'stable',
      stripAtMentions: true,
      updateSetting: mockUpdateSetting,
      resetToDefaults: vi.fn(),
      isOpen: true,
      activeSection: 'general',
      closeSettings: vi.fn(),
      openSettings: vi.fn(),
      setActiveSection: vi.fn()
    }

    Object.defineProperty(window, 'updaterOps', {
      configurable: true,
      writable: true,
      value: {
        getVersion: vi.fn().mockResolvedValue('1.0.71'),
        checkForUpdate: vi.fn().mockResolvedValue(undefined)
      }
    })
  })

  it('renders translated labels in SettingsGeneral when locale is zh-CN', async () => {
    const { SettingsGeneral } = await import('@/components/settings/SettingsGeneral')
    render(<SettingsGeneral />)

    expect(screen.getByText('通用')).toBeInTheDocument()
    expect(screen.getByText('语言')).toBeInTheDocument()
    expect(screen.getByText('AI 提供方')).toBeInTheDocument()
    expect(screen.getByText('会话期间保持唤醒')).toBeInTheDocument()
    expect(screen.getByText('分支命名')).toBeInTheDocument()
    expect(screen.getByTestId('reset-all-settings')).toHaveTextContent('重置全部设置')
  })

  it('updates locale when the language selector changes', async () => {
    const { SettingsGeneral } = await import('@/components/settings/SettingsGeneral')
    render(<SettingsGeneral />)

    await userEvent.selectOptions(screen.getByTestId('settings-language-select'), 'en')

    expect(mockUpdateSetting).toHaveBeenCalledWith('locale', 'en')
  })

  it('renders translated navigation labels in SettingsModal', async () => {
    const { SettingsModal } = await import('@/components/settings/SettingsModal')
    render(<SettingsModal />)

    expect(screen.getByText('设置')).toBeInTheDocument()
    expect(screen.getByTestId('settings-nav-general')).toHaveTextContent('通用')
    expect(screen.getByTestId('settings-nav-appearance')).toHaveTextContent('外观')
    expect(screen.getByTestId('settings-nav-updates')).toHaveTextContent('更新')
  })

  it('renders translated labels in SettingsAppearance', async () => {
    const { SettingsAppearance } = await import('@/components/settings/SettingsAppearance')
    render(<SettingsAppearance />)

    expect(screen.getByText('外观')).toBeInTheDocument()
    expect(screen.getByTestId('dark-themes-header')).toHaveTextContent('深色主题')
    expect(screen.getByTestId('light-themes-header')).toHaveTextContent('浅色主题')
  })

  it('renders translated labels in SettingsUpdates', async () => {
    const { SettingsUpdates } = await import('@/components/settings/SettingsUpdates')
    render(<SettingsUpdates />)

    expect(screen.getByText('更新')).toBeInTheDocument()
    expect(screen.getByText('更新通道')).toBeInTheDocument()
    await waitFor(() => {
      expect(screen.getByText('当前版本：')).toBeInTheDocument()
    })
  })

  it('translates file search and command palette copy', () => {
    expect(translate('zh-CN', 'fileSearch.placeholder')).toBe('按文件名或路径搜索...')
    expect(translate('zh-CN', 'fileTree.gitStatus.conflicted')).toBe('有冲突')
    expect(translate('zh-CN', 'fileTree.gitStatus.stagedSuffix')).toBe('（已暂存）')
    expect(translate('zh-CN', 'fileTree.header.title')).toBe('文件')
    expect(translate('zh-CN', 'fileTree.branchDiff.selectBranch')).toBe('选择要比较的分支...')
    expect(translate('zh-CN', 'fileTree.changes.mergeConflicts')).toBe('合并冲突')
    expect(translate('zh-CN', 'terminalToolbar.status.exited', { code: 1 })).toBe('已退出（1）')
    expect(translate('zh-CN', 'terminalToolbar.actions.clear')).toBe('清空终端（Cmd+K）')
    expect(translate('zh-CN', 'runOutputSearch.placeholder')).toBe('在输出中查找...')
    expect(translate('zh-CN', 'runOutputSearch.count', { current: 2, total: 5 })).toBe('2 / 5')
    expect(translate('zh-CN', 'setupTab.empty.configure')).toBe('配置 setup 脚本')
    expect(translate('zh-CN', 'setupTab.actions.rerun')).toBe('重新运行 Setup')
    expect(translate('zh-CN', 'copyMessageButton.ariaLabel')).toBe('复制消息')
    expect(translate('zh-CN', 'editMessageButton.ariaLabel')).toBe('编辑消息')
    expect(translate('zh-CN', 'dialogs.forkFromMessage.dismiss')).toBe('不再显示')
    expect(translate('zh-CN', 'codeBlock.toasts.copied')).toBe('代码已复制到剪贴板')
    expect(translate('zh-CN', 'errorFallback.title')).toBe('出了点问题')
    expect(translate('zh-CN', 'errorFallback.retry')).toBe('重试')
    expect(translate('zh-CN', 'updateToast.available.skip')).toBe('跳过此版本')
    expect(translate('zh-CN', 'ghosttyPromoToast.activate')).toBe('启用')
    expect(translate('zh-CN', 'compactionPill.auto')).toBe('已自动压缩上下文')
    expect(translate('zh-CN', 'indeterminateProgressBar.working')).toBe('Agent 正在执行')
    expect(translate('zh-CN', 'gitCommitForm.summaryPlaceholder')).toBe('提交摘要')
    expect(translate('zh-CN', 'gitCommitForm.stagedCount', { count: 3, label: '文件' })).toBe(
      '3 个文件'
    )
    expect(translate('zh-CN', 'gitPushPull.merge.filterPlaceholder')).toBe('筛选分支...')
    expect(translate('zh-CN', 'gitPushPull.toasts.mergeSuccess', { branch: 'main' })).toBe(
      '已成功合并 main'
    )
    expect(translate('zh-CN', 'gitStatusPanel.sections.staged')).toBe('已暂存变更')
    expect(translate('zh-CN', 'gitStatusPanel.fileItem.viewChangesTitle', { path: 'a.ts' })).toBe(
      '查看变更：a.ts'
    )
    expect(translate('zh-CN', 'projectList.empty.noProjects')).toBe('还没有添加任何项目。')
    expect(translate('zh-CN', 'fileViewer.externalChanges.keepMine')).toBe('保留我的更改')
    expect(translate('zh-CN', 'spaces.dialogs.createTitle')).toBe('创建空间')
    expect(translate('zh-CN', 'mainPane.welcomeTitle')).toBe('欢迎使用玄圃')
    expect(translate('zh-CN', 'agentPicker.title')).toBe('选择默认 AI Agent')
    expect(translate('zh-CN', 'fileMentionPopover.noFiles')).toBe('没有找到文件')
    expect(translate('zh-CN', 'sessionView.empty.title')).toBe('开始一段对话')
    expect(translate('zh-CN', 'sessionTerminalView.loading')).toBe('正在加载终端...')
    expect(translate('zh-CN', 'permissionPrompt.actions.allowAlways')).toBe('始终允许')
    expect(translate('zh-CN', 'commandApprovalPrompt.header.required')).toBe('需要命令审批')
    expect(translate('zh-CN', 'terminalManager.empty.selectWorktree')).toBe(
      '选择一个 worktree 以打开终端'
    )
    expect(translate('zh-CN', 'connectionList.title')).toBe('连接')
    expect(translate('zh-CN', 'dialogs.manageConnectionWorktrees.save')).toBe('保存')
    expect(translate('zh-CN', 'prReview.viewer.selectedCount', { count: 2 })).toBe('已选择 2 条')
    expect(translate('zh-CN', 'prReview.commentCard.copyRawHtml')).toBe('复制原始 HTML')
    expect(translate('zh-CN', 'appLayout.drop.maxFiles', { count: 10 })).toBe(
      '单次最多拖入 10 个文件'
    )
    expect(translate('zh-CN', 'dialogs.projectSettings.runScript.placeholder')).toBe('pnpm run dev')
    expect(translate('zh-CN', 'loading.default')).toBe('加载中...')
    expect(translate('zh-CN', 'toolViews.grep.showAllResults', { count: 12 })).toBe(
      '显示全部 12 条结果'
    )
    expect(translate('zh-CN', 'toolViews.read.linesRange', { start: 10, end: 20 })).toBe(
      '第 10-20 行'
    )
    expect(translate('zh-CN', 'toolViews.task.defaultTitle')).toBe('子代理')
    expect(translate('zh-CN', 'toolViews.edit.moreAdded', { count: 4 })).toBe('... 还有 4 行被新增')
    expect(translate('zh-CN', 'toolViews.fallback.note')).toBe('暂无自定义渲染器，当前显示原始数据')
    expect(translate('zh-CN', 'toolViews.skill.loading')).toBe('正在加载技能内容...')
    expect(translate('zh-CN', 'toolViews.todo.empty')).toBe('没有任务')
    expect(translate('zh-CN', 'toolViews.webFetch.bytesPlural', { count: 512 })).toBe('512 字节')
    expect(translate('zh-CN', 'toolViews.lsp.operations.workspaceSymbols')).toBe('工作区符号')
    expect(translate('zh-CN', 'toolViews.lsp.showAll', { count: 3, label: '位置' })).toBe(
      '显示全部 3 条位置'
    )
    expect(translate('zh-CN', 'contextIndicator.title')).toBe('上下文窗口')
    expect(translate('zh-CN', 'contextIndicator.cost.session', { cost: '$1.2345' })).toBe(
      '会话成本：$1.2345'
    )
    expect(translate('zh-CN', 'sessionHq.composer.successCriteriaPlaceholder')).toBe('成功标准...')
    expect(translate('zh-CN', 'sessionHq.composer.queuePlaceholder')).toBe(
      '输入要排队到本轮之后的后续消息...'
    )
    expect(translate('zh-CN', 'sessionHq.timeline.emptyTitle')).toBe('暂无消息')
    expect(translate('zh-CN', 'sessionHq.header.providerUpdateError')).toBe('更新提供方失败')
    expect(translate('zh-CN', 'sessionHq.agentRail.interrupts.commandApproval')).toBe(
      '需要命令审批'
    )
    expect(translate('zh-CN', 'sessionHq.missionControl.allTasksCompleted')).toBe('全部任务已完成')
    expect(translate('zh-CN', 'sessionHq.cards.plan.requiresApproval')).toBe('需要批准')
    expect(translate('zh-CN', 'sessionHq.cards.askUser.waitingForReply')).toBe('等待回复')
    expect(translate('zh-CN', 'sessionHq.cards.bash.truncated')).toBe('...（已截断）')
    expect(translate('zh-CN', 'sessionHq.cards.search.query')).toBe('查询：')
    expect(translate('zh-CN', 'sessionHq.cards.goal.dismissCompleted')).toBe('关闭已完成目标')
    expect(translate('zh-CN', 'helpOverlay.title')).toBe('键盘快捷键')
    expect(translate('zh-CN', 'helpOverlay.panels.setup')).toBe('启动脚本')
    expect(translate('zh-CN', 'helpOverlay.dynamic.pinnedPrefix', { name: 'foo' })).toBe(
      '[固定] foo'
    )
    expect(translate('zh-CN', 'keyboardShortcuts.items.sessionNew')).toBe('新建会话')
    expect(translate('zh-CN', 'connectionStore.toasts.createSuccess', { name: 'demo' })).toBe(
      '连接“demo”已创建'
    )
    expect(translate('zh-CN', 'pinnedStore.toasts.pinConnectionError')).toBe('固定连接失败')
    expect(translate('zh-CN', 'sessionStore.errors.createConnectionSession')).toBe(
      '创建连接会话失败'
    )
    expect(translate('zh-CN', 'prReview.store.fetchError')).toBe('获取评论失败')
    expect(translate('zh-CN', 'prReview.store.unknownReviewer')).toBe('未知评论者')
    expect(translate('zh-CN', 'commandPalette.hints.goBack')).toBe('返回')
    expect(translate('zh-CN', 'recent.status.planReady')).toBe('计划已就绪')
    expect(translate('zh-CN', 'recent.status.commandApproval')).toBe('审批命令')
    expect(translate('zh-CN', 'dialogs.gitInit.title')).toBe('不是 Git 仓库')
    expect(translate('zh-CN', 'dialogs.connect.connect')).toBe('连接')
    expect(translate('zh-CN', 'dialogs.addAttachment.placeholder')).toBe('粘贴 Jira 或 Figma 链接')
    expect(translate('zh-CN', 'header.controls.mergePR')).toBe('合并 PR')
    expect(translate('zh-CN', 'pinned.title')).toBe('已固定')
    expect(translate('zh-CN', 'pinned.menu.renameBranch')).toBe('重命名分支')
    expect(translate('zh-CN', 'pinned.menu.openInFileManager', { manager: 'Finder' })).toBe(
      '在 Finder 中打开'
    )
    expect(translate('zh-CN', 'pinned.menu.pin')).toBe('固定')
    expect(translate('zh-CN', 'pinned.status.archiving')).toBe('归档中')
    expect(translate('zh-CN', 'pinned.status.commandApproval')).toBe('审批命令')
    expect(translate('zh-CN', 'pinned.toasts.pathCopied')).toBe('路径已复制到剪贴板')
    expect(translate('zh-CN', 'projectItem.menu.openInFileManager', { manager: 'Finder' })).toBe(
      '在 Finder 中打开'
    )
    expect(translate('zh-CN', 'projectItem.toasts.createWorktreeError')).toBe('创建 worktree 失败')
    expect(translate('zh-CN', 'sidebar.filterPopover.noMatchingCommands')).toBe('没有匹配的命令')
  })
})
