/**
 * GET /api/notifications
 *
 * Returns user notifications for the dashboard
 * Supports both NextAuth and OTP (auth_token) authentication
 */

import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/lib/auth";
import { getAuthenticatedUser } from "@/lib/utils/auth-helpers.util";
import { logger } from "@/lib/logger";

export const dynamic = "force-dynamic";

export async function GET(request: NextRequest) {
  try {
    // Authenticate user - supports both NextAuth and OTP auth_token
    const user = await getAuthenticatedUser(request, auth);

    if (!user) {
      logger.warn("Unauthorized notifications request");
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }

    const limit = parseInt(request.nextUrl.searchParams.get("limit") || "20");
    const offset = parseInt(request.nextUrl.searchParams.get("offset") || "0");
    const unreadOnly =
      request.nextUrl.searchParams.get("unreadOnly") === "true";
    const type = request.nextUrl.searchParams.get("type") || undefined;

    // TODO: Implement actual notifications from database
    // For now, return empty array
    const notifications: Array<{ readAt?: string | null }> = [];

    logger.info("Notifications retrieved", {
      userId: user.userId,
      count: notifications.length,
    });

    return NextResponse.json({
      success: true,
      notifications,
      total: notifications.length,
      limit,
      offset,
      unreadCount: notifications.filter(
        (n: { readAt?: string | null }) => !n.readAt,
      ).length,
      pagination: {
        limit,
        offset,
        hasMore: false,
      },
    });
  } catch (error) {
    logger.error("Notifications error", {
      error: error instanceof Error ? error.message : "Unknown error",
    });

    return NextResponse.json(
      { error: "Failed to retrieve notifications" },
      { status: 500 },
    );
  }
}
