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: true, defaultValue: '' },
  },
  { timestamps: false },
);

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

    // create table
    await sequelize.sync();

    let products = [
      {
        cid: 3,
        name: 'Chicken',
        description: 'A versatile and popular type of poultry, available in many cuts and preparations.',
        price: 9.95,
        product_image: 'MEAT1',
      },
      {
        cid: 3,
        name: 'Beef',
        description: 'A type of red meat that comes from cows, available in a variety of cuts and grades.',
        price: 13.5,
        product_image: 'MEAT2',
      },
      {
        cid: 3,
        name: 'Pork',
        description: 'A type of meat that comes from pigs, available in many cuts and preparations.',
        price: 27.8,
        product_image: 'MEAT3',
      },
      {
        cid: 3,
        name: 'Lamb',
        description: 'A type of red meat that comes from sheep, available in many cuts and preparations.',
        price: 25.95,
        product_image: 'MEAT4',
      },
    ];

    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);
  }
})();
