const fs = require('fs/promises');
const chalk = require('chalk');
const path = require('path');
import QuillDB from "../_/quilldb.js"
import Titles from "./model/Titles.js"
import Members from './model/Members.js'
import Tokens from './model/Tokens.js'
import Payments from "./model/Payments.js"
import Posts from "./model/Forum/Posts.js"
import Conversations from "./model/Messages/Conversations.js"
import Messages from "./model/Messages/Messages.js"

export default class Database {
    titles = new Titles()
    members = new Members()
    tokens = new Tokens()
    payments = new Payments()
    posts = new Posts()
    conversations = new Conversations()
    messages = new Messages()

    fromID = {
        "HY": this.titles,
        "MEMBER": this.members,
        "TOKEN": this.tokens,
        "PAYMENT": this.payments,
        "POST": this.posts,
        "CONVERSATION": this.conversations,
        "DM": this.messages
    }

    constructor() {
        this.loadData()
    }

    async loadData() {
        const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
        let dbJson;
        try {
            dbJson = JSON.parse(dbData);
        } catch {
            dbJson = []
        }
        let nodes = dbJson["nodes"];
        let entries = Object.entries(nodes)
        
        for(let i=0; i<entries.length; i++) {
            let entry = entries[i]
            let id = entry[0]; let node = entry[1];
            let type = id.split("-")[0]
            try {
                let collection = this.fromID[type]
                if(collection) {
                    collection.save(node, id)
                } else {
                    throw new Error("Type does not exist for node: ", id)
                }
            } catch(e) {
                throw e
            }

        }

        setInterval(() => {
            console.log("saving db")
            global.db.saveData()
        }, 5000)
    }

    async saveData() {
        let data = {
            "nodes": {

            },
            "edges": {

            }
        }
        let arrs = [
            this.titles.entries,
            this.members.entries,
            this.tokens.entries,
            this.posts.entries,
            this.conversations.entries,
            this.messages.entries,
            this.payments.entries,
        ]
        let ids = [
            Object.entries(this.titles.ids),
            Object.entries(this.members.ids),
            Object.entries(this.tokens.ids),
            Object.entries(this.posts.ids),
            Object.entries(this.conversations.ids),
            Object.entries(this.messages.ids),
            Object.entries(this.payments.ids),
        ]
        for(let i=0; i<arrs.length; i++) {
            let arr = arrs[i]
            for(let j=0; j<arr.length; j++) {
                data.nodes[ids[i][j][0]] = arr[j]
            }
        }

        let string = JSON.stringify(data, null, 4)
        await fs.writeFile(path.join(process.cwd(), 'db/db.json'), string, "utf8");
    }
}