import { NextResponse } from "next/server";
import { isConvexEnabled } from "@/lib/convex/enabled";
import { getAuthedConvexClient } from "@/lib/convex/route";
import { api } from "@/lib/convex/api";

type DisplayMode = "inline" | "embed";

interface RecordReferencePayload {
  workspaceId: string;
  sourcePageId: string;
  targetPageId: string;
  sourceBlockId?: string | null;
  alias?: string | null;
  displayMode: DisplayMode;
  isPreviewable?: boolean;
}

const isValidDisplayMode = (mode: string): mode is DisplayMode => mode === "inline" || mode === "embed";

export async function POST(request: Request) {
  if (isConvexEnabled()) {
    const { auth, client } = await getAuthedConvexClient();
    const body = (await request.json()) as RecordReferencePayload;
    if (!body?.workspaceId || !body?.sourcePageId || !body?.targetPageId) {
      return NextResponse.json({ error: "缺少必要参数" }, { status: 400 });
    }
    if (!isValidDisplayMode(String(body.displayMode ?? ""))) {
      return NextResponse.json({ error: "非法的引用模式" }, { status: 400 });
    }

    const reference = await client.mutation(api.references.record, {
      userId: auth.userId,
      workspaceId: body.workspaceId,
      sourcePageId: body.sourcePageId,
      targetPageId: body.targetPageId,
      sourceBlockId: body.sourceBlockId ?? null,
      alias: body.alias ?? null,
      displayMode: body.displayMode,
      isPreviewable: Boolean(body.isPreviewable ?? true),
    });

    return NextResponse.json({ reference });
  }

  return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });

  /*
  const supabase = await createSupabaseRouteClient();
  const {
    data: { session },
  } = await supabase.auth.getSession();

  if (!session) {
    return NextResponse.json({ error: "未登录" }, { status: 401 });
  }

  const body = (await request.json()) as RecordReferencePayload;
  const { workspaceId, sourcePageId, targetPageId, sourceBlockId, alias, displayMode, isPreviewable = true } = body;

  if (!workspaceId || !sourcePageId || !targetPageId) {
    return NextResponse.json({ error: "缺少必要参数" }, { status: 400 });
  }
  if (!isValidDisplayMode(displayMode)) {
    return NextResponse.json({ error: "非法的引用模式" }, { status: 400 });
  }

  const { data: membership, error: membershipError } = await supabase
    .from("workspace_members")
    .select("workspace_id")
    .eq("workspace_id", workspaceId)
    .eq("user_id", session.user.id)
    .maybeSingle();

  if (membershipError) {
    return NextResponse.json({ error: membershipError.message }, { status: 500 });
  }
  if (!membership) {
    return NextResponse.json({ error: "无权访问该工作空间" }, { status: 403 });
  }

  const { data, error } = await supabase.rpc("record_page_ref", {
    p_workspace_id: workspaceId,
    p_source_page_id: sourcePageId,
    p_source_block_id: sourceBlockId ?? null,
    p_target_page_id: targetPageId,
    p_alias: alias ?? null,
    p_display_mode: displayMode,
    p_is_previewable: isPreviewable,
    p_created_by: session.user.id,
  });

  if (error) {
    return NextResponse.json({ error: error.message }, { status: 500 });
  }

  return NextResponse.json({ reference: data });
  */
}
