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

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

    // create table
    await sequelize.sync();

    let products = [
      {
        cid: 1,
        name: 'French Baguette',
        description: 'A long, crusty loaf of French bread with a soft interior.',
        price: 5.5,
        product_image: 'BREAD1',
      },
      {
        cid: 1,
        name: 'Whole Wheat Bread',
        description: 'A nutritious bread made with whole wheat flour and grains.',
        price: 6.5,
        product_image: 'BREAD2',
      },
      {
        cid: 1,
        name: 'Croissants',
        description: 'Buttery, flaky pastries often enjoyed with coffee or tea.',
        price: 8.5,
        product_image: 'BREAD3',
      },
      {
        cid: 1,
        name: 'Donuts',
        description: 'Sweet, fried pastries with a variety of toppings and fillings.',
        price: 4.95,
        product_image: 'BREAD4',
      },
      {
        cid: 1,
        name: 'Whole Wheat Bread',
        description: 'A nutritious bread made with whole wheat flour and grains.',
        price: 6.5,
        product_image: 'BREAD2',
      },
      {
        cid: 1,
        name: 'Croissants',
        description: 'Buttery, flaky pastries often enjoyed with coffee or tea.',
        price: 8.5,
        product_image: 'BREAD3',
      },
    ];

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