import { computed, reactive } from 'vue'

const localeKey = 'supertodo_locale'
export type Locale = 'zh' | 'en'

const messages = {
  en: {
    brand_name: 'SuperTodo',
    brand_caption: 'Simple team execution board',
    nav_todo: 'Todo',
    nav_team: 'Team',
    nav_user_settings: 'User Settings',
    nav_team_settings: 'Team Settings',
    nav_login: 'Login',
    logout: 'Logout',
    guest_mode: 'Guest mode',
    language: 'Language',
    lang_switch: '中文',
    login_title: 'Account Access',
    login_subtitle: 'Use your account to enter the workspace.',
    login_hint: 'Minimal Todo workspace for personal and team execution.',
    login_tab: 'Login',
    register_tab: 'Register',
    email: 'Email',
    password: 'Password',
    auth_failed: 'Authentication failed, please check credentials.',
    loading_wait: 'Please wait...',
    register_and_login: 'Register and Login',
    todo_title: 'Todo Board',
    todo_subtitle: 'Simple execution list with team-ready structure.',
    refresh: 'Refresh',
    new_todo: 'New Todo',
    no_description: 'No description',
    priority_due: 'Priority P{priority} · Due {due}',
    due_empty: '-',
    edit: 'Edit',
    delete: 'Delete',
    loading_tasks: 'Loading tasks...',
    load_tasks_failed: 'Failed to load tasks. Please login again.',
    save_task_failed: 'Failed to save task.',
    update_task_failed: 'Failed to update task.',
    delete_task_failed: 'Failed to delete task.',
    page: 'Page',
    previous: 'Previous',
    next: 'Next',
    edit_todo: 'Edit Todo',
    create_todo: 'New Todo',
    title: 'Title',
    due_at: 'Due At',
    priority: 'Priority',
    high: 'High',
    medium: 'Medium',
    low: 'Low',
    tags: 'Tags',
    tags_placeholder: 'backend, planning',
    description: 'Description',
    cancel: 'Cancel',
    save: 'Save',
    team_entry: 'Team Entry',
    team_subtitle: 'Switch between teams and check active workload.',
    team_members: '{count} members',
    team_open_todos: '{count} open todos',
    user_settings_title: 'User Settings',
    user_settings_subtitle: 'Personal profile and preference controls.',
    display_name: 'Display Name',
    timezone: 'Timezone',
    reminders: 'Receive task reminders',
    save_settings: 'Save Settings',
    team_settings_title: 'Team Settings',
    team_settings_subtitle: 'Global conventions for team collaboration.',
    team_name: 'Team Name',
    workspace_slug: 'Workspace Slug',
    default_assignee: 'Default Assignee',
    completion_rule: 'Completion Rule',
    save_team_settings: 'Save Team Settings',
    task_updated_success: 'Task updated successfully',
    task_created_success: 'Task created successfully',
    task_deleted_success: 'Task deleted successfully',
    confirm_delete: 'Are you sure you want to delete this task?',
  },
  zh: {
    brand_name: 'SuperTodo',
    brand_caption: '简约的团队任务执行面板',
    nav_todo: '待办',
    nav_team: '团队入口',
    nav_user_settings: '用户设置',
    nav_team_settings: '团队设置',
    nav_login: '登录',
    logout: '退出登录',
    guest_mode: '访客模式',
    language: '语言',
    lang_switch: 'EN',
    login_title: '账号入口',
    login_subtitle: '使用账号登录后进入工作区。',
    login_hint: '一个简洁的个人与团队任务协作空间。',
    login_tab: '登录',
    register_tab: '注册',
    email: '邮箱',
    password: '密码',
    auth_failed: '认证失败，请检查邮箱和密码。',
    loading_wait: '请稍候...',
    register_and_login: '注册并登录',
    todo_title: '待办看板',
    todo_subtitle: '轻量执行清单，支持团队协作结构。',
    refresh: '刷新',
    new_todo: '新建待办',
    no_description: '暂无描述',
    priority_due: '优先级 P{priority} · 截止 {due}',
    due_empty: '-',
    edit: '编辑',
    delete: '删除',
    loading_tasks: '任务加载中...',
    load_tasks_failed: '加载任务失败，请重新登录。',
    save_task_failed: '保存任务失败。',
    update_task_failed: '更新任务失败。',
    delete_task_failed: '删除任务失败。',
    page: '第',
    previous: '上一页',
    next: '下一页',
    edit_todo: '编辑待办',
    create_todo: '新建待办',
    title: '标题',
    due_at: '截止时间',
    priority: '优先级',
    high: '高',
    medium: '中',
    low: '低',
    tags: '标签',
    tags_placeholder: '后端, 规划',
    description: '描述',
    cancel: '取消',
    save: '保存',
    team_entry: '团队入口',
    team_subtitle: '在团队之间切换并查看当前工作量。',
    team_members: '{count} 位成员',
    team_open_todos: '{count} 个未完成任务',
    user_settings_title: '用户设置',
    user_settings_subtitle: '管理个人资料和偏好设置。',
    display_name: '显示名称',
    timezone: '时区',
    reminders: '接收任务提醒',
    save_settings: '保存设置',
    team_settings_title: '团队设置',
    team_settings_subtitle: '管理团队协作的默认规则。',
    team_name: '团队名称',
    workspace_slug: '工作区标识',
    default_assignee: '默认负责人',
    completion_rule: '完成规则',
    save_team_settings: '保存团队设置',
    task_updated_success: '任务更新成功',
    task_created_success: '任务创建成功',
    task_deleted_success: '任务删除成功',
    confirm_delete: '确定要删除这个任务吗？',
  },
} as const

type MessageKey = keyof typeof messages.en

const initialLocale = (localStorage.getItem(localeKey) as Locale | null) ?? 'zh'

export const i18n = reactive({
  locale: initialLocale === 'en' ? 'en' : 'zh',
})

export const currentLocale = computed(() => i18n.locale)

export function toggleLocale() {
  i18n.locale = i18n.locale === 'zh' ? 'en' : 'zh'
  localStorage.setItem(localeKey, i18n.locale)
}

export function t(key: MessageKey, params?: Record<string, string | number>) {
  const locale = i18n.locale as Locale
  let text: string = messages[locale][key] ?? messages.en[key]
  if (params) {
    Object.entries(params).forEach(([k, v]) => {
      text = text.replace(new RegExp(`{${k}}`, 'g'), String(v))
    })
  }
  return text
}
