import { NextResponse } from 'next/server';
import { buildCharacterSpec } from '@/lib/packGenerator';
import { detectProvider } from '@/lib/providers';
import { loadSession, saveSession } from '@/lib/storage';
import type { LockCharacterRequest, LockCharacterResponse } from '@/lib/types';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

export async function POST(req: Request) {
  const { sessionId, imageId, force = false } = (await req.json()) as LockCharacterRequest;

  if (!sessionId || !imageId) {
    return NextResponse.json({ error: 'sessionId and imageId required' }, { status: 400 });
  }

  const session = await loadSession(sessionId);
  if (!session) return NextResponse.json({ error: 'session not found' }, { status: 404 });

  const sourceImage = session.images.find(image => image.id === imageId);
  if (!sourceImage) return NextResponse.json({ error: 'image not found' }, { status: 404 });

  if (!force && session.characterSpec?.sourceImageId === imageId) {
    const response: LockCharacterResponse = {
      characterSpec: session.characterSpec,
      provider: detectProvider(),
    };
    return NextResponse.json(response);
  }

  try {
    const characterSpec = await buildCharacterSpec(session, sourceImage);
    session.characterSpec = characterSpec;
    await saveSession(session);

    const response: LockCharacterResponse = {
      characterSpec,
      provider: detectProvider(),
    };
    return NextResponse.json(response);
  } catch (error) {
    return NextResponse.json({ error: String(error) }, { status: 500 });
  }
}
import { NextResponse } from 'next/server';
import { buildCharacterSpec } from '@/lib/packGenerator';
import { detectProvider } from '@/lib/providers';
import { loadSession, saveSession } from '@/lib/storage';
import type { LockCharacterRequest, LockCharacterResponse } from '@/lib/types';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

export async function POST(req: Request) {
  const { sessionId, imageId, force = false } = (await req.json()) as LockCharacterRequest;
  if (!sessionId || !imageId) {
    return NextResponse.json({ error: 'sessionId and imageId required' }, { status: 400 });
  }

  const session = await loadSession(sessionId);
  if (!session) return NextResponse.json({ error: 'session not found' }, { status: 404 });

  const sourceImage = session.images.find(image => image.id === imageId);
  if (!sourceImage) return NextResponse.json({ error: 'image not found' }, { status: 404 });

  if (!force && session.characterSpec?.sourceImageId === imageId) {
    return NextResponse.json({
      characterSpec: session.characterSpec,
      provider: detectProvider(),
    } satisfies LockCharacterResponse);
  }

  try {
    const characterSpec = await buildCharacterSpec(session, sourceImage);
    session.characterSpec = characterSpec;
    await saveSession(session);
    return NextResponse.json({
      characterSpec,
      provider: detectProvider(),
    } satisfies LockCharacterResponse);
  } catch (error) {
    return NextResponse.json({ error: String(error) }, { status: 500 });
  }
}
