// @ts-check

import httpsRequest from "../package-shared/functions/backend/httpsRequest";
import cron from "cron";
import path from "path";
import { dbSchemaExecDbUpdate } from "../functions/backend/dbSchemaExec";
import dbHandler from "../package-shared/functions/backend/dbHandler";

export default async function () {
    ////////////////////////////////////////
    ////////////////////////////////////////
    ////////////////////////////////////////

    new cron.CronJob(
        "20 * * * * *",
        async () => {
            /** @type {import("@/package-shared/types").DSQL_MYSQL_user_databases_Type[] | null} */ // @ts-ignore
            const remoteConnectedDbs:
                | import("@/package-shared/types").DSQL_MYSQL_user_databases_Type[]
                | null = await dbHandler(
                `SELECT * FROM user_databases WHERE remote_connected = 1`
            );

            if (remoteConnectedDbs?.[0]) {
                for (let i = 0; i < remoteConnectedDbs.length; i++) {
                    try {
                        const database = remoteConnectedDbs[i];

                        const {
                            user_id,
                            remote_connection_host,
                            remote_db_full_name,
                            remote_connection_key,
                            remote_connection_type,
                            db_full_name,
                        } = database;

                        if (database.remote_connection_type == "pull") {
                            const dbSchema = (await httpsRequest({
                                url: remote_connection_host,
                                headers: {
                                    Authorization: remote_connection_key,
                                    "Content-Type": "application/json",
                                },
                                method: "GET",
                                path: `/api/query/get-schema?database=${remote_db_full_name?.replace(
                                    /^datasquirel_user_\d+_/i,
                                    ""
                                )}`,
                            })) as any;

                            /** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType | null | undefined} */
                            const remoteDbSchemaData:
                                | import("@/package-shared/types").DSQL_DatabaseSchemaType
                                | null
                                | undefined = JSON.parse(dbSchema).payload;

                            if (remoteDbSchemaData) {
                                const update = dbSchemaExecDbUpdate({
                                    dbSchema: remoteDbSchemaData,
                                    database: database,
                                    userId: user_id,
                                });

                                console.log(update);
                            }
                        }
                    } catch (/** @type {any} */ error: any) {
                        console.log(error.message);
                    }
                }
            }
        },
        null,
        true,
        "America/Los_Angeles"
    );

    ////////////////////////////////////////
    ////////////////////////////////////////
    ////////////////////////////////////////
}
