import db from "../src/models/index.js";

async function seed() {
  console.log("🌱 Starting database seeding for KYC Stages and System Services...");
  try {
    await db.sequelize.authenticate();
    console.log("✔ Database connection authenticated.");

    // 1. Seed KYC Stages
    const stagesData = [
      { name: "BVN Stage", code: "bvn_stage", sequence: 1, description: "Stage 1: BVN Verification" },
      { name: "NIN Stage", code: "nin_stage", sequence: 2, description: "Stage 2: NIN ID Verification" }
    ];

    const stages = [];
    for (const data of stagesData) {
      const [stage] = await db.KycStage.findOrCreate({
        where: { code: data.code },
        defaults: data
      });
      stages.push(stage);
      console.log(`- Stage verified/created: ${stage.name} (${stage.code})`);
    }

    // 2. Seed System Services
    const servicesData = [
      { name: "NGN Wallet as a Service", code: "ngn_wallet_as_a_service", currency: "NGN", type: "currency_based", description: "Enables NGN wallet balance and storage", isActive: true, canReceive: true, canSend: false },
      { name: "NGN Virtual Account Service", code: "ngn_virtual_account_service", currency: "NGN", type: "currency_based", description: "Generates virtual NGN accounts for customers", isActive: true, canReceive: true, canSend: true },
      { name: "USD Virtual Account Service", code: "usd_virtual_account_service", currency: "USD", type: "currency_based", description: "Generates virtual USD accounts for customers", isActive: true, canReceive: true, canSend: true },
      { name: "Card Service", code: "card_service", currency: null, type: "global_based", description: "Enables debit and virtual cards issuance", isActive: true, canReceive: true, canSend: true }
    ];

    const services = {};
    for (const data of servicesData) {
      const [service] = await db.SystemService.findOrCreate({
        where: { code: data.code },
        defaults: data
      });
      services[service.code] = service;
      console.log(`- Service verified/created: ${service.name} (${service.code})`);
    }

    // 3. Seed Stage-to-Service Mappings
    const bvnStage = stages.find(s => s.code === "bvn_stage");
    const ninStage = stages.find(s => s.code === "nin_stage");

    const mappings = [
      { stageId: bvnStage.id, serviceId: services["ngn_wallet_as_a_service"].id, isActive: true },
      { stageId: bvnStage.id, serviceId: services["ngn_virtual_account_service"].id, isActive: true },
      { stageId: ninStage.id, serviceId: services["usd_virtual_account_service"].id, isActive: true },
      { stageId: ninStage.id, serviceId: services["card_service"].id, isActive: true }
    ];

    for (const map of mappings) {
      await db.KycStageService.findOrCreate({
        where: { stageId: map.stageId, serviceId: map.serviceId },
        defaults: map
      });
    }

    console.log("✅ Seeding completed successfully.");
    process.exit(0);
  } catch (error) {
    console.error("❌ Seeding failed:", error);
    process.exit(1);
  }
}

seed();
