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 },
    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: 6,
        name: 'Pasta',
        description: 'A variety of pasta shapes and types, such as spaghetti, macaroni, and lasagna noodles.',
        price: 5.5,
        product_image: 'PANTRY_1',
      },
      {
        cid: 6,
        name: 'Rice',
        description: 'A variety of rice types, such as white rice, brown rice, and jasmine rice.',
        price: 6.5,
        product_image: 'PANTRY_2',
      },
      {
        cid: 6,
        name: 'Canned Beans',
        description: 'A variety of beans, such as black beans, kidney beans, and chickpeas, canned for convenience.',
        price: 8.5,
        product_image: 'PANTRY_3',
      },
      {
        cid: 6,
        name: 'Canned Tomatoes',
        description: 'A variety of canned tomatoes, such as diced tomatoes, crushed tomatoes, and tomato sauce.',
        price: 4.95,
        product_image: 'PANTRY_4',
      },
      {
        cid: 6,
        name: 'Canned Beans',
        description: 'A variety of beans, such as black beans, kidney beans, and chickpeas, canned for convenience.',
        price: 8.5,
        product_image: 'PANTRY_3',
      },
      {
        cid: 6,
        name: 'Canned Tomatoes',
        description: 'A variety of canned tomatoes, such as diced tomatoes, crushed tomatoes, and tomato sauce.',
        price: 4.95,
        product_image: 'PANTRY_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);
  }
})();
