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 },
    product_image: { 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: 5,
        name: 'Frozen Vegetables',
        description: 'A variety of vegetables, such as peas, corn, and broccoli, frozen at peak freshness.',
        price: 5.5,
        product_image: 'FROZEN_1',
      },
      {
        cid: 5,
        name: 'Frozen Seafood',
        description: 'A variety of seafood, such as shrimp, fish, and scallops, frozen for freshness and convenience.',

        price: 6.5,
        product_image: 'FROZEN_2',
      },
      {
        cid: 5,
        name: 'Frozen Meals',
        description: 'Pre-made meals such as pizza, lasagna, and stir-fry, available for quick and easy dinners.',

        price: 8.5,
        product_image: 'FROZEN_3',
      },
      {
        cid: 5,
        name: 'Frozen Fruits',
        description: 'A variety of fruits, such as berries, mango, and pineapple, frozen at peak freshness.',

        price: 4.95,
        product_image: 'FROZEN_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);
  }
})();
