public IActionResult DeleteCustomer(Guid id)

        {
            try
            {
                if (ModelState.IsValid)
                {
                    CustomerServiceResponseModel deleteResponseReciever = _icustomer.DeleteCustomerService(id);

                    if (deleteResponseReciever.code == responseCode.ErrorOccured)
                    {
                        return(BadRequest(deleteResponseReciever.customer, deleteResponseReciever.Message, deleteResponseReciever.code));
                    }
                    else if (deleteResponseReciever.code == responseCode.Successful)
                    {
                        return(Ok(deleteResponseReciever.customer, deleteResponseReciever.Message, deleteResponseReciever.code));
                    }
                    else
                    {
                        return(BadRequest(null, "Error Occured", responseCode.ErrorOccured));
                    }
                }
                return(BadRequest(null, "Null Entity", responseCode.ErrorOccured));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
        public IActionResult CreateCustomer([FromBody] CustomerViewModel customer)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    CustomerServiceResponseModel createMethodServiceResponseModel = _icustomer.CreateCustomerService(customer);

                    if (createMethodServiceResponseModel.code == responseCode.ErrorOccured)
                    {
                        return(BadRequest(createMethodServiceResponseModel.customer, createMethodServiceResponseModel.Message, createMethodServiceResponseModel.code));
                    }
                    else if (createMethodServiceResponseModel.code == responseCode.Successful)
                    {
                        return(Ok(createMethodServiceResponseModel.customer, createMethodServiceResponseModel.Message, createMethodServiceResponseModel.code));
                    }
                    else
                    {
                        return(BadRequest(null, "Error Occured", responseCode.ErrorOccured));
                    }
                }
                return(BadRequest(null, "Null Entity", responseCode.ErrorOccured));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
        public IActionResult UpdateCustomer([FromBody] CustomerViewModel customer)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    CustomerServiceResponseModel updateMethodServiceResponseModel = _icustomer.UpdateCustomerService(customer);

                    if (updateMethodServiceResponseModel.code == "001")
                    {
                        return(BadRequest(updateMethodServiceResponseModel.customer, updateMethodServiceResponseModel.Message, updateMethodServiceResponseModel.code));
                    }
                    else if (updateMethodServiceResponseModel.code == "002")
                    {
                        return(Ok(updateMethodServiceResponseModel.customer, updateMethodServiceResponseModel.Message, updateMethodServiceResponseModel.code));
                    }
                    else if (updateMethodServiceResponseModel.code == "005")
                    {
                        return(BadRequest(updateMethodServiceResponseModel.customer, updateMethodServiceResponseModel.Message, updateMethodServiceResponseModel.code));
                    }
                    else
                    {
                        return(BadRequest(null, "Error Occured", "003"));
                    }
                }
                return(BadRequest(null, "Null Entity", "004"));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
        public IActionResult GetCustomerById(Guid id)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    CustomerServiceResponseModel getByIdResponseReciever = _icustomer.GetCustomerByIdService(id);

                    if (getByIdResponseReciever.code == responseCode.ErrorOccured)
                    {
                        return(BadRequest(getByIdResponseReciever.customer, getByIdResponseReciever.Message, getByIdResponseReciever.code));
                    }
                    else if (getByIdResponseReciever.code == responseCode.Successful)
                    {
                        return(Ok(getByIdResponseReciever.customer, getByIdResponseReciever.Message, getByIdResponseReciever.code));
                    }
                    else
                    {
                        return(BadRequest("Error Occured", responseCode.ErrorOccured));
                    }
                }
                return(BadRequest("Null Entity", responseCode.ErrorOccured));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
        //this service deletes customers by there id
        public CustomerServiceResponseModel DeleteCustomerService(Guid id)
        {
            try
            {
                Customer customer = UnitOfWork.GetRepository <Customer>().Single(p => p.Id == id);
                if (customer == null)
                {
                    customerModel = new CustomerServiceResponseModel()
                    {
                        customer = null, Message = "Entity Does Not Exist", code = responseCode.ErrorOccured
                    };
                    return(customerModel);
                }
                else
                {
                    if (customer.Status == EntityStatus.Active)
                    {
                        customer.Status = EntityStatus.InActive;
                        UnitOfWork.GetRepository <Customer>().Update(customer);
                        UnitOfWork.SaveChanges();

                        //Audit Logger
                        _iauditExtension.Auditlogger(customer.Company_Id, customer.Createdby_Userid, "You Deleted a Customer");

                        customerModel = new CustomerServiceResponseModel()
                        {
                            customer = customer, Message = "Entity Deleted Successfully", code = responseCode.Successful
                        };
                        return(customerModel);
                    }
                    else
                    {
                        customerModel = new CustomerServiceResponseModel()
                        {
                            customer = null, Message = "Entity Does Not Exist", code = responseCode.ErrorOccured
                        };
                        return(customerModel);
                    }
                }
            }
            catch (Exception ex)
            {
                _loggerManager.LogError(ex.Message);
                throw;
            }
        }
        //this service gets customers by there id
        public CustomerServiceResponseModel GetCustomerByIdService(Guid id)
        {
            try
            {
                Customer customer = UnitOfWork.GetRepository <Customer>().Single(p => p.Id == id);

                //since i cant send company directly, i get the company and pass the values i need into the companyViewModel which i then return
                //CompanyViewModel companyViewModel = new CompanyViewModel
                //{
                //    Company_Name = company.Company_Name,
                //    Id = company.Id
                //};

                if (customer != null)
                {
                    if (customer.Status == EntityStatus.Active)
                    {
                        customerModel = new CustomerServiceResponseModel()
                        {
                            customer = customer, Message = "Entity Fetched Successfully", code = responseCode.Successful
                        };
                        return(customerModel);
                    }
                    else
                    {
                        customerModel = new CustomerServiceResponseModel()
                        {
                            customer = null, Message = "Entity Does Not Exist", code = responseCode.ErrorOccured
                        };
                        return(customerModel);
                    }
                }
                customerModel = new CustomerServiceResponseModel()
                {
                    customer = null, Message = "Entity Does Not Exist", code = responseCode.ErrorOccured
                };
                return(customerModel);
            }
            catch (Exception ex)
            {
                _loggerManager.LogError(ex.Message);
                throw ex;
            }
        }
        //this service creates new customers
        public CustomerServiceResponseModel CreateCustomerService(CustomerViewModel customer)
        {
            try
            {
                Company checkIfCompanyExists = UnitOfWork.GetRepository <Company>().Single(p => p.Id == customer.Company_Id && p.Status == EntityStatus.Active);
                if (checkIfCompanyExists == null)
                {
                    customerModel = new CustomerServiceResponseModel()
                    {
                        customer = null, Message = "Company Do Not Exist", code = responseCode.ErrorOccured
                    };
                    return(customerModel);
                }
                else
                {
                    Customer customerToBeCreated = UnitOfWork.GetRepository <Customer>().Single(p => p.Phonenumber == customer.Phonenumber && p.Company_Id == customer.Company_Id);
                    if (customerToBeCreated != null)
                    {
                        customerModel = new CustomerServiceResponseModel()
                        {
                            customer = null, Message = "Customer Already Exists", code = responseCode.ErrorOccured
                        };
                        return(customerModel);
                    }
                    else
                    {
                        customerToBeCreated = new Customer
                        {
                            Company_Id         = customer.Company_Id,
                            Createdby_Userid   = customer.Createdby_Userid,
                            Company_Name       = checkIfCompanyExists.Company_Name,
                            First_Name         = customer.First_Name,
                            Last_Name          = customer.Last_Name,
                            Email              = customer.Email,
                            Phonenumber        = customer.Phonenumber,
                            XendCode           = customer.XendCode,
                            Status             = EntityStatus.Active,
                            CreatedAt          = DateTime.Now,
                            CreatedAtTimeStamp = DateTime.Now.ToTimeStamp(),
                            UpdatedAt          = DateTime.Now,
                            UpdatedAtTimeStamp = DateTime.Now.ToTimeStamp()
                        };
                        UnitOfWork.GetRepository <Customer>().Add(customerToBeCreated);
                        UnitOfWork.SaveChanges();

                        //Audit Logger
                        _iauditExtension.Auditlogger(customerToBeCreated.Company_Id, customerToBeCreated.Createdby_Userid, "You Created a Customer");

                        customerModel = new CustomerServiceResponseModel()
                        {
                            customer = null, Message = "Entity Created Successfully", code = responseCode.Successful
                        };
                        return(customerModel);
                    }
                }
                //unit of work is used to replace _context.

                //Customer customerToBeCreated = UnitOfWork.GetRepository<Customer>().Single(p => p.Email == customer.Email || p.Phonenumber == customer.Phonenumber || p.XendCode == customer.XendCode);
                //if (customerToBeCreated != null)
                //{
                //	customerModel = new CustomerServiceResponseModel() { customer = customerToBeCreated, Message = "Entity Already Exists", code = responseCode.ErrorOccured };
                //	return customerModel;
                //}
                //else
                //{
                //	Company checkIfCompanyExists = UnitOfWork.GetRepository<Company>().Single(p => p.Id == customer.Company_Id && p.Status == EntityStatus.Active);
                //	if (checkIfCompanyExists != null)
                //	{
                //		customerToBeCreated = new Customer
                //		{
                //			Company_Id = customer.Company_Id,
                //			Createdby_Userid = customer.Createdby_Userid,
                //			First_Name = customer.First_Name,
                //			Last_Name = customer.Last_Name,
                //			Email = customer.Email,
                //			Phonenumber = customer.Phonenumber,
                //			XendCode = customer.XendCode,
                //			Status = EntityStatus.Active,
                //			CreatedAt = DateTime.Now,
                //			CreatedAtTimeStamp = DateTime.Now.ToTimeStamp(),
                //			UpdatedAt = DateTime.Now,
                //			UpdatedAtTimeStamp = DateTime.Now.ToTimeStamp()
                //		};
                //		UnitOfWork.GetRepository<Customer>().Add(customerToBeCreated);
                //		UnitOfWork.SaveChanges();

                //		//Audit Logger
                //		_iauditExtension.Auditlogger(customerToBeCreated.Company_Id, customerToBeCreated.Createdby_Userid, "You Created a Customer");

                //		customerModel = new CustomerServiceResponseModel() { customer = customerToBeCreated, Message = "Entity Created Successfully", code = responseCode.Successful };
                //		return customerModel;
                //	}
                //	else
                //	{
                //		customerModel = new CustomerServiceResponseModel() { customer = customerToBeCreated, Message = "Company Do Not Exist", code = responseCode.ErrorOccured };
                //		return customerModel;
                //	}

                //}
            }
            catch (Exception ex)
            {
                _loggerManager.LogError(ex.Message);
                throw;
            }
        }
        //this service updates customer details
        public CustomerServiceResponseModel UpdateCustomerService(CustomerViewModel customer)
        {
            try
            {
                Customer toBeUpdatedCustomer = UnitOfWork.GetRepository <Customer>().Single(p => p.Id == customer.Id);
                if (toBeUpdatedCustomer == null)
                {
                    customerModel = new CustomerServiceResponseModel()
                    {
                        customer = null, Message = "Entity Does Not Exist", code = responseCode.ErrorOccured
                    };
                    return(customerModel);
                }
                else
                {
                    if (toBeUpdatedCustomer.Status == EntityStatus.Active)
                    {
                        Company checkIfCompanyExists = UnitOfWork.GetRepository <Company>().Single(p => p.Id == customer.Company_Id && p.Status == EntityStatus.Active);
                        if (checkIfCompanyExists != null)
                        {
                            //here i will assign directly what i want to update to the model instead of creating a new instance
                            toBeUpdatedCustomer.Updatedby_Userid   = customer.Updatedby_Userid;
                            toBeUpdatedCustomer.First_Name         = customer.First_Name;
                            toBeUpdatedCustomer.Last_Name          = customer.Last_Name;
                            toBeUpdatedCustomer.Email              = customer.Email;
                            toBeUpdatedCustomer.Phonenumber        = customer.Phonenumber;
                            toBeUpdatedCustomer.XendCode           = customer.XendCode;
                            toBeUpdatedCustomer.Status             = EntityStatus.Active;
                            toBeUpdatedCustomer.UpdatedAt          = DateTime.Now;
                            toBeUpdatedCustomer.UpdatedAtTimeStamp = DateTime.Now.ToTimeStamp();
                            UnitOfWork.GetRepository <Customer>().Update(toBeUpdatedCustomer);;
                            UnitOfWork.SaveChanges();

                            //Audit Logger
                            Guid idOfUserWhoUpdatedCustomer = customer.Updatedby_Userid.GetValueOrDefault();
                            _iauditExtension.Auditlogger(toBeUpdatedCustomer.Company_Id, idOfUserWhoUpdatedCustomer, "You Udated a Customer");
                            customerModel = new CustomerServiceResponseModel()
                            {
                                customer = toBeUpdatedCustomer, Message = "Entity Updated Successfully", code = responseCode.Successful
                            };
                            return(customerModel);
                        }
                        else
                        {
                            customerModel = new CustomerServiceResponseModel()
                            {
                                customer = toBeUpdatedCustomer, Message = "Company Do Not Exist", code = responseCode.ErrorOccured
                            };
                            return(customerModel);
                        }
                    }
                    else
                    {
                        customerModel = new CustomerServiceResponseModel()
                        {
                            customer = null, Message = "Entity Does Not Exist", code = responseCode.ErrorOccured
                        };
                        return(customerModel);
                    }
                }
            }
            catch (Exception ex)
            {
                _loggerManager.LogError(ex.Message);
                throw;
            }
        }