import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { requireAuth } from "@/lib/auth";

export async function GET(req: NextRequest) {
  const jwtPayload = await requireAuth(req);
  if (!jwtPayload) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const user = await prisma.user.findUnique({
    where: { id: jwtPayload.userId },
  });

  if (!user) {
    return NextResponse.json({ error: "User not found" }, { status: 404 });
  }

  return NextResponse.json({
    user: {
      id: user.id,
      email: user.email,
      name: user.name,
      isPremium: user.isPremium,
      isPro: user.isPro,
      flhBalance: user.flhBalance,
      dailyMsgCount: user.dailyMsgCount,
      experienceLevel: user.experienceLevel,
      madhab: user.madhab,
      coachPersona: user.coachPersona,
      preferredName: user.preferredName,
      coachingGoals: user.coachingGoals,
      trialEndsAt: user.trialEndsAt,
      createdAt: user.createdAt,
    },
  });
}
