// server/api/members/create.post.js
import Member from '../../models/member.js'
import { connectDB } from '../../utils/mongoose.js'
import { getSlackService } from '../../utils/slack.ts'
import { validateBody } from '../../utils/validateBody.js'
import { memberCreateSchema } from '../../utils/schemas.js'
import { sendWelcomeEmail } from '../../utils/resend.js'
import { assignMemberNumber } from '../../utils/memberNumber.js'

// Function to invite member to Slack
async function inviteToSlack(member) {
  try {
    const slackService = getSlackService()
    if (!slackService) {
      console.warn('Slack service not configured, skipping invitation')
      return
    }

    console.warn(`Processing Slack invitation for member`)
    
    const inviteResult = await slackService.inviteUserToSlack(
      member.email,
      member.name
    )

    if (inviteResult.success) {
      // Update member record based on the actual result
      if (inviteResult.status === 'existing_user_added_to_channel' || 
          inviteResult.status === 'user_already_in_channel' || 
          inviteResult.status === 'new_user_invited_to_workspace') {
        await Member.findByIdAndUpdate(
          member._id,
          { $set: { slackInviteStatus: 'sent', slackUserId: inviteResult.userId, slackInvited: true } },
          { runValidators: false }
        )
      } else {
        // Manual invitation required
        await Member.findByIdAndUpdate(
          member._id,
          { $set: { slackInviteStatus: 'pending', slackInvited: false } },
          { runValidators: false }
        )
      }
      
      // Send notification to vetting channel
      await slackService.notifyNewMember(
        member.name,
        member.email,
        member.circle,
        member.contributionAmount,
        inviteResult.status
      )
      
      console.warn(`Slack invitation processed: ${inviteResult.status}`)
    } else {
      // Update member record to reflect failed invitation
      await Member.findByIdAndUpdate(
        member._id,
        { $set: { slackInviteStatus: 'failed' } },
        { runValidators: false }
      )
      
      console.error(`Failed to process Slack invitation: ${inviteResult.error}`)
      // Don't throw error - member creation should still succeed
    }
  } catch (error) {
    console.error('Error during Slack invitation process:', error)
    
    // Update member record to reflect failed invitation
    try {
      await Member.findByIdAndUpdate(
        member._id,
        { $set: { slackInviteStatus: 'failed' } },
        { runValidators: false }
      )
    } catch (saveError) {
      console.error('Failed to update member Slack status:', saveError)
    }
    
    // Don't throw error - member creation should still succeed
  }
}

export default defineEventHandler(async (event) => {
  // Ensure database is connected
  await connectDB()
  
  const validatedData = await validateBody(event, memberCreateSchema)

  try {
    // Check if member already exists
    const existingMember = await Member.findOne({ email: validatedData.email })
    if (existingMember) {
      throw createError({
        statusCode: 409,
        statusMessage: 'A member with this email already exists'
      })
    }

    const member = new Member(validatedData)
    await member.save()

    await assignMemberNumber(member._id)

    // Log member joined
    logActivity(member._id, 'member_joined', {
      circle: member.circle
    }, { timestamp: member.createdAt })

    // Send Slack invitation for new members
    await inviteToSlack(member)

    // Send welcome email (non-blocking)
    try {
      await sendWelcomeEmail(member)
      logActivity(member._id, 'email_sent', {
        emailType: 'welcome',
        subject: 'Welcome to Ghost Guild'
      })
    } catch (emailError) {
      console.error('Failed to send welcome email:', emailError)
    }

    return {
      success: true,
      member: {
        id: member._id,
        email: member.email,
        name: member.name,
        circle: member.circle,
        contributionAmount: member.contributionAmount,
        status: member.status
      }
    }
  } catch (error) {
    if (error.statusCode) {
      throw error
    }
    throw createError({
      statusCode: 400,
      statusMessage: 'Member creation failed'
    })
  }
})