import { streamPrompt } from "@/ai";
import { loadConfig } from "@/config";
import { saveImage } from "@/image";
import { jobs } from "@/jobs";

async function main(): Promise<void> {
	const config = loadConfig();
	const enabledJobs = jobs.filter((job) => job.enabled !== false);

	if (enabledJobs.length === 0) {
		throw new Error("No enabled jobs found.");
	}

	for (const job of enabledJobs) {
		console.log(`\n=== ${job.name} ===`);
		console.log(`Prompt: ${job.prompt}`);
		console.log(`Input images: ${job.images?.length ?? 0}`);

		const image = await streamPrompt(config, job);

		const savedImagePath = await saveImage(
			image.bytes,
			image.mediaType,
			job.name,
		);

		console.log(`\nSaved image: ${savedImagePath}`);
		console.log(`Image media type: ${image.mediaType}`);
	}
}

main().catch((error: unknown) => {
	const message = error instanceof Error ? error.message : String(error);
	console.error(`\nRequest failed: ${message}`);
	process.exitCode = 1;
});
