using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using rockfishCore.Models;
using rockfishCore.Util;
using System.ComponentModel.DataAnnotations;

namespace rockfishCore.Controllers
{
     //### OUR ROUTE CALLED FROM ROCKFISH CLIENT ####
    [Produces("application/json")]
    [Route("rvl")]
    [Authorize]
    public class RvlController : Controller 
    {
        private readonly rockfishContext ct;


        public RvlController(rockfishContext context)
        {
            ct = context;
        }

        public class dtoRavLicense
        {
            [Required]
            public string RegisteredTo { get; set; }
            [Required]
            public Guid DbId { get; set; }
            [Required]
            public long LicenseExpiration { get; set; }
            [Required]
            public long MaintenanceExpiration { get; set; }
            [Required]
            public List<dtoLicenseFeature> Features { get; set; }
            [Required]
            public long SiteId { get; set; }
        }
        public class dtoLicenseFeature
        {
            dtoLicenseFeature()
            {
                Count = 0;
            }
            //name of feature / product
            public string Feature { get; set; }
            //Optional count for items that require it
            public long Count { get; set; }
        }


#if (DEBUG)
        private const string LICENSE_SERVER_URL = "http://localhost:3001/";
        public const string SUPPORT_EMAIL = "cardjohn@ayanova.com";
#else
        private const string LICENSE_SERVER_URL = "https://rockfish.ayanova.com/";
        public const string SUPPORT_EMAIL="support@ayanova.com";
#endif

        [HttpPost]
        public async Task<IActionResult> Post([FromBody] dtoRavLicense l)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }


            //Get customer from site id
            var CustomerId = await ct.Site.AsNoTracking().Where(z => z.Id == l.SiteId).Select(z => z.CustomerId).FirstOrDefaultAsync();

            if (CustomerId == 0)
            {
                return BadRequest($"Customer could not be found for site id {l.SiteId}");
            }

            var Customer = await ct.Customer.AsNoTracking().Where(z => z.Id == CustomerId).FirstOrDefaultAsync();
            if (Customer == null)
            {
                return BadRequest($"Customer could not be found for customer id {CustomerId}");
            }

            if (string.IsNullOrWhiteSpace(Customer.AdminEmail))
            {
                return BadRequest($"Customer does not have an Admin / License email address, no where to send key");
            }

            if (!Customer.Active)
            {
                return BadRequest($"Customer {Customer.Name} is not Active, set to active and try again");
            }

            if (Customer.DoNotContact)
            {
                return BadRequest($"Customer {Customer.Name} is set to DO NOT CONTACT, unable to process and send key");
            }

            try
            {
                var newLicense = new RavenKeyFactory.AyaNovaLicenseKey();

                newLicense.RegisteredTo = l.RegisteredTo;
                newLicense.DbId = l.DbId;
                newLicense.LicenseExpiration = DateUtil.EpochToDate(l.LicenseExpiration);
                newLicense.MaintenanceExpiration = DateUtil.EpochToDate(l.MaintenanceExpiration);
                foreach (dtoLicenseFeature f in l.Features)
                {
                    newLicense.Features.Add(new RavenKeyFactory.LicenseFeature() { Feature = f.Feature, Count = f.Count });
                }

                //Everything seems to be in order generate the license, save it and send it

                var Key = RavenKeyFactory.GenerateRavenKey(newLicense);

                //save it to the database
                var DBLicense = new License();
                DBLicense.CustomerId = CustomerId;
                DBLicense.SiteId = l.SiteId;
                DBLicense.Email = Customer.AdminEmail;
                DBLicense.DbId = l.DbId;
                DBLicense.Key = Key;
                DBLicense.RegTo = l.RegisteredTo;
                await ct.License.AddAsync(DBLicense);
                await ct.SaveChangesAsync();

                //Key generated, record saved successfully
                //inform customer and return no content
                var body = $"Thank you for your purchase!\nYour AyaNova license key is ready to be installed.\nAyaNova will fetch it automatically within 24 hours or you can force it to fetch immediately from the License page in AyaNova now.\n---\n{l}";
                //send license email
                RfMail.SendMessage("support@ayanova.com", Customer.AdminEmail, "AyaNova license key", body, false);
                return NoContent();
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"Exception processing request: {ex.Message}");
            }
        }





        // [HttpGet("verify/{code}")]
        // public async Task<IActionResult> GetVerify([FromRoute] string code)
        // {
        //     //is there a valid trial request
        //     var req = await ct.TrialRequest.Where(z => z.EmailConfirmCode == code && z.DtFetched == null && z.Status == TrialRequest.TrialRequestStatus.NotSet).FirstOrDefaultAsync();
        //     if (req == null)
        //     {
        //         return new ContentResult
        //         {
        //             ContentType = "text/html",
        //             StatusCode = 200,
        //             Content = "<html><body><h4>Request not found</h4><p>There was no evaluation request associated with this verification code.</p></body></html>"
        //         };
        //     }

        //     req.EmailValidated = true;
        //     await ct.SaveChangesAsync();

        //     //notify *us*
        //     //http://localhost:3001/default.htm#!/trials/[id]
        //     var rfUrl = LICENSE_SERVER_URL + $"default.htm#!/trialEdit/{req.Id}";
        //     var body = $"Email address {req.Email} was just verified for {req.ContactName} at {req.CompanyName}.\r\nTrial key is ready to be processed now:\r\n{rfUrl}";
        //     //send confirmation email
        //     RfMail.SendMessage("support@ayanova.com", SUPPORT_EMAIL, "AyaNova trial request requiring action", body, false);

        //     return new ContentResult
        //     {
        //         ContentType = "text/html",
        //         StatusCode = 200,
        //         Content = "<html><body><h4>Email validated!</h4><p>Your request is being processed and you will receive an email with instructions for starting your AyaNova evaluation.</p></body></html>"
        //     };
        // }


    }//eoc
}//eons