import { db } from "./connection";
import { branches } from "./schema";
import crypto from "crypto";

function generateId(): string {
  return crypto.randomUUID();
}

async function seedBranches() {
  console.log("🌱 Seeding branches from CSV...");

  // Check if branches already exist
  const existingBranches = await db.select().from(branches);
  if (existingBranches.length > 0) {
    console.log("⏭️  Database already has branches. Skipping...");
    console.log(
      `   Existing branches: ${existingBranches.map((b) => b.code).join(", ")}`,
    );
    return;
  }

  // Branch IDs from CSV
  const branchData = [
    {
      id: "b6722de8-562b-4d51-8ad2-1f6df826a772",
      name: "สาขาหลัก (ที่ 1)",
      code: "BR1",
    },
    {
      id: "c76ce8ec-6062-4e48-9d22-0aaaa6be79b2",
      name: "สาขาหลัก (ที่ 2)",
      code: "BR2",
    },
    {
      id: "8724a231-108b-4c74-a778-0eafd282edd4",
      name: "สาขาหลัก (ที่ 3)",
      code: "BR3",
    },
  ];

  for (const branch of branchData) {
    await db.insert(branches).values(branch);
  }

  console.log(`✅ Created ${branchData.length} branches:`);
  branchData.forEach((branch) => {
    console.log(`   - ${branch.code}: ${branch.name} (${branch.id})`);
  });

  console.log(
    "\n✨ Now you can run 'npx tsx src/db/seed-emp.ts' to seed employees!",
  );
}

seedBranches().catch((error) => {
  console.error("❌ Fatal error during seeding:", error);
  process.exit(1);
});
