import { NextResponse } from 'next/server';

import { PrismaClient } from '@prisma/client';

const usingPrisma = false;
let prisma: PrismaClient;
if (usingPrisma) prisma = new PrismaClient();

export async function GET(request: Request) {
	try {
		const events = [
			{
				id: "1234",
				title: "Earthquake in Germany",
				text1: "Magnitude 8.5",
				text2: "30 minutes ago",
				magnitude: 8.5,
				longitude: 10.4515, // Near Berlin, Germany
				latitude: 52.52,
			},
			{
				id: "2134",
				title: "Earthquake in California",
				text1: "Magnitude 5.3",
				text2: "2 hours ago",
				magnitude: 5.3,
				longitude: -122.4194, // Near San Francisco, California, USA
				latitude: 37.7749,
			},
			{
				id: "2314",
				title: "Tremor in Japan",
				text1: "Magnitude 4.7",
				text2: "5 hours ago",
				magnitude: 4.7,
				longitude: 139.6917, // Near Tokyo, Japan
				latitude: 35.6762,
			},
			{
				id: "2341",
				title: "Tremor in Spain",
				text1: "Magnitude 2.1",
				text2: "10 hours ago",
				magnitude: 2.1,
				longitude: -3.7038, // Near Madrid, Spain
				latitude: 40.4168,
			},
		];

		let earthquakes;
		if (usingPrisma) earthquakes = await prisma.earthquakes.findMany();

		if (earthquakes) {
			return NextResponse.json({ message: "Got earthquakes successfully", earthquakes }, { status: 200 });
		} else {
			return NextResponse.json({ message: "Got earthquakes successfully", earthquakes: events }, { status: 200 });
			// return NextResponse.json({ message: "Failed to get earthquakes" }, { status: 401 });
		}
	} catch (error) {
		console.error("Error in earthquakes endpoint:", error);
		return NextResponse.json({ message: "Internal Server Error" }, { status: 500 });
	} finally {
		if (usingPrisma) await prisma.$disconnect();
	}
}
