import { NextResponse } from 'next/server';
import { generateAssetPack } from '@/lib/packGenerator';
import { loadSession, saveSession } from '@/lib/storage';
import type { GeneratePackRequest, GeneratePackResponse, PackKind } from '@/lib/types';

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

const PACK_KINDS: PackKind[] = ['patent', 'production', 'marketing'];

export async function POST(req: Request) {
  const body = (await req.json()) as GeneratePackRequest;
  const { sessionId, imageId, kind } = body;

  if (!sessionId || !imageId || !PACK_KINDS.includes(kind)) {
    return NextResponse.json({ error: 'sessionId, imageId and valid kind 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 });

  try {
    const { pack, manifest, provider } = await generateAssetPack({ session, sourceImage, kind });
    const packs = (session.packs ?? []).filter(item => !(item.kind === kind && item.sourceImageId === imageId));
    const exports = (session.exports ?? []).filter(item => !(item.packKind === kind && item.source.sourceImageId === imageId));

    session.characterSpec = pack.characterSpec;
    session.packs = [...packs, pack];
    session.exports = [...exports, manifest];
    await saveSession(session);

    const response: GeneratePackResponse = { pack, manifest, provider };
    return NextResponse.json(response);
  } catch (error) {
    return NextResponse.json({ error: String(error) }, { status: 500 });
  }
}
import { NextResponse } from 'next/server';
import { generateAssetPack } from '@/lib/packGenerator';
import { loadSession, saveSession } from '@/lib/storage';
import type { GeneratePackRequest, GeneratePackResponse, PackKind } from '@/lib/types';

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

const PACK_KINDS: PackKind[] = ['patent', 'production', 'marketing'];

export async function POST(req: Request) {
  const { sessionId, imageId, kind } = (await req.json()) as GeneratePackRequest;
  if (!sessionId || !imageId || !PACK_KINDS.includes(kind)) {
    return NextResponse.json({ error: 'sessionId, imageId and valid kind 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 (sourceImage.status !== 'selected') {
    return NextResponse.json({ error: 'image must be selected before generating packs' }, { status: 400 });
  }

  try {
    const { pack, manifest, provider } = await generateAssetPack({ session, sourceImage, kind });
    session.characterSpec = pack.characterSpec;
    session.packs = [
      ...(session.packs ?? []).filter(existing => !(existing.kind === kind && existing.sourceImageId === imageId)),
      pack,
    ];
    session.exports = [
      ...(session.exports ?? []).filter(existing => !(existing.packKind === kind && existing.source.sourceImageId === imageId)),
      manifest,
    ];
    await saveSession(session);

    return NextResponse.json({ pack, manifest, provider } satisfies GeneratePackResponse);
  } catch (error) {
    return NextResponse.json({ error: String(error) }, { status: 500 });
  }
}
