import serverError from "./serverError";
import fs from "fs";
import path from "path";
import { DSQL_DatabaseSchemaType } from "../../types";

type Param = {
    userId: string | number;
    schemaData: DSQL_DatabaseSchemaType[];
};

/**
 * # Set User Schema Data
 */
export default function setUserSchemaData({
    userId,
    schemaData,
}: Param): boolean {
    try {
        const userSchemaFilePath = path.resolve(
            process.cwd(),
            `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${userId}/main.json`
        );
        fs.writeFileSync(
            userSchemaFilePath,
            JSON.stringify(schemaData),
            "utf8"
        );

        return true;
    } catch (error: any) {
        serverError({
            component: "/functions/backend/setUserSchemaData",
            message: error.message,
        });

        return false;
    }
}
