import { z } from 'zod';

export default class Payment  {
    prefix = "PAYMENT"
    indices = null

    constructor(indices) {
        this.indices = indices
    }

    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 = `${this.prefix}-${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[`${this.prefix}-${id}`]]
    }
}