import { auth } from "@clerk/nextjs/server";
import { NextRequest, NextResponse } from "next/server";
import { getDatabase } from "@/lib/database";
import { ensureUserSynced } from "@/lib/sync-user";
import log from "@/lib/logger";

export async function POST(req: NextRequest) {
  try {
    const { userId } = await auth();
    if (!userId) return new NextResponse("Unauthorized", { status: 401 });

    const db = await getDatabase();
    await ensureUserSynced(userId, db);

    const body = await req.json();
    const { date, entries, totalWater, waterGoal } = body;

    if (!date) {
      return NextResponse.json({ error: "Date is required" }, { status: 400 });
    }

    // Check if entry already exists for this date
    const existing = await db.getDailyHydration(userId, date);

    let result;
    if (existing) {
      // Update existing entry
      result = await db.updateDailyHydration(existing.id, {
        entries,
        totalWater: totalWater ?? existing.totalWater,
        waterGoal: waterGoal ?? existing.waterGoal,
      });
    } else {
      // Create new entry
      result = await db.createDailyHydration({
        userId,
        date,
        entries: entries || [],
        totalWater: totalWater || 0,
        waterGoal: waterGoal || 2000,
      });
    }

    return NextResponse.json(result);
  } catch (error) {
    log.error("Failed to save hydration data", error);
    return NextResponse.json(
      { error: "Internal server error" },
      { status: 500 },
    );
  }
}

export async function GET(req: NextRequest) {
  try {
    const { userId } = await auth();
    if (!userId) return new NextResponse("Unauthorized", { status: 401 });

    const db = await getDatabase();
    await ensureUserSynced(userId, db);

    const url = new URL(req.url);
    const date = url.searchParams.get("date");
    const startDate = url.searchParams.get("startDate");
    const endDate = url.searchParams.get("endDate");

    // Single date query
    if (date) {
      const result = await db.getDailyHydration(userId, date);
      return NextResponse.json(result);
    }

    // Date range query
    if (startDate && endDate) {
      const results = await db.getDailyHydrationRange(
        userId,
        startDate,
        endDate,
      );
      return NextResponse.json(results);
    }

    return NextResponse.json(
      { error: "Either 'date' or 'startDate' and 'endDate' are required" },
      { status: 400 },
    );
  } catch (error) {
    log.error("Failed to fetch hydration data", error);
    return NextResponse.json(
      { error: "Internal server error" },
      { status: 500 },
    );
  }
}

export async function DELETE(req: NextRequest) {
  try {
    const { userId } = await auth();
    if (!userId) return new NextResponse("Unauthorized", { status: 401 });

    const db = await getDatabase();
    await ensureUserSynced(userId, db);

    const url = new URL(req.url);
    const id = url.searchParams.get("id");

    if (!id) {
      return NextResponse.json({ error: "ID is required" }, { status: 400 });
    }

    // Verify ownership before deletion
    const existing = await db.getDailyHydrationById(id);
    if (!existing) {
      return NextResponse.json({ error: "Not found" }, { status: 404 });
    }

    if (existing.userId !== userId) {
      return NextResponse.json(
        { error: "Forbidden: You can only delete your own hydration data" },
        { status: 403 },
      );
    }

    const success = await db.deleteDailyHydration(id);

    if (success) {
      return NextResponse.json({ success: true });
    } else {
      return NextResponse.json({ error: "Not found" }, { status: 404 });
    }
  } catch (error) {
    log.error("Failed to delete hydration data", error);
    return NextResponse.json(
      { error: "Internal server error" },
      { status: 500 },
    );
  }
}
