// app/api/wechat/login/route.ts
// 微信小程序登录接口

import { NextRequest, NextResponse } from 'next/server'

const APPID = process.env.WECHAT_APPID || 'wx0976665c3a3d5a7c'
const SECRET = process.env.WECHAT_APPSECRET || 'a262f1be43422f03734f205d0bca1882'

// POST: 微信小程序登录
export async function POST(req: NextRequest) {
  try {
    const body = await req.json()
    const { code } = body

    if (!code) {
      return NextResponse.json(
        { error: '缺少code参数' },
        { status: 400 }
      )
    }

    // 调用微信API获取session_key和openid
    const wxUrl = `https://api.weixin.qq.com/sns/jscode2session?appid=${APPID}&secret=${SECRET}&js_code=${code}&grant_type=authorization_code`

    const wxResponse = await fetch(wxUrl)
    const wxData = await wxResponse.json()

    if (wxData.errcode) {
      console.error('微信登录失败:', wxData)
      return NextResponse.json(
        { error: wxData.errmsg || '微信登录失败' },
        { status: 400 }
      )
    }

    const { openid, session_key, unionid } = wxData

    // TODO: 将openid和session_key存储到数据库
    // 这里简单生成一个token
    const token = Buffer.from(`${openid}:${Date.now()}`).toString('base64')

    // 返回用户信息和token
    const user = {
      id: openid,
      openid,
      unionid,
      nickname: '用户' + openid.substr(-4),
      avatar: 'https://picsum.photos/200/200?random=' + openid.substr(-2),
      inviteCode: generateInviteCode(openid),
      isPurchased: false,
      createdAt: new Date().toISOString()
    }

    return NextResponse.json({
      success: true,
      token,
      user,
      message: '登录成功'
    })
  } catch (error) {
    console.error('登录接口错误:', error)
    return NextResponse.json(
      { error: '服务器错误' },
      { status: 500 }
    )
  }
}

// 生成邀请码
function generateInviteCode(openid: string): string {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
  const hash = openid.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0)
  let code = ''
  
  for (let i = 0; i < 6; i++) {
    code += chars.charAt((hash + i) % chars.length)
  }
  
  return code
}
