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 Category = sequelize.define(
  'Categories',
  {
    cid: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, unique: true },
    name: { type: DataTypes.STRING, allowNull: false },
    description: { type: DataTypes.STRING, allowNull: false },
  },
  { timestamps: false },
);

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

    // create table
    await Category.drop();
    await sequelize.sync();

    let category;

    category = await Category.create({ name: 'Bakery', description: 'Bread, pastries, cakes, cookies' });
    category = await Category.create({ name: 'Dairy', description: 'Milk, cheese, eggs, yogurt, butter' });
    category = await Category.create({
      name: 'Meat & Seafood',
      description: 'Raw meat products, poultry, fish, shellfish',
    });
    category = await Category.create({
      name: 'Deli Counter',
      description: 'Prepared foods such as sandwiches, salads, and cold cuts',
    });
    category = await Category.create({
      name: 'Frozen Foods',
      description: 'Ice cream, frozen meals, pizza, vegetables, berries',
    });
    category = await Category.create({
      name: 'Pantry Staples',
      description: 'Pasta, rice, sauces, oils, vinegar, spices, canned goods',
    });

    const categories = await Category.findAll();
  } catch (error) {
    console.error('Unable to connect to the database:', error);
  }
})();
