import mongoose from "mongoose";
import dotenv from "dotenv";
import AdminModel from "./models/adminModel.js";

dotenv.config();

mongoose.connect(process.env.MONGODB_URI)
  .then(() => console.log("✅ Database Connected"))
  .catch(err => console.log("❌ DB Connection Error:", err));

const createAdmin = async () => {
  try {
    const exists = await AdminModel.findOne({ email: "admin@gmail.com" });
    if (exists) {
      console.log("⚠️ Admin already exists!");
      process.exit();
    }

    const admin = new AdminModel({
      name: "Super Admin",
      email: "admin@gmail.com",
      password: "admin123"
    });

    await admin.save();
    console.log("🎉 Admin Created Successfully!");
    process.exit();
  } catch (err) {
    console.log("❌ Error:", err);
    process.exit();
  }
};

createAdmin();
