import { NextResponse } from "next/server";
import { auth } from "@/lib/auth";
import { headers } from "next/headers";
import { prisma } from "@/lib/prisma";

export async function GET() {
  try {
    const session = await auth.api.getSession({ headers: await headers() });
    if (!session?.user) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }
    const user = session.user;

    // 获取所有激活的模型（不再与套餐绑定）
    const allModels = await prisma.model.findMany({
      where: { isActive: true },
      orderBy: [
        { provider: "asc" },
        { name: "asc" }
      ]
    });

    const models = allModels.map(model => ({
      id: model.id,
      modelId: model.modelId,
      name: model.name,
      provider: model.provider,
      serviceProvider: model.serviceProvider,
      outputType: model.outputType,
      description: model.description,
      maxTokens: model.maxTokens,
      inputCostPer1k: model.inputCostPer1k,
      outputCostPer1k: model.outputCostPer1k,
      supportedFeatures: model.supportedFeatures,
    }));

    return NextResponse.json({ models });
  } catch (error) {
    console.error("Error fetching available models:", error);
    return NextResponse.json(
      { error: "Internal server error" },
      { status: 500 }
    );
  }
}