// POST: api/Categories/products

        //[Route("products")]
        //[HttpPost]
        //public IHttpActionResult PostCategoriesWithProducts(ICollection<Category> categories)
        //{
        //    if (!ModelState.IsValid)
        //    {
        //        return BadRequest(ModelState);
        //    }

        //    try
        //    {
        //        CategoryProductRepository categpryProductRepo = new CategoryProductRepository(new MyRoomDbContext());
              
        //        categpryProductRepo.InsertCategoryProduct(categories.ToList());

        //        return Ok("Category Product Inserted");
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}
        // POST: api/Categories
        public IHttpActionResult PostCategories(CategoryViewModel categoryViewModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                //if (category.Modules != null)
                //{ 
                //    categoryRepo.Update(category);
                //} 
                Category category = CategoryMapper.CreateModel(categoryViewModel);
                categoryRepo.ModuleStateUnchange(category);
                 
                if (!category.IsFirst)
                {
                    category.Modules = null;
                }

                categoryRepo.Insert(category);

                //busco hotel con el catalogo seleccionado
                ActiveHotelCatalogRepository hotelCatalog = new ActiveHotelCatalogRepository(new MyRoomDbContext());
                int hotelId = hotelCatalog.GetByCatalogId(categoryViewModel.CatalogId);
                if (hotelId > 0)
                { 
                    //inserto categorias a hotel relacionado
                    ActiveHotelCategoryRepository activeHotelCategoryRepo = new ActiveHotelCategoryRepository(new MyRoomDbContext());
                    List<ActiveHotelCategory> hotelscategories = new List<ActiveHotelCategory>();
                    hotelscategories.Add(new ActiveHotelCategory() { IdCategory = category.CategoryId, IdHotel = hotelId });
                    activeHotelCategoryRepo.InsertActiveHotelCategory(hotelscategories, hotelId, true);
                }
                return Ok(category);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public IHttpActionResult PostAssignHotelElements(AssignHotelCatalogViewModel assignHotelCatalogViewModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            ActiveHotelProductRepository activeHotelProductRepo = new ActiveHotelProductRepository(new MyRoomDbContext());
            List<ActiveHotelProduct> products = ActiveHotelProductsMapper.CreateModel(assignHotelCatalogViewModel);
            ActiveHotelCategoryRepository activeHotelCategoryRepo = new ActiveHotelCategoryRepository(new MyRoomDbContext());
            List<ActiveHotelCategory> categories = ActiveHotelCategoriesMapper.CreateModel(assignHotelCatalogViewModel);
            ActiveHotelModuleRepository activeHotelModuleRepo = new ActiveHotelModuleRepository(new MyRoomDbContext());
            List<ActiveHotelModule> modules = ActiveHotelModulesMapper.CreateModel(assignHotelCatalogViewModel);

            try
            {
                activeHotelProductRepo.InsertActiveHotelProduct(products, assignHotelCatalogViewModel.HotelId);
                activeHotelCategoryRepo.InsertActiveHotelCategory(categories, assignHotelCatalogViewModel.HotelId, true);
                activeHotelModuleRepo.InsertActiveHotelModule(modules, assignHotelCatalogViewModel.HotelId, true);
                return Ok("Elements Assigned to hotels");
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }