// [Authorize(Roles = "SC_SPECIALIST,SC_REVIEWER,PPRB_SPECIALIST,PPRB_REVIEWER,PPRM_SPECIALIST,PPRM_REVIEWER")]
        public IActionResult Edit(int?id, string useCase, string mk = null)
        {
            if (HttpContext == null ||
                HttpContext.Session == null ||
                HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY) == null)
            {
                return(RedirectToAction("Warning", "Home"));
            }

            string logSnippet = new StringBuilder("[")
                                .Append(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"))
                                .Append("][CorrectiveActionsController][HttpGet][Edit] => ")
                                .ToString();

            UserViewModel qmsUserVM = HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY);

            Console.WriteLine(logSnippet + $"(qmsUserVM): {qmsUserVM}");

            if (qmsUserVM.CanEditCorrectiveAction == false && qmsUserVM.CanViewAllCorrectiveActions == false)
            {
                return(RedirectToAction("UnauthorizedAccess", "Home"));
            }

            if (id == null || id.HasValue == false)
            {
                Console.WriteLine(logSnippet + $"ID that was passed-in is null or has no value. Cannot continue. (id: '{id}')");
                return(NotFound());
            }

            Console.WriteLine(logSnippet + $"(CorectiveActionId): '{id}'");
            Console.WriteLine(logSnippet + $"(useCase)..........: '{useCase}'");

            User             qmsUser = _userService.RetrieveByEmailAddress(qmsUserVM.EmailAddress);
            CorrectiveAction entity  = _correctiveActionService.RetrieveById(id.Value, qmsUser);

            Console.WriteLine(logSnippet + "(CorrectiveAction.Status == null): " + (entity.Status == null));

            Console.WriteLine(logSnippet + "CorrectiveAction.ToJson() => BEGIN");
            Console.WriteLine(entity.ToJson());
            Console.WriteLine(logSnippet + "END <= CorrectiveAction.ToJson()");

            HttpContext.Items[CorrectiveActionsConstants.NOA_CODE_KEY]          = entity.NOACode;
            HttpContext.Items[CorrectiveActionsConstants.CURRENT_STATUS_ID_KEY] = entity.StatusId;

            string[] selectedErrorTypeIds = CorrectiveActionUtil.ExtractErrorTypeIds(entity);
            HttpContext.Items[CorrectiveActionsConstants.SELECTED_ERROR_TYPES_KEY] = selectedErrorTypeIds;

            Console.WriteLine(logSnippet + $"(qmsUserVM.CanAssignTasks).....: {qmsUserVM.CanAssignTasks}");
            Console.WriteLine(logSnippet + $"(CorrectiveAction.IsAssignable): {entity.IsAssignable}");

            HttpContext.Items[CorrectiveActionsConstants.IS_ASSIGNABLE_KEY] = false;
            HttpContext.Items[CorrectiveActionsConstants.CURRENT_ASSIGNED_TO_USER_ID_KEY] = null;

            if (qmsUserVM.CanAssignTasks && entity.IsAssignable)
            {
                HttpContext.Items[CorrectiveActionsConstants.IS_ASSIGNABLE_KEY] = true;
                if (entity.AssignedByUserId.HasValue)
                {
                    HttpContext.Items[CorrectiveActionsConstants.CURRENT_ASSIGNED_TO_USER_ID_KEY] = entity.AssignedToUserId.ToString();
                }
                else
                {
                    HttpContext.Items[CorrectiveActionsConstants.CURRENT_ASSIGNED_TO_USER_ID_KEY] = null;
                }
            }

            if (HttpContext.Session.GetObject <bool>(CorrectiveActionsConstants.NEWLY_CREATED_COMMENT_KEY) == true)
            {
                ViewData[CorrectiveActionsConstants.NEWLY_CREATED_COMMENT_KEY] = HttpContext.Session.GetObject <bool>(CorrectiveActionsConstants.NEWLY_CREATED_COMMENT_KEY);
                HttpContext.Session.SetObject(CorrectiveActionsConstants.NEWLY_CREATED_COMMENT_KEY, false);
            }

            ViewData[CorrectiveActionsConstants.CA_EDIT_MESSAGE_KEY] = null;
            if (String.IsNullOrEmpty(mk) == false && String.IsNullOrWhiteSpace(mk) == false)
            {
                if (HttpContext.Session.GetObject <string>(mk) != null)
                {
                    ViewData[CorrectiveActionsConstants.CA_EDIT_MESSAGE_KEY] = HttpContext.Session.GetObject <string>(mk);
                    HttpContext.Session.SetObject(mk, null);
                }
            }

            List <ModuleMenuItem> moduleMenuItems = HttpContext.Session.GetObject <List <ModuleMenuItem> >(MiscConstants.MODULE_MENU_ITEMS_SESSION_KEY);

            return(View(CorrectiveActionUtil.MapToViewModel(entity, qmsUserVM.UserId, useCase, moduleMenuItems, qmsUserVM.CanAssignTasks)));
        }
        // [Authorize(Roles = "SC_SPECIALIST,SC_REVIEWER,PPRB_SPECIALIST,PPRB_REVIEWER")]
        public IActionResult Create
        (
            [Bind("EmployeeSearchResult, NatureOfAction,EffectiveDateOfPar,IsPaymentMismatch,ErrorTypeIds,ActionRequestTypeId,StatusTypeId,Details")]
            CorrectiveActionFormViewModel correctiveActionVM, string[] selectedErrorTypes
        )
        {
            if (HttpContext == null ||
                HttpContext.Session == null ||
                HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY) == null)
            {
                return(RedirectToAction("Warning", "Home"));
            }

            string logSnippet = new StringBuilder("[")
                                .Append(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"))
                                .Append("][CorrectiveActionsController][HttpPost][Create] => ")
                                .ToString();

            UserViewModel qmsUserVM = HttpContext.Session.GetObject <UserViewModel>(MiscConstants.USER_SESSION_VM_KEY);

            Console.WriteLine(logSnippet + $"(qmsUserVM): {qmsUserVM}");

            if (qmsUserVM.CanCreateCorrectiveAction == false)
            {
                return(RedirectToAction("UnauthorizedAccess", "Home"));
            }

            ///////////////////////////////////////////////////////////////////////////////////
            // Error Categories Validation
            ///////////////////////////////////////////////////////////////////////////////////
            if (selectedErrorTypes == null || selectedErrorTypes.Count() == 0)
            {
                Console.WriteLine(logSnippet + "No error categories/types selected. Adding a model error.");
                ModelState.AddModelError(string.Empty, "Please select at least one error category.");
            }

            ///////////////////////////////////////////////////////////////////////////////////
            // Employee Search
            ///////////////////////////////////////////////////////////////////////////////////
            string emplIdStr = CorrectiveActionUtil.ExtractEmplId(correctiveActionVM.EmployeeSearchResult);
            long   emplIdLong;

            if (String.IsNullOrEmpty(emplIdStr) == false &&
                long.TryParse(emplIdStr, out emplIdLong) == true)
            {
                try
                {
                    Employee employee = _employeeService.RetrieveById(emplIdStr);
                }
                catch (EmployeeNotFoundException enfe)
                {
                    Console.WriteLine(logSnippet + "EmployeeNotFoundException encountered:");
                    Console.WriteLine(enfe.Message);
                    ModelState.AddModelError(string.Empty, $"Employee not found with Employee ID of '{emplIdStr}'");
                }
            }

            Console.WriteLine(logSnippet + $"(ModelState.IsValid): {ModelState.IsValid}");

            if (ModelState.IsValid)
            {
                CorrectiveAction correctiveActionModel = CorrectiveActionUtil.MapToUIModelOnCreate(correctiveActionVM, selectedErrorTypes);
                User             qmsUser = _userService.RetrieveByEmailAddress(qmsUserVM.EmailAddress);

                Console.WriteLine(logSnippet + "CorrectiveAction (BEFORE SAVE) => BEGIN");
                Console.WriteLine(correctiveActionModel.ToJson());
                Console.WriteLine(logSnippet + "END <= CorrectiveAction (BEFORE SAVE)");

                int entityId = _correctiveActionService.Save(correctiveActionModel, qmsUser);

                Console.WriteLine(logSnippet
                                  + $"Newly created CorrectiveAction #{entityId} has been saved to the database, redirecting in Home Index page");

                ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                // PREPARE SUCCESS MESSAGE
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                string msgKey = Guid.NewGuid().ToString();
                string msgVal = $"Corrective Action with an ID of {entityId} has been created for {correctiveActionVM.EmployeeSearchResult}";
                HttpContext.Session.SetObject(msgKey, msgVal);

                return(RedirectToAction("Index", "Home", new { @mk = msgKey }));
            }
            else
            {
                Console.WriteLine(logSnippet + "ModelState IS NOT VALID, returning User to Create Corrective Actions page");
                if (selectedErrorTypes != null || selectedErrorTypes.Count() > 0)
                {
                    /////////////////////////////////////////////////////////////////////////////////////////////
                    // The HttpContext.Items collection is used to store data while processing a single request.
                    // The collection's contents are discarded after a request is processed.
                    /////////////////////////////////////////////////////////////////////////////////////////////
                    HttpContext.Items[CorrectiveActionsConstants.SELECTED_ERROR_TYPES_KEY] = selectedErrorTypes;
                }

                return(View());
            }
        }