using System;

namespace Plugin.MongoLogs.Utils
{
    public class MongoLogsConfiguration
    {
        public MongoLogsConfiguration(string host, short port, string dbName, string username, string password)
        {
            Host = host;
            Port = port;
            DbName = dbName;
            Username = username;
            Password = password;
        }

        public string Host { get; }
        public short Port { get; }
        public string DbName { get; }
        public string Username { get; }
        public string Password { get; }

        public static MongoLogsConfiguration FromEnv()
        {
            string username = Environment.GetEnvironmentVariable("WINGSEMU_MONGO_USERNAME")
                              ?? throw new InvalidOperationException("WINGSEMU_MONGO_USERNAME environment variable is required");
            string password = Environment.GetEnvironmentVariable("WINGSEMU_MONGO_PWD")
                              ?? throw new InvalidOperationException("WINGSEMU_MONGO_PWD environment variable is required");

            return new MongoLogsConfiguration(
                Environment.GetEnvironmentVariable("WINGSEMU_MONGO_HOST") ?? "127.0.0.1",
                short.Parse(Environment.GetEnvironmentVariable("WINGSEMU_MONGO_PORT") ?? "27017"),
                Environment.GetEnvironmentVariable("WINGSEMU_MONGO_DB") ?? "wingsemu_logs",
                username,
                password
            );
        }

        public override string ToString() => $"mongodb://***:***@{Host}:{Port}";
    }
}