"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const argon = __importStar(require("@node-rs/argon2"));
const prisma_service_1 = require("../src/prisma/prisma.service");
const enums_1 = require("../generated/prisma/enums");
const prisma = new prisma_service_1.PrismaService();
async function main() {
    console.log("🌱 Seeding database...");
    const now = new Date();
    const password = await argon.hash("password123");
    const branchTgr = await prisma.branch.upsert({
        where: { code: "TGR" },
        update: {},
        create: {
            code: "TGR",
            name: "Tangerang",
            address: "Tangerang",
            beginEffective: now,
        },
    });
    const branchDepok = await prisma.branch.upsert({
        where: { code: "DPK" },
        update: {},
        create: {
            code: "DPK",
            name: "Depok",
            address: "Depok",
            beginEffective: now,
        },
    });
    const branchBintaro = await prisma.branch.upsert({
        where: { code: "BTR" },
        update: {},
        create: {
            code: "BTR",
            name: "Bintaro",
            address: "Bintaro",
            beginEffective: now,
        },
    });
    const branchMap = {
        ALL: null,
        TANGERANG: branchTgr,
        DEPOK: branchDepok,
        BINTARO: branchBintaro,
    };
    const roles = [
        { code: "FRONTLINER", name: "Frontliner", accessAllBranch: false },
        { code: "HEAD_STORE", name: "Head Store", accessAllBranch: false },
        { code: "HEAD_OPS", name: "Head Ops", accessAllBranch: true },
        { code: "ADMIN_FINANCE", name: "Admin Finance", accessAllBranch: true },
        { code: "HEAD_FINANCE", name: "Head Finance", accessAllBranch: true },
        { code: "QC", name: "QC", accessAllBranch: true },
        { code: "SUPER_ADMIN", name: "Master Admin", accessAllBranch: true },
        { code: "STAKEHOLDERS", name: "Stakeholders", accessAllBranch: true },
    ];
    for (const role of roles) {
        await prisma.role.upsert({
            where: { code: role.code },
            update: {
                name: role.name,
                accessAllBranch: role.accessAllBranch,
            },
            create: role,
        });
    }
    const roleMap = {
        MASTER: await prisma.role.findUnique({ where: { code: "SUPER_ADMIN" } }),
        HEAD_OPS: await prisma.role.findUnique({ where: { code: "HEAD_OPS" } }),
        ADMIN_FINANCE: await prisma.role.findUnique({ where: { code: "ADMIN_FINANCE" } }),
        HEAD_STORE: await prisma.role.findUnique({ where: { code: "HEAD_STORE" } }),
        FRONTLINER: await prisma.role.findUnique({ where: { code: "FRONTLINER" } }),
    };
    const users = [
        { name: "Fitra Filian", role: "MASTER", branch: "ALL", email: "fitra@superemas.id" },
        { name: "Farhan Fajarmukti", role: "MASTER", branch: "ALL", email: "farhan@superemas.id" },
        { name: "Julio Engelhard", role: "HEAD_OPS", branch: "ALL", email: "julio@superemas.id" },
        { name: "Fanny Dwi Maulidya", role: "ADMIN_FINANCE", branch: "ALL", email: "fanny.dwi@superemas.id" },
        { name: "Ananda Febiani", role: "ADMIN_FINANCE", branch: "ALL", email: "ananda.febiani@superemas.id" },
        { name: "Faradila Nayafa Albi", role: "HEAD_STORE", branch: "TANGERANG", email: "faradila.nayafa@superemas.id" },
        { name: "Sindy Amelia", role: "HEAD_STORE", branch: "TANGERANG", email: "sindy.amelia@superemas.id" },
        { name: "Tenggo Vanhegar", role: "FRONTLINER", branch: "TANGERANG", email: "tenggo.vanhegar@superemas.id" },
        { name: "Umamsyah Mujidin", role: "FRONTLINER", branch: "TANGERANG", email: "umamsyah.mujidin@superemas.id" },
        { name: "Adji Putra Nugroho", role: "HEAD_STORE", branch: "DEPOK", email: "adji.putra@superemas.id" },
        { name: "Thomas Ardho", role: "HEAD_STORE", branch: "DEPOK", email: "thomas.ardho@superemas.id" },
        { name: "Syah Rani", role: "FRONTLINER", branch: "DEPOK", email: "syah.rani@superemas.id" },
        { name: "Alin Arimbi", role: "FRONTLINER", branch: "DEPOK", email: "alin.arimbi@superemas.id" },
        { name: "Ananda Pratama Putra", role: "HEAD_STORE", branch: "BINTARO", email: "ananda.pratama@superemas.id" },
        { name: "Catherine Rara Lintin", role: "HEAD_STORE", branch: "BINTARO", email: "catherine.rara@superemas.id" },
        { name: "Dani Prihat Bren", role: "FRONTLINER", branch: "BINTARO", email: "dani.prihat@superemas.id" },
    ];
    for (const u of users) {
        const [firstName, ...rest] = u.name.split(" ");
        const lastName = rest.join(" ");
        const role = roleMap[u.role];
        const user = await prisma.user.upsert({
            where: { email: u.email },
            update: {},
            create: {
                email: u.email,
                hash: password,
                firstName,
                lastName,
                roleId: role.id,
            },
        });
        if (u.branch !== "ALL") {
            const branch = branchMap[u.branch];
            await prisma.userBranch.upsert({
                where: {
                    userId_branchId: {
                        userId: user.id,
                        branchId: branch.id,
                    },
                },
                update: {},
                create: {
                    userId: user.id,
                    branchId: branch.id,
                },
            });
        }
    }
    function generateCustomerPrefix(name) {
        const clean = name.replace(/[^a-zA-Z]/g, "").toUpperCase();
        return clean.slice(0, 3).padEnd(3, "X");
    }
    function generateCustomerCode(name, index) {
        const prefix = generateCustomerPrefix(name);
        const number = String(index + 1).padStart(3, "0");
        return `${prefix}${number}`;
    }
    const customers = [
        {
            fullName: "Fitra",
            phoneNumber: "0811111111",
            address: "Denpasar",
            ktpNumber: "5101010101010001",
        },
        {
            fullName: "Ichsan",
            phoneNumber: "0822222222",
            address: "Jakarta",
            ktpNumber: "3171010101010002",
        },
        {
            fullName: "Farhan",
            phoneNumber: "0833333333",
            address: "Bandung",
            ktpNumber: "3271010101010003",
        },
    ];
    for (let i = 0; i < customers.length; i++) {
        const c = customers[i];
        const code = generateCustomerCode(c.fullName, i);
        await prisma.customer.upsert({
            where: { code },
            update: {},
            create: {
                code,
                fullName: c.fullName,
                phoneNumber: c.phoneNumber,
                ktpNumber: c.ktpNumber,
                address: c.address,
            },
        });
    }
    const lmNonAntam = await prisma.itemType.upsert({
        where: { code: 'GLB' },
        update: {},
        create: {
            code: 'GLB',
            name: 'Gold Bar',
            desc: 'Gold bar non-Antam',
            status: enums_1.SettingStatus.ACTIVE,
        },
    });
    const jewelry = await prisma.itemType.upsert({
        where: { code: 'JWL' },
        update: {},
        create: {
            code: 'JWL',
            name: 'Jewelry',
            desc: 'Jewelry items',
            status: enums_1.SettingStatus.ACTIVE,
        },
    });
    console.log('✅ Item types seeded:', { lmNonAntam, jewelry });
    const cancellationHeader = await prisma.headerSetting.upsert({
        where: { code: 'CANCELLATION_REASON' },
        update: {},
        create: {
            code: 'CANCELLATION_REASON',
            description: 'Alasan Batal Transaksi',
            status: 'ACTIVE',
        },
    });
    const cancellationReasons = [
        { code: 'CR001', name: 'Harga masih terlalu murah' },
        { code: 'CR002', name: 'Tidak bersedia untuk emasnya dipotong' },
        { code: 'CR003', name: 'Mau cek dulu aja' },
        { code: 'CR004', name: 'Pelayanan kurang baik' },
        { code: 'CR005', name: 'Potongan berat terlalu besar' },
        { code: 'CR006', name: 'Kadar tidak sesuai estimasi customer' },
    ];
    for (const item of cancellationReasons) {
        await prisma.detailSetting.upsert({
            where: { headerId_code: { headerId: cancellationHeader.id, code: item.code } },
            update: {},
            create: {
                headerId: cancellationHeader.id,
                code: item.code,
                name: item.name,
                status: 'ACTIVE',
                beginEffective: now,
            },
        });
    }
    console.log('✅ Cancellation reasons seeded');
    const referralHeader = await prisma.headerSetting.upsert({
        where: { code: 'REFERRAL_SOURCE' },
        update: {},
        create: {
            code: 'REFERRAL_SOURCE',
            description: 'Dari mana tau Super Emas?',
            status: 'ACTIVE',
        },
    });
    const referralSources = [
        { code: 'RS001', name: 'Tiktok' },
        { code: 'RS002', name: 'Facebook' },
        { code: 'RS003', name: 'Instagram Super Emas' },
        { code: 'RS004', name: 'Instagram Richard Theodore' },
        { code: 'RS005', name: 'Instagram David Noah' },
        { code: 'RS006', name: 'Iklan Instagram' },
        { code: 'RS007', name: 'Teman/Kerabat' },
        { code: 'RS008', name: 'Lihat dari jalanan' },
    ];
    for (const item of referralSources) {
        await prisma.detailSetting.upsert({
            where: { headerId_code: { headerId: referralHeader.id, code: item.code } },
            update: {},
            create: {
                headerId: referralHeader.id,
                code: item.code,
                name: item.name,
                status: 'ACTIVE',
                beginEffective: now,
            },
        });
    }
    const banks = [
        { code: 'BRI', name: 'Bank Rakyat Indonesia' },
        { code: 'MANDIRI', name: 'Bank Mandiri' },
        { code: 'BNI', name: 'Bank Negara Indonesia' },
        { code: 'BTN', name: 'Bank Tabungan Negara' },
        { code: 'DANAMON', name: 'Bank Danamon Indonesia' },
        { code: 'PERMATA', name: 'Bank Permata' },
        { code: 'BCA', name: 'Bank Central Asia' },
        { code: 'MAYBANK', name: 'Bank Maybank Indonesia' },
        { code: 'PANIN', name: 'Pan Indonesia Bank' },
        { code: 'CIMB', name: 'Bank CIMB Niaga' },
        { code: 'UOB', name: 'Bank UOB Indonesia' },
        { code: 'OCBC', name: 'Bank OCBC NISP' },
        { code: 'ARTHA_GRAHA', name: 'Bank Artha Graha Internasional' },
        { code: 'BUMI_ARTA', name: 'Bank Bumi Arta' },
        { code: 'HSBC', name: 'Bank HSBC Indonesia' },
        { code: 'JTRUST', name: 'Bank JTrust Indonesia' },
        { code: 'MAYAPADA', name: 'Bank Mayapada Internasional' },
        { code: 'SINARMAS', name: 'Bank Sinarmas' },
        { code: 'MEGA', name: 'Bank Mega' },
        { code: 'ICBC', name: 'Bank ICBC Indonesia' },
        { code: 'QNB', name: 'Bank QNB Indonesia' },
        { code: 'KB_BUKOPIN', name: 'Bank KB Bukopin' },
        { code: 'SBI', name: 'Bank SBI Indonesia' },
        { code: 'MIZUHO', name: 'Bank Mizuho Indonesia' },
        { code: 'BNP_PARIBAS', name: 'Bank BNP Paribas Indonesia' },
        { code: 'CTBC', name: 'Bank CTBC Indonesia' },
        { code: 'DBS', name: 'Bank DBS Indonesia' },
        { code: 'RESONA', name: 'Bank Resona Perdania' },
        { code: 'ANZ', name: 'Bank ANZ Indonesia' },
        { code: 'BTPN', name: 'Bank BTPN' },
        { code: 'BTPN_SYARIAH', name: 'Bank BTPN Syariah' },
        { code: 'BSI', name: 'Bank Syariah Indonesia' },
        { code: 'MUAMALAT', name: 'Bank Muamalat Indonesia' },
        { code: 'VICTORIA_SYR', name: 'Bank Victoria Syariah' },
        { code: 'ALADIN', name: 'Bank Aladin Syariah' },
        { code: 'JAGO', name: 'Bank Jago' },
        { code: 'NEO', name: 'Bank Neo Commerce' },
        { code: 'AMAR', name: 'Bank Amar Indonesia' },
        { code: 'VICTORIA', name: 'Bank Victoria International' },
        { code: 'CAPITAL', name: 'Bank Capital Indonesia' },
        { code: 'INDEX', name: 'Bank Index Selindo' },
        { code: 'HARDA', name: 'Bank Harda Internasional' },
        { code: 'NOBU', name: 'Bank Nationalnobu' },
        { code: 'WOORI', name: 'Bank Woori Saudara Indonesia 1906' },
        { code: 'KEB_HANA', name: 'Bank KEB Hana Indonesia' },
        { code: 'CCB', name: 'Bank China Construction Bank Indonesia' },
        { code: 'COMMONWEALTH', name: 'Bank Commonwealth' },
        { code: 'INA_PERDANA', name: 'Bank Ina Perdana' },
        { code: 'SAHABAT', name: 'Bank Sahabat Sampoerna' },
        { code: 'MULTIARTA', name: 'Bank Multiarta Sentosa' },
        { code: 'GOPAY', name: 'E-Wallet - Gopay' },
        { code: 'DANA', name: 'E-Wallet - Dana' },
        { code: 'SHOPEEPAY', name: 'E-Wallet - Shopee Pay' },
        { code: 'OVO', name: 'E-Wallet - OVO' },
        { code: 'LINKAJA', name: 'E-Wallet - LinkAja' },
    ];
    for (const bank of banks) {
        await prisma.bank.upsert({
            where: { code: bank.code },
            update: { name: bank.name },
            create: { ...bank, status: 'ACTIVE' },
        });
    }
    console.log("✅ Seeding finished.");
}
main()
    .catch((e) => {
    console.error(e);
    process.exit(1);
})
    .finally(async () => {
    await prisma.$disconnect();
});
//# sourceMappingURL=seed.js.map