import { NextResponse } from 'next/server'
import { ConfigManager } from '@/remote-control/config/manager'

export async function GET() {
  const mgr = ConfigManager.getInstance()
  await mgr.ensureLoaded()
  const config = mgr.get()
  const statuses: Record<string, unknown> = {}

  // Discord Bot 状态
  if (config.discord.enabled) {
    try {
      const discordBot = await import('@/remote-control/bots/discord')
      statuses.discord = discordBot.getStatus()
    } catch {
      statuses.discord = { platform: 'discord', status: 'disconnected', error: 'Bot 模块未加载' }
    }
  } else {
    statuses.discord = { platform: 'discord', status: 'disconnected' }
  }

  // 钉钉 Bot 状态
  if (config.dingtalk.enabled) {
    try {
      const dingtalkBot = await import('@/remote-control/bots/dingtalk')
      statuses.dingtalk = dingtalkBot.getStatus()
    } catch {
      statuses.dingtalk = { platform: 'dingtalk', status: 'disconnected', error: 'Bot 模块未加载' }
    }
  } else {
    statuses.dingtalk = { platform: 'dingtalk', status: 'disconnected' }
  }

  // 飞书 Bot 状态
  if (config.lark.enabled) {
    try {
      const larkBot = await import('@/remote-control/bots/lark')
      statuses.lark = larkBot.getStatus()
    } catch {
      statuses.lark = { platform: 'lark', status: 'disconnected', error: 'Bot 模块未加载' }
    }
  } else {
    statuses.lark = { platform: 'lark', status: 'disconnected' }
  }

  // Telegram Bot 状态
  if (config.telegram.enabled) {
    try {
      const telegramBot = await import('@/remote-control/bots/telegram')
      statuses.telegram = telegramBot.getStatus()
    } catch {
      statuses.telegram = { platform: 'telegram', status: 'disconnected', error: 'Bot 模块未加载' }
    }
  } else {
    statuses.telegram = { platform: 'telegram', status: 'disconnected' }
  }

  // Slack Bot 状态
  if (config.slack.enabled) {
    try {
      const slackBot = await import('@/remote-control/bots/slack')
      statuses.slack = slackBot.getStatus()
    } catch {
      statuses.slack = { platform: 'slack', status: 'disconnected', error: 'Bot 模块未加载' }
    }
  } else {
    statuses.slack = { platform: 'slack', status: 'disconnected' }
  }

  return NextResponse.json(statuses)
}
