import { NextResponse } from "next/server";
import { sql } from "@/db";
import { requireFamily } from "@/lib/auth";
import { MILESTONES } from "@/lib/milestones";

export async function GET(request: Request) {
  const auth = await requireFamily();
  if (!auth.success) return NextResponse.json({ error: auth.error }, { status: auth.status });

  const { searchParams } = new URL(request.url);
  const childId = searchParams.get("childId");
  if (!childId) return NextResponse.json({ error: "childId required" }, { status: 400 });

  const familyId = auth.session!.familyId!;

  const rows = await sql`
    SELECT milestone_key, achieved_at, notes
    FROM milestone_achievements
    WHERE child_id = ${childId} AND family_id = ${familyId}
  `;

  const achievedMap = new Map(rows.map(r => [r.milestone_key, r]));

  const items = MILESTONES.map(m => ({
    ...m,
    achieved: achievedMap.has(m.key),
    achievedAt: achievedMap.get(m.key)?.achieved_at ?? null,
    notes: achievedMap.get(m.key)?.notes ?? null,
  }));

  return NextResponse.json({ items });
}

export async function POST(request: Request) {
  const auth = await requireFamily();
  if (!auth.success) return NextResponse.json({ error: auth.error }, { status: auth.status });

  const familyId = auth.session!.familyId!;
  const { childId, milestoneKey, achievedAt, notes } = await request.json();

  if (!childId || !milestoneKey || !achievedAt) {
    return NextResponse.json({ error: "childId, milestoneKey, achievedAt required" }, { status: 400 });
  }

  await sql`
    INSERT INTO milestone_achievements (child_id, family_id, milestone_key, achieved_at, notes)
    VALUES (${childId}, ${familyId}, ${milestoneKey}, ${achievedAt}, ${notes ?? null})
    ON CONFLICT (child_id, milestone_key) DO UPDATE SET
      achieved_at = EXCLUDED.achieved_at,
      notes = EXCLUDED.notes
  `;

  return NextResponse.json({ success: true });
}
