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 },
    product_image: { type: DataTypes.STRING, allowNull: true, defaultValue: '' },
  },
  { timestamps: false },
);

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

    await Product.drop();

    await sequelize.sync();

    await sequelize.close();
  } catch (error) {
    console.error('Unable to connect to the database:', error);
  }
})();
