using System.Linq.Expressions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using sodoff.Model;
using sodoff.Schema;
using sodoff.Services;

namespace sodoff.Controllers.Internal
{
    [ApiController]
    public class ModerationController : Controller
    {
        public readonly DBContext ctx;
        public readonly ModerationService moderationService;
        public ModerationController(DBContext ctx, ModerationService moderationService)
        {
            this.ctx = ctx;
            this.moderationService = moderationService;
        }

        [HttpPost]
        [Route("Moderation/AddBanToVikingByGuid")]
        public IActionResult AddBanToVikingByGuid([FromForm] Guid token, [FromForm] Guid userId, [FromForm] int banType, [FromForm] int days = 0)
        {
            var validationResult = ValidateSession(token);

            if(validationResult)
            {
                // get the viking
                Viking? viking = ctx.Vikings.FirstOrDefault(e => e.Uid == userId);

                // get execution timestamp
                DateTime timestamp = DateTime.UtcNow;

                DateTime expiration = new DateTime();

                if (days == 0) expiration = new DateTime(9999, 99, 99); // a days value of 0 should mean indefinite ban (might change later)
                else expiration = timestamp.AddDays(days);

                if (viking != null) return Ok(moderationService.AddBanToViking(viking, (UserBanType)banType, expiration));
                else return NotFound();
            }

            return Unauthorized("You Do Not Have Sufficient Permissions To Moderate Users");
        }

        [HttpPost]
        [Route("Moderation/RemoveBansFromVikingByGuidAndType")]
        public IActionResult RemoveBanFromVikingByGuid([FromForm] Guid token, [FromForm] Guid userId, [FromForm] int banType)
        {
            var validationResult = ValidateSession(token);

            if (validationResult)
            {
                // get the viking
                Viking? viking = ctx.Vikings.FirstOrDefault(e => e.Uid == userId);

                // remove all bans of type
                if (viking != null) return Ok(moderationService.RemoveBansFromVikingByType(viking, (UserBanType)banType));
                else return NotFound();
            }

            return Unauthorized("You Do Not Have Sufficient Permissions To Moderate Users");
        }

        [HttpPost]
        [Route("Moderation/CheckForVikingBan")]
        public IActionResult CheckForVikingBan([FromForm] Guid token)
        {
            // get viking session
            var session = ctx.Sessions.FirstOrDefault(e => e.ApiToken == token);

            if (session != null && session.Viking != null) return Ok(moderationService.IsVikingBanned(session.Viking));
            else return Ok(UserBanType.NotBanned); // invalid session, for now just return not banned
        }

        private bool ValidateSession(Guid token)
        {
            // get active session
            var session = ctx.Sessions.FirstOrDefault(e => e.ApiToken == token);

            if (session != null)
            {
                // most endpoints here should only be activated by a 'Moderator' or above
                Role? vikingRole = session.Viking?.MMORoles.FirstOrDefault()?.Role;
                if (vikingRole != null && (vikingRole == Role.Moderator || vikingRole == Role.Admin)) return true;
                else return false;
            } else return false;
        }
    }
}
