const { Voucher } = require('./models');
const sequelize = require('./config/database');

const seedVoucher = async () => {
    try {
        await sequelize.authenticate();
        console.log('Database connected...');

        // Check if voucher exists
        const existing = await Voucher.findOne({ where: { code: 'DISKON10' } });
        if (existing) {
            console.log('Voucher DISKON10 already exists.');
            return;
        }

        await Voucher.create({
            code: 'DISKON10',
            discount: 10,
            type: 'Percentage',
            minPurchase: 0,
            expiryDate: new Date('2025-12-31'),
            status: 'Active',
            description: 'Voucher Diskon 10%'
        });

        console.log('Voucher DISKON10 created successfully!');
    } catch (error) {
        console.error('Error seeding voucher:', error);
    } finally {
        await sequelize.close();
    }
};

seedVoucher();
