import { DATASQUIREL_LoggedInUser, UserType } from "../../../types";
import path from "path";

type Param = {
    user?: DATASQUIREL_LoggedInUser | UserType;
    userId?: string | number | null;
};
export default function grabDirNames(param?: Param) {
    const appDir = process.env.DSQL_APP_DIR;
    const schemasDir = process.env.DSQL_DB_SCHEMA_DIR;
    const tempDirName = ".tmp";

    if (!appDir)
        throw new Error("Please provide the `DSQL_APP_DIR` env variable.");

    if (!schemasDir)
        throw new Error(
            "Please provide the `DSQL_DB_SCHEMA_DIR` env variable."
        );

    const pakageSharedDir = path.join(appDir, `package-shared`);

    const mainDbTypeDefFile = path.join(pakageSharedDir, `types/dsql.ts`);
    const mainShemaJSONFilePath = path.join(schemasDir, `main.json`);
    const defaultTableFieldsJSONFilePath = path.join(
        pakageSharedDir,
        `data/defaultFields.json`
    );

    const usersSchemaDir = path.join(schemasDir, `users`);

    const userDirPath = param?.user?.id
        ? path.join(usersSchemaDir, `user-${param.user.id}`)
        : param?.userId
        ? path.join(usersSchemaDir, `user-${param.userId}`)
        : undefined;
    const userSchemaMainJSONFilePath = userDirPath
        ? path.join(userDirPath, `main.json`)
        : undefined;
    const userPrivateMediaDir = userDirPath
        ? path.join(userDirPath, `media`)
        : undefined;
    const userPrivateExportsDir = userDirPath
        ? path.join(userDirPath, `export`)
        : undefined;
    const userPrivateSQLExportsDir = userPrivateExportsDir
        ? path.join(userPrivateExportsDir, `sql`)
        : undefined;
    const userPrivateTempSQLExportsDir = userPrivateSQLExportsDir
        ? path.join(userPrivateSQLExportsDir, tempDirName)
        : undefined;
    const userPrivateTempJSONSchemaFilePath = userPrivateTempSQLExportsDir
        ? path.join(userPrivateTempSQLExportsDir, `schema.json`)
        : undefined;
    const userPrivateDbExportZipFileName = `db-export.zip`;
    const userPrivateDbExportZipFilePath = userPrivateSQLExportsDir
        ? path.join(userPrivateSQLExportsDir, userPrivateDbExportZipFileName)
        : undefined;

    const userPrivateDbImportZipFileName = `db-export.zip`;
    const userPrivateDbImportZipFilePath = userPrivateSQLExportsDir
        ? path.join(userPrivateSQLExportsDir, userPrivateDbImportZipFileName)
        : undefined;

    const dbNginxLoadBalancerConfigFile = path.join(
        appDir,
        "docker/mariadb/load-balancer/config/template/nginx.conf"
    );

    return {
        schemasDir,
        userDirPath,
        mainShemaJSONFilePath,
        mainDbTypeDefFile,
        tempDirName,
        defaultTableFieldsJSONFilePath,
        usersSchemaDir,
        userSchemaMainJSONFilePath,
        userPrivateMediaDir,
        userPrivateExportsDir,
        userPrivateSQLExportsDir,
        userPrivateTempSQLExportsDir,
        userPrivateTempJSONSchemaFilePath,
        userPrivateDbExportZipFileName,
        userPrivateDbExportZipFilePath,
        userPrivateDbImportZipFileName,
        userPrivateDbImportZipFilePath,
        dbNginxLoadBalancerConfigFile,
    };
}