using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using rockfishCore.Models;
using rockfishCore.Util;


namespace rockfishCore.Controllers
{
    [Produces("application/json")]
    [Route("api/ops")]
    [Authorize]
    public class OpsController : Controller
    {
        private readonly rockfishContext _context;

        public OpsController(rockfishContext context)
        {
            _context = context;
        }


        //status of ops
        [HttpGet("status/{service}")]
        public dtoOpsStatus GetOpsStatus([FromRoute] string service)
        {
            dtoOpsStatus Ret = new dtoOpsStatus();
            try
            {
                switch (service)
                {
                    case "MailMirror":
                        Ret.Status = RfMail.MailIsMirroringProperly();
                        {
                            DateTime? sslExpiry = OpsDiagnostics.SSLCertExpiryDate("https://mail.ayanova.com/");
                            if (sslExpiry != null)
                            {
                                Ret.ServiceCheckError += "Mail server SSL expires: " + sslExpiry.ToString();
                            }
                        }
                        break;
                    case "AyaNovaSite":
                        Ret.Status = OpsDiagnostics.CheckWebsite("https://www.ayanova.com/", "Ground Zero Tech-Works Inc.");
                        {
                            DateTime? sslExpiry = OpsDiagnostics.SSLCertExpiryDate("https://www.ayanova.com");
                            if (sslExpiry != null)
                            {
                                Ret.ServiceCheckError += "AyaNova.com SSL expires: " + sslExpiry.ToString();
                            }
                        }
                        break;
                    case "APISite":
                        Ret.Status = OpsDiagnostics.CheckWebsite("https://api.ayanova.com/", "WorkorderTypes Enumeration");
                        break;
                    case "ContactForm":
                        Ret.Status = OpsDiagnostics.CheckWebsite("https://contact.ayanova.com/contact", "Contact.Email");
                        break;
                    // case "RequestForm":
                    //     Ret.Status = OpsDiagnostics.CheckWebsite("https://contact.ayanova.com/request", "Contact.Email");
                    //     break;
                    // case "RequestLiteForm":
                    //     Ret.Status = OpsDiagnostics.CheckWebsite("https://contact.ayanova.com/requestlite", "Contact.Email");
                    //     break;
                    case "Subversion":
                        Ret.Status = OpsDiagnostics.CheckWebsite("http://svn.ayanova.com:3343/csvn/login/auth", "Subversion Edge");
                        break;
                    case "Forum":
                        Ret.Status = OpsDiagnostics.CheckWebsite("http://forum.ayanova.com/", "<title>AyaNova forum</title>");//changed july 2021 but unreleased yet
                        break;
                    case "DevOps":
                        Ret.Status = OpsDiagnostics.CheckWebsite("https://test.helloayanova.com/api/v8/", "AyaNova server");
                        //devops https check url is https://www.helloayanova.com/
                        {
                            DateTime? sslExpiry = OpsDiagnostics.SSLCertExpiryDate("https://www.helloayanova.com/");
                            if (sslExpiry != null)
                            {
                                Ret.ServiceCheckError += "DEVOPS SSL expires: " + sslExpiry.ToString();
                            }
                        }
                        break;
                    case "Backup":
                        Ret.Status = OpsDiagnostics.VerifyBackups();
                        break;
                    default:
                        Ret.Status = false;
                        Ret.ServiceCheckError = $"Unknown service: \"{service}\"";
                        break;
                }
            }
            catch (Exception ex)
            {
                Ret.ServiceCheckError += $"{service}: {ex.Message}\r\n";
            }

            return Ret;
        }

        public class dtoOpsStatus
        {
            public bool Status { get; set; }
            public string ServiceCheckError { get; set; }

            public dtoOpsStatus()
            {
                Status = false;
                ServiceCheckError = string.Empty;
            }
        }


    }//eoc
}//eons