'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Order extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      this.belongsTo(models.User, {
        foreignKey: "user_id",
        as: "user",
      });

      this.belongsTo(models.Address, {
        foreignKey: "address_id",
        as: "address",
      });

      this.hasMany(models.OrderItems, {
        foreignKey: "order_id",
        as: "order_items",
      });

      this.hasOne(models.Payment, {
        foreignKey: "order_id",
        as: "payment",
      });
    }
  }
  Order.init({
      total_harga: {
        type: DataTypes.INTEGER,
        allowNull: false
      },
      status: {
        type: DataTypes.ENUM("pending", "diproses", "selesai", "dibatalkan"),
        defaultValue: "pending",
        allowNull: false
      },
      user_id: {
        type: DataTypes.INTEGER,
        allowNull:false,
        references :{
          model:"user",
          key:"id"
        }
      },
      address_id: {
        type: DataTypes.INTEGER,
        allowNull:false,
        references :{
          model:"address",
          key:"id"
        }
      },
  }, {
    sequelize,
    modelName: 'Order',
    tableName:"orders"
  });
  return Order;
};