import sanitizeHtml from "sanitize-html";
import sanitizeHtmlOptions from "../html/sanitizeHtmlOptions";
import updateDbEntry from "./updateDbEntry";
import _ from "lodash";
import encrypt from "../../dsql/encrypt";
import connDbHandler from "../../../utils/db/conn-db-handler";
import checkIfIsMaster from "../../../utils/check-if-is-master";
import { DbContextsArray } from "./runQuery";

type Param = {
    dbContext?: (typeof DbContextsArray)[number];
    paradigm?: "Read Only" | "Full Access";
    dbFullName?: string;
    tableName: string;
    data: any;
    tableSchema?: import("../../../types").DSQL_TableSchemaType;
    duplicateColumnName?: string;
    duplicateColumnValue?: string;
    update?: boolean;
    encryptionKey?: string;
    encryptionSalt?: string;
    forceLocal?: boolean;
};

/**
 * Add a db Entry Function
 */
export default async function addDbEntry({
    dbContext,
    paradigm,
    dbFullName,
    tableName,
    data,
    tableSchema,
    duplicateColumnName,
    duplicateColumnValue,
    update,
    encryptionKey,
    encryptionSalt,
    forceLocal,
}: Param): Promise<any> {
    /**
     * Initialize variables
     */
    const isMaster = forceLocal
        ? true
        : checkIfIsMaster({ dbContext, dbFullName });

    const DB_CONN = isMaster
        ? global.DSQL_DB_CONN
        : global.DSQL_FULL_ACCESS_DB_CONN || global.DSQL_DB_CONN;
    const DB_RO_CONN = isMaster
        ? global.DSQL_DB_CONN
        : global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;

    ////////////////////////////////////////
    ////////////////////////////////////////
    ////////////////////////////////////////

    if (data?.["date_created_timestamp"]) delete data["date_created_timestamp"];
    if (data?.["date_updated_timestamp"]) delete data["date_updated_timestamp"];
    if (data?.["date_updated"]) delete data["date_updated"];
    if (data?.["date_updated_code"]) delete data["date_updated_code"];
    if (data?.["date_created"]) delete data["date_created"];
    if (data?.["date_created_code"]) delete data["date_created_code"];

    ////////////////////////////////////////
    ////////////////////////////////////////
    ////////////////////////////////////////

    if (duplicateColumnName && typeof duplicateColumnName === "string") {
        const checkDuplicateQuery = `SELECT * FROM ${
            isMaster ? "" : `\`${dbFullName}\`.`
        }\`${tableName}\` WHERE \`${duplicateColumnName}\`=?`;

        const duplicateValue = await connDbHandler(
            DB_RO_CONN,
            checkDuplicateQuery,
            [duplicateColumnValue]
        );

        if (duplicateValue?.[0] && !update) {
            return null;
        } else if (duplicateValue && duplicateValue[0] && update) {
            return await updateDbEntry({
                dbContext,
                dbFullName,
                tableName,
                data,
                tableSchema,
                encryptionKey,
                encryptionSalt,
                identifierColumnName: duplicateColumnName,
                identifierValue: duplicateColumnValue || "",
            });
        }
    }

    /**
     * Declare variables
     *
     * @description Declare "results" variable
     */
    const dataKeys = Object.keys(data);

    let insertKeysArray = [];
    let insertValuesArray = [];

    for (let i = 0; i < dataKeys.length; i++) {
        try {
            const dataKey = dataKeys[i];
            // @ts-ignore
            let value = data?.[dataKey];

            const targetFieldSchemaArray = tableSchema
                ? tableSchema?.fields?.filter(
                      (field) => field.fieldName == dataKey
                  )
                : null;
            const targetFieldSchema =
                targetFieldSchemaArray && targetFieldSchemaArray[0]
                    ? targetFieldSchemaArray[0]
                    : null;

            if (value == null || value == undefined) continue;

            if (
                targetFieldSchema?.dataType?.match(/int$/i) &&
                typeof value == "string" &&
                !value?.match(/./)
            )
                continue;

            if (targetFieldSchema?.encrypted) {
                value = encrypt({
                    data: value,
                    encryptionKey,
                    encryptionSalt,
                });
                console.log("DSQL: Encrypted value =>", value);
            }

            const htmlRegex = /<[^>]+>/g;

            if (targetFieldSchema?.richText || String(value).match(htmlRegex)) {
                value = sanitizeHtml(value, sanitizeHtmlOptions);
            }

            if (targetFieldSchema?.pattern) {
                const pattern = new RegExp(
                    targetFieldSchema.pattern,
                    targetFieldSchema.patternFlags || ""
                );
                if (!pattern.test(value)) {
                    console.log("DSQL: Pattern not matched =>", value);
                    value = "";
                }
            }

            insertKeysArray.push("`" + dataKey + "`");

            if (typeof value === "object") {
                value = JSON.stringify(value);
            }

            if (typeof value == "number") {
                insertValuesArray.push(String(value));
            } else {
                insertValuesArray.push(value);
            }
        } catch (error: any) {
            console.log("DSQL: Error in parsing data keys =>", error.message);
            continue;
        }
    }

    ////////////////////////////////////////

    if (!data?.["date_created"]) {
        insertKeysArray.push("`date_created`");
        insertValuesArray.push(Date());
    }

    if (!data?.["date_created_code"]) {
        insertKeysArray.push("`date_created_code`");
        insertValuesArray.push(Date.now());
    }

    ////////////////////////////////////////

    if (!data?.["date_updated"]) {
        insertKeysArray.push("`date_updated`");
        insertValuesArray.push(Date());
    }

    if (!data?.["date_updated_code"]) {
        insertKeysArray.push("`date_updated_code`");
        insertValuesArray.push(Date.now());
    }

    ////////////////////////////////////////

    const query = `INSERT INTO ${
        isMaster ? "" : `\`${dbFullName}\`.`
    }\`${tableName}\` (${insertKeysArray.join(",")}) VALUES (${insertValuesArray
        .map(() => "?")
        .join(",")})`;
    const queryValuesArray = insertValuesArray;

    const newInsert = await connDbHandler(DB_CONN, query, queryValuesArray);

    /**
     * Return statement
     */
    return newInsert;
}
