import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const banks = [
  ["002", "Bank Rakyat Indonesia (Persero) Tbk"],
  ["008", "Bank Mandiri (Persero) Tbk"],
  ["009", "Bank Negara Indonesia (Persero) Tbk"],
  ["014", "Bank Central Asia Tbk"],
  ["019", "Bank Panin Tbk"],
  ["022", "Bank CIMB Niaga Tbk"],
  ["023", "Bank UOB Indonesia"],
  ["026", "Bank Lippo"],
  ["028", "Bank OCBC NISP Tbk"],
  ["030", "American Express Bank"],
  ["031", "Citibank N.A."],
  ["032", "JP. Morgan Chase Bank NA"],
  ["033", "Bank of America N.A."],
  ["034", "ING Indonesia Bank"],
  ["036", "Bank Ekspor Indonesia"],
  ["037", "Bank Artha Graha Int'l"],
  ["039", "Bank Credit Agricole Indosuez"],
  ["040", "The Bank of Hongkong & Shanghai B.C. (Hongkong)"],
  ["041", "The Bank of Tokyo Mitsubishi UFJ"],
  ["042", "Bank Sumitomo Mitsui Indonesia"],
  ["045", "Bank Sumitomo Mitsui Indonesia"],
  ["046", "Bank DBS Indonesia"],
  ["047", "Bank Resona Perdania"],
  ["048", "Bank Mizuho Indonesia"],
  ["050", "Standard Chartered Bank"],
  ["052", "Bank ABN Amro"],
  ["053", "Bank Keppel Tatlee Buana"],
  ["054", "Bank Capital Indonesia"],
  ["057", "Bank BNP Paribas Indonesia"],
  ["059", "Korea Exchange Bank Danamon"],
  ["061", "ANZ Indonesia"],
  ["067", "Deutsche Bank AG"],
  ["068", "Bank Woori Indonesia"],
  ["069", "Bank of China"],
  ["076", "Bank Bumi Arta"],
  ["089", "Bank IFI"],
  ["093", "Bank Jtrust"],
  ["097", "Bank Haja"],
  ["110", "Bank Jabar Banten (BJB)"],
  ["111", "Bank DKI"],
  ["112", "Bank Pembangunan Daerah Daerah Istimewa Yogyakarta"],
  ["113", "Bank Jateng"],
  ["114", "Bank Jatim"],
  ["115", "Bank Jambi"],
  ["116", "Bank Aceh"],
  ["117", "Bank Sumut"],
  ["118", "Bank Nagari"],
  ["119", "Bank Riau Kepri"],
  ["120", "Bank Sumsel Babel"],
  ["121", "Bank Lampung"],
  ["122", "Bank Kalsel"],
  ["123", "Bank Kaltimtara"],
  ["124", "Bank Kaltim & Utara"],
  ["125", "Bank Kalteng"],
  ["126", "Bank Sulselbar"],
  ["127", "Bank SulutGo"],
  ["128", "Bank NTB Syariah"],
  ["129", "Bank Bali"],
  ["130", "Bank NTT"],
  ["131", "Bank Maluku Malut"],
  ["132", "Bank Papua"],
  ["133", "Bank Bengkulu"],
  ["134", "Bank Sulselbar"],
  ["135", "Bank Sultra"],
  ["136", "Bank Sulawesi Tenggara"],
  ["137", "Bank Sulawesi Tengah"],
  ["138", "Bank Sulawesi Selatan"],
  ["139", "Bank Sulawesi Utara"],
  ["141", "Bank Kalteng (Kaltimtara)"],
  ["147", "Bank Muamalat Indonesia"],
  ["145", "Bank Nusantara Parahyangan"],
  ["146", "Bank of India Indonesia"],
  ["151", "Bank Mestika Dharma"],
  ["152", "Shinhan Bank Indonesia (Metro Express)"],
  ["153", "Bank Sinarmas"],
  ["157", "Bank Maspion"],
  ["159", "Bank Hagakita"],
  ["161", "Bank Ganesha"],
  ["162", "Bank Windu Kentjana"],
  ["166", "Bank ICBC Indonesia (Halim Indonesia Bank)"],
  ["167", "Bank QNB Indonesia (QNB Kesawan)"],
  ["171", "Bank of Tokyo Mitsubishi UFJ Indonesia"],
  ["176", "Bank QNB Indonesia"],
  ["186", "Bank Agris"],
  ["213", "Bank Tabungan Pensiunan Nasional (BTPN)"],
  ["200", "Bank Tabungan Negara (BTN)"],
  ["203", "Bank Bumi Arta"],
  ["213", "BTPN/Jenius BTPN"],
  ["216", "Bank Artha Graha International"],
  ["240", "Bank Victoria International"],
  ["244", "Bank Index Selindo"],
  ["245", "Bank Kesejahteraan Ekonomi"],
  ["246", "Bank Harfa"],
  ["247", "Bank Artos Indonesia"],
  ["251", "Prima Master Bank"],
  ["252", "Bank Persyarikatan Indonesia"],
  ["253", "Liman International Bank"],
  ["254", "Bank Dipo International (Sahabat Sampoerna)"],
  ["255", "Bank Fama Internasional"],
  ["256", "Bank Kesehatan Bumi Arta"],
  ["257", "Bank Mayora Indonesia"],
  ["258", "Bank Royal Indonesia"],
  ["259", "Centratama Nasional Bank"],
  ["261", "Bank Indonesia"],
  ["262", "Bank Multika"],
  ["263", "Bank Permata"],
  ["267", "Bank Raya Indonesia"],
  ["273", "Bank Nusantara Parahyangan Syariah"],
  ["274", "Bank Jasa Jakarta"],
  ["275", "Bank Alfindo"],
  ["282", "Bank Yudha Bhakti"],
  ["283", "Bank MNC"],
  ["285", "Bank Bintang Manunggal"],
  ["286", "Bank Haga"],
  ["287", "Bank Mega"],
  ["294", "Bank Bisnis Internasional"],
  ["295", "Bank Sri Partha"],
  ["422", "BRI Syariah (migrasi ke BSI)"],
  ["423", "BCA Syariah"],
  ["425", "Bank BJB Syariah"],
  ["426", "Bank Mega"],
  ["427", "Bank Syariah Mandiri / BNI Syariah"],
  ["431", "Bank BTPN Syariah"],
  ["451", "Bank Syariah Indonesia"],
  ["453", "Bank BPD Kaltim"],
  ["454", "Bank Jatim Syariah"],
  ["456", "Bank Nusantara Parahyangan Syariah"],
  ["503", "Bank Agris"],
  ["506", "Bank Sinarmas Syariah"],
  ["510", "Prima Master Bank"],
  ["513", "Bank Ina Perdana"],
  ["517", "Bank Harfa"],
  ["521", "Bank Akita"],
  ["526", "Liman International Bank"],
  ["531", "Anglomas Internasional Bank"],
  ["535", "Bank Kesejahteraan"],
  ["536", "BCA Syariah"],
  ["542", "Artos Indonesia Bank"],
  ["547", "Bank Purba Danarta"],
  ["548", "Bank Multi Arta Sentosa"],
  ["550", "Bank Andara"],
  ["553", "Mayora Bank"],
  ["555", "Bank Victoria International"],
  ["562", "Bank Fama International"],
  ["564", "Bank Mandiri Taspen Pos"],
  ["566", "Bank Victoria International"],
  ["567", "Bank Harda"],
  ["688", "BPR KS"],
  ["761", "Bank Rakyat Indonesia"],
  ["789", "Indosat Dompetku"],
  ["811", "BPR Bank Indonesia"],
  ["836", "Bank OCBC NISP"],
  ["911", "Link Aja"],
  ["949", "Bank CTBC (China Trust) Indonesia"],
  ["950", "Bank Commonwealth"],
  ["956", "Bank Merincorp"],
  ["957", "Bank Diners Club"],
  ["985", "The Royal Bank"],
  ["988", "Bank Swaguna"],
  ["989", "Bank QNB Indonesia"],
  ["990", "Bank Swaguna"],
  ["992", "Bank Himpunan Saudara"],
  ["993", "Bank Jasa Jakarta"],
  ["994", "Bank Swaguna"]
];

function toRecords(list) {
  const byCode = new Map();
  const byName = new Set();

  for (const [code, name] of list) {
    const trimmedCode = String(code).trim().padStart(3, "0");
    const trimmedName = String(name).trim();
    if (!trimmedCode || !trimmedName) continue;
    const normalizedName = trimmedName.toUpperCase();

    if (!byCode.has(trimmedCode) && !byName.has(normalizedName)) {
      byCode.set(trimmedCode, trimmedName);
      byName.add(normalizedName);
    }
  }
  return [...byCode.entries()].map(([code, name]) => ({ code, name, address: "Indonesia" }));
}

async function main() {
  const records = toRecords(banks);
  let created = 0;
  let updated = 0;

  for (const record of records) {
    const current = await prisma.bank.findUnique({ where: { code: record.code } });
    if (current) {
      await prisma.bank.update({
        where: { code: record.code },
        data: { name: record.name, address: record.address, status: "ACTIVE" }
      });
      updated += 1;
    } else {
      await prisma.bank.create({
        data: { code: record.code, name: record.name, address: record.address, status: "ACTIVE" }
      });
      created += 1;
    }
  }

  const total = await prisma.bank.count();
  console.log(`Seeded banks: created=${created}, updated=${updated}, total=${total}`);
}

main()
  .catch((error) => {
    console.error(error);
    process.exitCode = 1;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
