const { Sequelize, DataTypes } = require('sequelize');
const { sequelize } = require('./model');

// const sequelize = new Sequelize('app_db', 'db_user', 'db_user_pass', {
//   host: 'mysql',
//   port: 3306,
//   dialect: 'mysql',
// });

const Product = sequelize.define(
  'Products',
  {
    pid: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, unique: true },
    cid: { type: DataTypes.INTEGER, allowNull: false },
    name: { type: DataTypes.STRING, allowNull: false },
    description: { type: DataTypes.STRING, allowNull: false },
    price: { type: DataTypes.DECIMAL, allowNull: false },
    product_image: { type: DataTypes.STRING, allowNull: false },
  },
  { timestamps: false },
);

(async () => {
  try {
    await sequelize.authenticate();
    console.log('Connection has been established successfully.');

    // create table
    await sequelize.sync();

    let products = [
      {
        cid: 2,
        name: 'Milk',
        description: 'A nutritious beverage made from the milk of cows, goats, or other mammals.',
        price: 5.5,
        product_image: 'DAIRY_1',
      },
      {
        cid: 2,
        name: 'Cheese',
        description: 'A versatile dairy product made from milk, available in a variety of flavors and textures.',
        price: 6.5,
        product_image: 'DAIRY_2',
      },
      {
        cid: 2,
        name: 'Yogurt',
        description: 'A creamy, fermented dairy product that comes in many flavors and styles.',
        price: 8.5,
        product_image: 'DAIRY_3',
      },
      {
        cid: 2,
        name: 'Butter',
        description: 'A dairy product made by churning cream, used for cooking and baking.',
        price: 4.95,
        product_image: 'DAIRY_4',
      },
    ];

    for (var i = 0; i < products.length; i++) {
      await Product.create(products[i]);
    }

    const categories = await Product.findAll();

    // await sequelize.close();
  } catch (error) {
    console.error('Unable to connect to the database:', error);
  }
})();
