import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { candles } from '@/lib/db/schema';

export async function GET(request: NextRequest) {
  try {
    const { searchParams } = request.nextUrl;
    const checkDb = searchParams.get('check');

    const response: any = {
      status: 'ok',
      timestamp: Date.now(),
    };

    if (checkDb === 'db') {
      try {
        // Test database connectivity with a simple query
        await db.select().from(candles).limit(1);
        response.database = 'ok';
      } catch (dbError: any) {
        console.error(dbError);
        return NextResponse.json(
          {
            status: 'error',
            database: 'failed',
            message: 'Internal server error',
          },
          { status: 503 }
        );
      }
    }

    return NextResponse.json(response);
  } catch (error: any) {
    console.error(error);
    return NextResponse.json(
      {
        status: 'error',
        message: 'Internal server error',
      },
      { status: 500 }
    );
  }
}
