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

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

    // create table
    await sequelize.sync();

    let products = [
      {
        cid: 7,
        name: 'Organic Cold Brew Coffee',
        description: 'Rich cold brew coffee crafted with organic fair trade arabica beans.',
        price: 3.5,
        product_image: 'BREAD1',
      },
      {
        cid: 7,
        name: 'Strawberry Banana Smoothie',
        description: 'Blended smoothie combining fresh strawberries and bananas with yogurt.',
        price: 4.5,
        product_image: 'BREAD2',
      },
      {
        cid: 7,
        name: 'Matcha Green Tea Latte',
        description: 'Warm frothy latte infused with antioxcidan7-rich Japanese green tea powder.',
        price: 4.95,
        product_image: 'BREAD3',
      },
      {
        cid: 7,
        name: 'Iced Passionfruit Black Tea Lemonade',
        description: 'Refreshing summertime drink blending passionfruit, black tea, and lemonade.',
        price: 3.95,
        product_image: 'BREAD4',
      },
      {
        cid: 7,
        name: 'Espresso Con Panna',
        description: 'Double shot espresso topped with whipped cream.',
        price: 3.95,
        product_image: 'BREAD5',
      },
    ];

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