import { NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { auth } from '@/lib/auth'
import { addCredit } from '@/lib/services/credit'
import { headers } from 'next/headers'

// POST /api/users/sync - 同步Better Auth用户到Prisma数据库
export async function POST() {
  try {
    const session = await auth.api.getSession({
      headers: await headers()
    })

    if (!session?.user) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
    }

    const authUser = session.user

    // 先检查用户是否存在
    let user = await prisma.user.findUnique({
      where: { id: authUser.id }
    })

    let isNewUser = false

    if (user) {
      // 用户已存在，更新信息
      user = await prisma.user.update({
        where: { id: authUser.id },
        data: {
          email: authUser.email,
          name: authUser.name,
          image: authUser.image,
          updatedAt: new Date()
        }
      })
    } else {
      // 用户不存在，需要创建
      try {
        user = await prisma.user.create({
          data: {
            id: authUser.id,
            email: authUser.email,
            name: authUser.name,
            emailVerified: authUser.emailVerified ?? false,
            image: authUser.image,
            subscriptionPlanId: 'free'
          }
        })
        isNewUser = true
      } catch (createError: unknown) {
        // 如果是唯一约束错误，可能是并发创建导致的
        const prismaError = createError as { code?: string }
        if (prismaError.code === 'P2002') {
          console.warn(`Concurrent user creation detected for ${authUser.id}, fetching existing user`)
          user = await prisma.user.findUnique({
            where: { id: authUser.id }
          })
          if (!user) {
            throw createError // 如果还是找不到用户，重新抛出错误
          }
        } else {
          throw createError
        }
      }
    }

    if (isNewUser) {
      // 为新用户添加系统赠送的5USD信用额度（1个月后过期）
      const expiresAt = new Date()
      expiresAt.setMonth(expiresAt.getMonth() + 1)
      
      try {
        await addCredit(
          user.id,
          5.0,
          'system_gift',
          '系统赠送 - 新用户礼包',
          expiresAt
        )
        console.log(`Added welcome credit for new user: ${user.id}`)
      } catch (creditError) {
        console.error('Failed to add welcome credit:', creditError)
        // 不影响用户创建流程
      }

      return NextResponse.json({ 
        message: 'User created successfully', 
        user 
      }, { status: 201 })
    } else {
      return NextResponse.json({ 
        message: 'User updated successfully', 
        user 
      })
    }

  } catch (error) {
    console.error('Error syncing user:', error)
    return NextResponse.json(
      { error: 'Failed to sync user' },
      { status: 500 }
    )
  }
}

// GET /api/users/sync - 获取当前用户信息
export async function GET() {
  try {
    const session = await auth.api.getSession({
      headers: await headers()
    })

    if (!session?.user) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
    }

    // 从Prisma数据库获取用户信息
    const user = await prisma.user.findUnique({
      where: { id: session.user.id }
    })

    if (!user) {
      return NextResponse.json({ error: 'User not found in database' }, { status: 404 })
    }

    return NextResponse.json({ user })

  } catch (error) {
    console.error('Error fetching user:', error)
    return NextResponse.json(
      { error: 'Failed to fetch user' },
      { status: 500 }
    )
  }
}