import { NextResponse } from "next/server"
import { getWeights, setWeights } from "@/services/rfm-engine"

export async function GET() {
  return NextResponse.json({ success: true, data: getWeights() })
}

export async function POST(req: Request) {
  try {
    const body = await req.json()
    let R = Number(body.R ?? 0.5)
    let F = Number(body.F ?? 0.3)
    let M = Number(body.M ?? 0.2)

    // 归一化
    const sum = Math.max(0.000001, R + F + M)
    R = R / sum
    F = F / sum
    M = M / sum

    setWeights({ R, F, M })
    return NextResponse.json({ success: true, data: { R, F, M } })
  } catch (e: any) {
    return NextResponse.json({ success: false, error: e?.message || "Invalid input" }, { status: 400 })
  }
}
