import { z } from 'zod';

const sendSchema = z.object({
    forum: z.string(),
    text: z.string(),
})
.strict()

const getSchema = z.object({
    forum: z.string(),
    by: z.string(),
    authorId: z.number()
})
.strict()

const deleteSchema = z.object({
    forum: z.string(),
    id: z.number()
})
.strict()

const putSchema = z.object({
    forum: z.string(),
    id: z.number(),
    text: z.string()
})
.strict()

export default class ForumHandler {
    static async handleSend(msg, ws) {
        try {
            let postId = await global.db.posts.add(msg.text, msg.forum, ws.userEmail)
            let newPost = global.db.posts.getByID(postId)
            if (newPost) {
                global.Socket.broadcast({
                    event: "new-post", 
                    app: "FORUM", 
                    forum: msg.forum, 
                    msg: newPost
               })
               return {success: true}
            } else {
                return {success: false}
            }
        } catch(e) {
            console.error(e)
        }
    }

    static handleGet(msg) {
        let data = global.db.posts.get(msg.forum, msg.by, msg.authorId)
        return data
    }

    static async handleDelete(msg, ws) {
        try {
            await global.db.posts.delete(msg.id, msg.forum, ws.userEmail)
            global.Socket.broadcast({
                event: "deleted-post", 
                app: "FORUM", 
                forum: msg.forum, 
                msg: msg.id
            })
            return {success: true}
        } catch(e) {
            console.error(e)
            return {success: false}
        }
    }

    static async handlePut(msg, ws) {
        try {
            let editedPost = await global.db.posts.edit(msg.id, msg.text, ws.userEmail)
            console.log(editedPost)
            if (editedPost) {
                global.Socket.broadcast({
                    event: "edited-post", 
                    app: "FORUM", 
                    forum: msg.forum, 
                    msg: editedPost
                })
            }
            return {success: true}
        } catch(e) {
            console.error(e)
            return {success: false}
        }
    }

    static async handle(operation, msg, ws) {
        switch(operation) {
            case "SEND":
                if(!sendSchema.safeParse(msg).success) throw new Error("Incorrectly formatted Forum ws message!")
                return await this.handleSend(msg, ws)
            case "GET":
                if(!getSchema.safeParse(msg).success) throw new Error("Incorrectly formatted Forum ws message!")
                return this.handleGet(msg)
            case "DELETE":
                if(!deleteSchema.safeParse(msg).success) throw new Error("Incorrectly formatted Forum ws message!")
                return this.handleDelete(msg, ws)
            case "PUT":
                if (!putSchema.safeParse(msg).success) throw new Error("Incorrectly formatted ws message!")
                return this.handlePut(msg, ws)
        }

    }
}