import mongoose from 'mongoose'
import Member from '../../models/member.js'
import Connection from '../../models/connection.js'
import { requireAuth } from '../../utils/auth.js'

export default defineEventHandler(async (event) => {
  const member = await requireAuth(event)
  const memberId = member._id

  const body = await readBody(event)
  const { recipientId } = body || {}

  if (!recipientId) {
    throw createError({
      statusCode: 400,
      statusMessage: 'recipientId is required'
    })
  }

  if (!mongoose.Types.ObjectId.isValid(recipientId)) {
    throw createError({
      statusCode: 400,
      statusMessage: 'Invalid recipientId'
    })
  }

  if (recipientId === memberId.toString()) {
    throw createError({
      statusCode: 400,
      statusMessage: 'Cannot connect with yourself'
    })
  }

  // Verify recipient exists and is active
  const recipient = await Member.findById(recipientId).lean()
  if (!recipient || recipient.status !== 'active') {
    throw createError({
      statusCode: 404,
      statusMessage: 'Recipient not found or not active'
    })
  }

  // Check for existing connection in either direction
  const existing = await Connection.findOne({
    $or: [
      { initiator: memberId, recipient: recipientId },
      { initiator: recipientId, recipient: memberId }
    ]
  })

  if (existing) {
    // If reverse pending connection exists, auto-confirm
    if (
      existing.status === 'pending' &&
      existing.initiator.toString() === recipientId &&
      existing.recipient.toString() === memberId.toString()
    ) {
      existing.status = 'confirmed'
      existing.confirmedAt = new Date()
      await existing.save()

      logActivity(memberId, 'connection_confirmed', { memberName: recipient.name })
      logActivity(recipientId, 'connection_confirmed', { memberName: member.name })

      return {
        connection: existing,
        autoConfirmed: true
      }
    }

    throw createError({
      statusCode: 409,
      statusMessage: 'Connection already exists'
    })
  }

  // Snapshot matching tags between the two members
  const myTopics = member.communityConnections?.topics || []
  const theirTopics = recipient.communityConnections?.topics || []
  const myTopicMap = {}
  for (const t of myTopics) {
    myTopicMap[t.tagSlug] = t.state
  }

  const matchingTags = []
  for (const t of theirTopics) {
    const myState = myTopicMap[t.tagSlug]
    if (myState) {
      matchingTags.push({
        tagSlug: t.tagSlug,
        initiatorState: myState,
        recipientState: t.state
      })
    }
  }

  const connection = await Connection.create({
    initiator: memberId,
    recipient: recipientId,
    status: 'pending',
    matchingTags
  })

  logActivity(memberId, 'connection_requested', { memberName: recipient.name })
  logActivity(recipientId, 'connection_requested', { memberName: member.name })

  return { connection }
})
