import path from "node:path";
import { PrismaClient } from "../src/generated/prisma/client";
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
import "dotenv/config";

const databaseUrl =
  process.env.DATABASE_URL ?? `file:${path.resolve("prisma/dev.db")}`;

const prisma = new PrismaClient({
  adapter: new PrismaBetterSqlite3({ url: databaseUrl }),
});

const moments = [
  { key: "FASTING", hour: 7, baseMin: 0.85, baseMax: 1.25 },
  { key: "LUNCH", hour: 14, baseMin: 1.05, baseMax: 1.75 },
  { key: "DINNER", hour: 21, baseMin: 1.0, baseMax: 1.7 },
] as const;

function rand(min: number, max: number) {
  return min + Math.random() * (max - min);
}

async function main() {
  await prisma.reading.deleteMany();

  const today = new Date();
  today.setHours(0, 0, 0, 0);
  const data: { measuredAt: Date; moment: string; value: number; notes: string | null }[] = [];

  for (let day = 29; day >= 0; day--) {
    for (const m of moments) {
      const d = new Date(today.getTime() - day * 86_400_000);
      d.setHours(m.hour, Math.floor(Math.random() * 30), 0, 0);
      let value = rand(m.baseMin, m.baseMax);
      // Quelques valeurs aberrantes pour montrer hypo / hyper
      if (Math.random() < 0.05) value = rand(0.5, 0.68);
      if (Math.random() < 0.05) value = rand(1.85, 2.4);
      data.push({
        measuredAt: d,
        moment: m.key,
        value: Math.round(value * 100) / 100,
        notes: Math.random() < 0.15 ? "Bonne forme" : null,
      });
    }
  }

  await prisma.reading.createMany({ data });
  console.log(`✓ ${data.length} relevés générés.`);
}

main().finally(() => prisma.$disconnect());
