"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) {
      Order.belongsTo(models.User, {
        foreignKey: "user_id",
        as: "user",
      });

      Order.belongsTo(models.Produk, {
        foreignKey: "produk_id",
        as: "produk",
      });
    }
  }

  Order.init(
    {
      user_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "user",
          key: "id",
        },
      },
      produk_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "produk",
          key: "id",
        },
      },

      jumlah: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      total_harga: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      status: {
        type: DataTypes.ENUM("pending", "selesai"),
        allowNull: false,
        defaultValue: "pending",
      },
      tanggal_pesan: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
      },
    },
    {
      sequelize,
      modelName: "Order",
      tableName: "order",
    },
  );

  return Order;
};
