const { resSukses, resError } = require("../../payload/payload.js");

const {
       allPayment,
       byIdPayment,
       createPayment,
       updatePayment,
       deletePayment
} = require("./service.js");


const semuaPayment = async (req, res) => {
    try {
        const data = await allPayment();
        return resSukses(res, 200, "success", "Data payment", data);
    } catch (error) {
        return resError(res, 500, "error", error.message);
    }
};

const paymentId = async (req, res) => {
    try {
        const { id } = req.params;

        const data = await byIdPayment(id);

        if (!data) {
            return resError(res, 404, "error", "Data payment tidak ditemukan", null);
        }

        return resSukses(res, 200, "success", "Detail payment", data);
    } catch (error) {
        return resError(res, 500, "error", error.message);
    }
};


const buatPayment = async (req, res) => {
    try {
        const { metode_pembayaran, status_pembayaran, order_id } = req.body;

        const body = {metode_pembayaran, status_pembayaran, order_id};

        const result = await createPayment(body);

        return resSukses(res, 201, "success", "Payment berhasil ditambahkan", result);
    } catch (error) {
        return resError(res, 500, "error", error.message);
    }
};


const editPayment = async (req, res) => {
    try {
        const { id } = req.params;
        const { metode_pembayaran, status_pembayaran} = req.body;

        const data = await byIdPayment(id);

        if (!data) {
            return resError(res, 404, "error", "Data payment tidak ditemukan", null);
        }

        const body = {metode_pembayaran, status_pembayaran};

        const result = await updatePayment(id, body);

        return resSukses(res, 200, "success", "Data payment berhasil diupdate", result);
    } catch (error) {
        return resError(res, 500, "error", error.message);
    }
};


const hapusPayment = async (req, res) => {
    try {
        const { id } = req.params;

        const data = await byIdPayment(id);

        if (!data) {
            return resError(res, 404, "error", "Data payment tidak ditemukan", null);
        }

        await deletePayment(id);

        return resSukses(res, 200, "success", "Data payment berhasil dihapus", null);
    } catch (error) {
        return resError(res, 500, "error", error.message);
    }
};

module.exports = {
    semuaPayment,
    paymentId,
    buatPayment,
    editPayment,
    hapusPayment
};