const fs = require('fs')
const path = require('path')

const servicesDir = path.join(__dirname, 'src', 'services')

const apis = {
  'user-simulator': \`import { apiClient } from '../apiClient'
export default {
  getArchetypes: () => apiClient('/api/user-simulator/archetypes'),
  generatePersonas: (data) => apiClient('/api/user-simulator/generate-personas', { method: 'POST', data }),
  generateMessage: (data) => apiClient('/api/user-simulator/generate-message', { method: 'POST', data }),
  chatWithBot: (data) => apiClient('/api/user-simulator/chat-with-bot', { method: 'POST', data }),
  evaluate: (data) => apiClient('/api/user-simulator/evaluate', { method: 'POST', data }),
  synthesize: (data) => apiClient('/api/user-simulator/synthesize', { method: 'POST', data }),
  runSimulation: (data) => apiClient('/api/user-simulator/run-simulation', { method: 'POST', data })
}
\`,
  'ai-sql': \`import { apiClient } from '../apiClient'
export default {
  chat: (data) => apiClient('/api/sql-chat', { method: 'POST', data }),
  getStatus: () => apiClient('/api/sql-chat/status'),
  dashboard: (data) => apiClient('/api/sql-dashboard', { method: 'POST', data }),
  v2Chat: (data) => apiClient('/api/text-to-sql/v2/chat', { method: 'POST', data })
}
\`,
  'ai-report': \`import { apiClient } from '../apiClient'
export default {
  generate: (data) => apiClient('/api/report-html', { method: 'POST', data }),
  stream: (taskId) => apiClient(\`/api/report-html/stream/\${taskId}\`),
  getReports: () => apiClient('/api/reports'),
  getConversations: () => apiClient('/api/reports/conversations'),
  getConversationDetail: (convId) => apiClient(\`/api/reports/conversations/\${convId}\`),
  getReportDetail: (reportId) => apiClient(\`/api/reports/\${reportId}\`),
  deleteReport: (reportId) => apiClient(\`/api/reports/\${reportId}\`, { method: 'DELETE' }),
  inlineEdit: (data) => apiClient('/api/report-inline-edit', { method: 'POST', data }),
  saveEdit: (data) => apiClient('/api/report-save-edit', { method: 'POST', data }),
  followup: (data) => apiClient('/api/report-followup', { method: 'POST', data })
}
\`,
  'stress-test': \`import { apiClient } from '../apiClient'
export default {
  run: (data) => apiClient('/api/stress-test/run', { method: 'POST', data })
}
\`,
  'regression-test': \`import { apiClient } from '../apiClient'
export default {
  run: (data) => apiClient('/api/regression-test/run', { method: 'POST', data })
}
\`,
  'user-insight': \`import { apiClient } from '../apiClient'
export default {
  getAll: () => apiClient('/api/user-insight/all'),
  getOne: (identityKey) => apiClient(\`/api/user-insight/\${identityKey}\`),
  deleteOne: (identityKey) => apiClient(\`/api/user-insight/\${identityKey}\`, { method: 'DELETE' })
}
\`,
  'roadmap': \`import { apiClient } from '../apiClient'
export default {
  getAll: () => apiClient('/api/roadmap'),
  create: (data) => apiClient('/api/roadmap', { method: 'POST', data }),
  update: (itemId, data) => apiClient(\`/api/roadmap/\${itemId}\`, { method: 'PUT', data }),
  remove: (itemId) => apiClient(\`/api/roadmap/\${itemId}\`, { method: 'DELETE' }),
  getFlow: () => apiClient('/api/roadmap/flow'),
  createFlow: (data) => apiClient('/api/roadmap/flow', { method: 'POST', data }),
  updateFlow: (itemId, data) => apiClient(\`/api/roadmap/flow/\${itemId}\`, { method: 'PUT', data }),
  removeFlow: (itemId) => apiClient(\`/api/roadmap/flow/\${itemId}\`, { method: 'DELETE' })
}
\`,
  'experiment-log': \`import { apiClient } from '../apiClient'
export default {
  getAll: () => apiClient('/api/experiment-log/experiments'),
  create: (data) => apiClient('/api/experiment-log/experiments', { method: 'POST', data }),
  update: (expId, data) => apiClient(\`/api/experiment-log/experiments/\${expId}\`, { method: 'PUT', data }),
  remove: (expId) => apiClient(\`/api/experiment-log/experiments/\${expId}\`, { method: 'DELETE' }),
  compare: () => apiClient('/api/experiment-log/experiments/compare')
}
\`,
   'product-desc': \`import { apiClient } from '../apiClient'
export default {
  generate: (data) => apiClient('/api/product-desc/generate', { method: 'POST', data })
}
\`,
   'fashion-matches': \`import { apiClient } from '../apiClient'
export default {
  match: (data) => apiClient('/api/fashion-matches/match', { method: 'POST', data })
}
\`
}

if (!fs.existsSync(servicesDir)) fs.mkdirSync(servicesDir, { recursive: true })

Object.keys(apis).forEach(page => {
  const pageDir = path.join(servicesDir, page)
  if (!fs.existsSync(pageDir)) fs.mkdirSync(pageDir, { recursive: true })
  fs.writeFileSync(path.join(pageDir, 'index.js'), apis[page])
})

// remove old files
const oldFiles = ['product.service.js', 'history.service.js', 'ai.service.js', 'system.service.js']
oldFiles.forEach(f => {
  const p = path.join(servicesDir, f)
  if(fs.existsSync(p)) fs.unlinkSync(p)
})

console.log('Build services mapping complete.')
