using HRD.WebApi.DAL;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Data.Common;

namespace HRD.WebApi.Helpers
{
    public static class ServerInfoHelper
    {
        public static ServerInfo GetServerInfo(WebApiBaseContext context)
        {
            ServerInfo serverInfo = new ServerInfo();
            {
                serverInfo.DatabaseName = context?.Database?.GetDbConnection()?.Database; ;
                serverInfo.DatabaseServer = context?.Database?.GetDbConnection()?.DataSource;
                serverInfo.Message = "Online";
                using DbConnection connection = context.Database.GetDbConnection();
                try
                {
                    connection.Open();
                    serverInfo.DatabaseStatus = "Online";
                    serverInfo.Status = StatusCodes.Status200OK;
                }
                catch (Exception ex)
                {
                    serverInfo.DatabaseStatus = "Offline";
                    serverInfo.Status = StatusCodes.Status500InternalServerError;
                    serverInfo.Message = ex.Message;
                }
            }
            serverInfo.Server = Environment.MachineName;
            serverInfo.Version = WebApiConfig.AssemblyVersion;
            serverInfo.IsLive = WebApiConfig.IsLive;

            serverInfo.ClientVersion = WebApiConfig.ClientVersion;
            return serverInfo;
        }
    }
}