using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using AyaNova.Util;
using AyaNova.Biz;

namespace AyaNova.Api.Controllers
{
    /// <summary>
    /// Meta controller class
    /// </summary>
    [ApiVersion("8.0")]
    [Route("api/v{version:apiVersion}/")]
    public class ApiMetaController : Controller
    {

        private readonly ILogger<ApiMetaController> _log;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        public ApiMetaController(ILogger<ApiMetaController> logger)
        {
            _log = logger;
        }

        /// <summary>
        /// AyaNova API documentation and manual
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult Index()
        {

            var resp = $@"<html lang=""en"">
            <head>
            <meta charset=""utf-8"">
            <meta name=""viewport"" content=""width=device-width, initial-scale=1, shrink-to-fit=no"">
            <title>AyaNova server</title>
            </head>
            <body >
            <div style=""text-align: center;""> 
            <div style=""display: inline-block;text-align:left;"">          
            <h1>{AyaNovaVersion.FullNameAndVersion}</h1> 
            <a href=""/"" target=""_blank"">AyaNova App</a><br/><br/>             
            <a href=""/docs"" target=""_blank"">AyaNova manual</a><br/><br/>                    
            <a href=""/api-docs"" target=""_blank"">API explorer</a><br/><br/>         
            <a href=""mailto:support@ayanova.com"">Email AyaNova support</a><br/><br/>  
            <h4>{LocaleBiz.GetDefaultLocalizedText("HelpLicense").Result}</h4>             
            <pre>{AyaNova.Core.License.LicenseInfo}</pre> 
            <h4>Schema version</h4>
            <pre>{AySchema.currentSchema.ToString()}</pre>
            <h4>Server time</h4>
            <pre>{DateUtil.ServerDateTimeString(System.DateTime.UtcNow)}</pre>             
            <pre>{TimeZoneInfo.Local.Id}</pre>           
            <h4>Server logs</h4>
            <pre>{ServerBootConfig.AYANOVA_LOG_PATH}</pre>                             
            </div>  
            </div>            
            </body>
            </html>";


            return new ContentResult
            {
                ContentType = "text/html",
                StatusCode = 200,
                Content = resp
            };

        }

        /// <summary>    
        /// Get API server info for general display
        /// 
        /// Required roles: Any
        /// 
        /// </summary>
        /// <returns>API server info</returns>    
        [HttpGet("ServerInfo")]
        public ActionResult ServerInfo()
        {
            return Ok(new
            {
                data = new
                {
                    ServerVersion = AyaNovaVersion.FullNameAndVersion,
                    DBSchemaVersion = AySchema.currentSchema,
                    ServerLocalTime = DateUtil.ServerDateTimeString(System.DateTime.UtcNow),
                    ServerTimeZone = TimeZoneInfo.Local.Id,
                    License = AyaNova.Core.License.LicenseInfoAsJson
                }
            });
        }

#if (DEBUG)
        /// <summary>
        /// Get build mode of server, used for automated testing purposes
        /// 
        /// Required roles: Any
        /// 
        /// </summary>
        /// <returns>"DEBUG" or "RELEASE"</returns>    
        [HttpGet("BuildMode")]
        public ActionResult BuildMode()
        {
            return Ok(new { data = new { BuildMode = "DEBUG" } });
        }
#else
 /// <summary>
        /// Get build mode of server, used for automated testing purposes
        /// 
        /// Required roles: Any
        /// 
        /// </summary>
        /// <returns>"DEBUG" or "RELEASE"</returns>    
        [HttpGet("BuildMode")]
        public ActionResult BuildMode()
        {
             return Ok(new { data = new { BuildMode = "RELEASE" } });
        }
#endif



    }
}