import Member from '../../../models/member.js'
import { connectDB } from '../../../utils/mongoose.js'

export default defineEventHandler(async (event) => {
  await connectDB()
  const member = await requireAuth(event)

  const body = await validateBody(event, communityConnectionsUpdateSchema)

  // Build update object for community connections settings
  const updateData = {
    'communityConnections.topics': body.topics || [],
    'communityConnections.offerPeerSupport': body.offerPeerSupport || false,
    'communityConnections.availability': body.availability || '',
    'communityConnections.slackHandle': body.slackHandle || '',
    'communityConnections.personalMessage': body.personalMessage || '',
    'communityConnections.details': body.details || '',
  }

  // If Slack handle provided and peer support offered, try to fetch Slack user ID and open DM
  if (body.offerPeerSupport && body.slackHandle) {
    try {
      console.log(
        `[Community Connections] Attempting to fetch Slack user ID for: ${body.slackHandle}`,
      )

      const { getSlackService } = await import('../../../utils/slack.ts')
      const slackService = getSlackService()

      if (slackService) {
        console.log('[Community Connections] Slack service initialized, looking up user...')
        const slackUserId = await slackService.findUserIdByUsername(body.slackHandle)

        if (slackUserId) {
          updateData['slackUserId'] = slackUserId
          console.log(
            `[Community Connections] ✓ Found Slack user ID for ${body.slackHandle}: ${slackUserId}`,
          )

          console.log('[Community Connections] Opening DM channel...')
          const dmChannelId = await slackService.openDMChannel(slackUserId)

          if (dmChannelId) {
            updateData['communityConnections.slackDMChannelId'] = dmChannelId
            console.log(`[Community Connections] ✓ Got DM channel ID: ${dmChannelId}`)
          } else {
            console.warn('[Community Connections] Could not get DM channel ID')
          }
        } else {
          console.warn(
            `[Community Connections] Could not find Slack user ID for handle: ${body.slackHandle}`,
          )
        }
      } else {
        console.log('[Community Connections] Slack service not configured, skipping user ID lookup')
      }
    } catch (error) {
      console.error('[Community Connections] Error fetching Slack user ID:', error.message)
      console.error('[Community Connections] Stack trace:', error.stack)
      // Continue anyway - we'll still save the handle
    }
  }

  try {
    const updated = await Member.findByIdAndUpdate(
      member._id,
      { $set: updateData },
      { new: true, runValidators: true },
    )

    if (!updated) {
      throw createError({
        statusCode: 404,
        statusMessage: 'Member not found',
      })
    }

    logActivity(member._id, 'community_connections_updated', {
      topicCount: (body.topics || []).length,
      offerPeerSupport: body.offerPeerSupport || false,
    })

    return {
      success: true,
      communityConnections: updated.communityConnections,
    }
  } catch (error) {
    if (error.statusCode) throw error
    console.error('Community connections update error:', error)
    throw createError({
      statusCode: 500,
      statusMessage: 'Failed to update community connections settings',
    })
  }
})
