const { Order, User, Produk } = require("../Database/models");

const getAllOrder = async () => {
  return await Order.findAll({
    include: [
      {
        model: User,
        as: "user",
        attributes: ["id", "nama", "email"],
      },
      {
        model: Produk,
        as: "produk",
        attributes: ["id", "nama_produk", "harga"],
      },
    ],
  });
};

const createOrderService = async ({
  user_id,
  produk_id,
  jumlah,
  total_harga,
  status,
  tanggal_pesan,
}) => {
  const order = await Order.create({
    user_id,
    produk_id,
    jumlah,
    total_harga,
    status,
    tanggal_pesan,
  });

  return order;
};
const getOrderWithUser = async (orderId) => {
  return await Order.findByPk(orderId, {
    include: [{ model: User, as: "user" }],
  });
};

const deleteOrder = async (id) => {
  return await Order.destroy({ where: { id } });
};

module.exports = {
  deleteOrder,
  getAllOrder,
  createOrderService,
  getOrderWithUser,
};
