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

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: 4,
        name: 'Sliced Meats',
        description: 'Deli meats such as ham, turkey, salami, and roast beef, sliced to order.',
        price: 6.95,
        product_image: 'DELI_1',
      },
      {
        cid: 4,
        name: 'Cheese',
        description: 'A variety of cheeses, such as cheddar, Swiss, and mozzarella, sliced to order.',
        price: 7.5,
        product_image: 'DELI_2',
      },
      {
        cid: 4,
        name: 'Prepared Salads',
        description: 'Salads such as pasta salad, potato salad, and coleslaw, made fresh in-store.',
        price: 8.5,
        product_image: 'DELI_3',
      },
      {
        cid: 4,
        name: 'Rotisserie Chicken',
        description: 'Whole chickens that have been seasoned and roasted in-store, ready to take home and serve.',
        price: 7.5,
        product_image: 'DELI_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);
  }
})();
