// Script ponctuel : génère et enregistre le brief de concept PROTO pour le
// drop O.M.T, créé avant que la génération automatique n'existe. Les
// nouveaux drops n'en ont plus besoin — createDrop() le fait à leur création.
import { PrismaClient } from "@prisma/client"
import { PrismaPg } from "@prisma/adapter-pg"
import { Pool } from "pg"
import { generateDropConcept } from "../lib/proto-concept"

const pool = new Pool({ connectionString: process.env.DATABASE_URL })
const db = new PrismaClient({ adapter: new PrismaPg(pool) })

async function main() {
  const drop = await db.drop.findUniqueOrThrow({ where: { slug: "omt" } })
  const concept = await generateDropConcept({ name: drop.name, description: drop.description })
  await db.drop.update({
    where: { id: drop.id },
    data: { aiConcept: concept.text, aiConceptSource: concept.source },
  })
  console.log(`Concept généré (source: ${concept.source}) :\n\n${concept.text}`)
}

main()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await db.$disconnect()
    await pool.end()
  })
