import { parse } from "@hibas123/config";
import Logging from "@hibas123/nodelogging";
import * as dotenv from "dotenv";
import moment = require("moment");

export const refreshTokenValidTime = moment.duration(6, "month");

dotenv.config();

export interface DatabaseConfig {
   host: string;
   database: string;
   username?: string;
   password?: string;
}

export interface WebConfig {
   port: string;
   secure: "true" | "false" | undefined;
}

export interface CoreConfig {
   name: string;
   url: string;
   dev: boolean;
   secret: string;
}

export interface Config {
   core: CoreConfig;
   database: DatabaseConfig;
   web: WebConfig;
}

const config = (parse(
   {
      core: {
         dev: {
            default: false,
            type: Boolean,
         },
         name: {
            type: String,
            default: "Open Auth",
         },
         url: String,
         secret: {
            type: String,
            optional: false,
            description: "Cookie secret"
         }
      },
      database: {
         database: {
            type: String,
            default: "openauth",
         },
         host: {
            type: String,
            default: "localhost",
         },
         username: {
            type: String,
            optional: true,
         },
         password: {
            type: String,
            optional: true,
         },
      },
      web: {
         port: {
            type: Number,
            default: 3004,
         },
         secure: {
            type: Boolean,
            default: false,
         },
      },
   },
   "config.ini"
) as any) as Config;

if (process.env.DEV === "true") config.core.dev = true;
if (config.core.dev)
   Logging.warning(
      "DEV mode active. This can cause major performance issues, data loss and vulnerabilities! "
   );

export default config;
