import { NextResponse } from "next/server";
import { PrismaClient } from "@prisma/client";
import { loadSystemConfig } from "@/lib/system-config";

export const runtime = "nodejs";

const prisma = new PrismaClient();

export async function GET() {
  try {
    const appSetup = await prisma.appSetup.findUnique({
      where: { id: 1 },
      select: {
        googleAuthEnabled: true,
        socials: true,
      },
    });

    const systemConfig = await loadSystemConfig();

    return NextResponse.json({
      ok: true,
      setup: {
        data: {
          googleAuthEnabled: appSetup?.googleAuthEnabled || false,
          googleClientId: systemConfig.oauth?.google?.clientId || "",
          socials: appSetup?.socials || {},
          smtp: {
            enabled: systemConfig.email?.enabled || false,
          },
          oauth: {
            google: {
              enabled: systemConfig.oauth?.google?.enabled || false,
              clientId: systemConfig.oauth?.google?.clientId || "",
            },
            github: {
              enabled: systemConfig.oauth?.github?.enabled || false,
              clientId: systemConfig.oauth?.github?.clientId || "",
            },
            facebook: {
              enabled: systemConfig.oauth?.facebook?.enabled || false,
              clientId: systemConfig.oauth?.facebook?.clientId || "",
            },
            discord: {
              enabled: systemConfig.oauth?.discord?.enabled || false,
              clientId: systemConfig.oauth?.discord?.clientId || "",
            },
          },
        },
      },
    });
  } catch (error) {
    console.error("Error fetching app setup:", error);
    return NextResponse.json(
      { ok: false, message: "Failed to fetch configuration" },
      { status: 500 }
    );
  }
}
