import OrderedObject from "./OrderedObject.js"
const { z } = require("zod")

export default class Payments extends OrderedObject {

    schema = z.object({
        id: z.number(),
        name: z.string(),
        email: z.string(),
        time: z.string(),
        amount: z.number(),
        product: z.string(),
    })

    save(payment) {
        let id = `PAYMENT-${payment.id}`
        let result = this.schema.safeParse(payment)
        if(result.success) {
            try {
                super.add(id, payment)
            } catch(e) {
                console.error(e)
                throw e
            }
        } else {
            console.error(result.error)
            throw new global.ServerError(400, "Invalid Member Data!");
        }
    }

    add(paymentObj) {
        let toSave = {
            id: this.entries.length+1,
            ...paymentObj
        }
        this.save(toSave)
    }

    get(id) {
        return this.entries[this.ids[`PAYMENT-${id}`]]
    }
}