package com.realnet.order_estimation.Services;
import com.realnet.order_estimation.Repository.Product_selectionRepository;
import com.realnet.order_estimation.Entity.Product_selection
;import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import com.realnet.SequenceGenerator.Service.SequenceService;
import org.springframework.data.domain.Page;
import com.realnet.realm.Entity.Realm;
import com.realnet.realm.Services.RealmService;
import org.springframework.data.domain.Pageable;
import org.springframework.http.*;
import com.realnet.users.service1.AppUserServiceImpl;
import com.realnet.users.entity1.AppUser;


import com.realnet.order_estimation.Entity.Manufacturer;
import com.realnet.order_estimation.Services.ManufacturerService;



import com.realnet.order_estimation.Entity.Type;
import com.realnet.order_estimation.Services.TypeService;



import com.realnet.order_estimation.Entity.Productss;
import com.realnet.order_estimation.Services.ProductssService;







import com.realnet.order_estimation.Entity.Product_selection;

import com.realnet.order_estimation.Repository.Product_selectionRepository;


	import org.springframework.stereotype.Service;

@Service
 public class Product_selectionService {
@Autowired
private Product_selectionRepository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;

@Autowired
	private ManufacturerService manufacturerserv;

@Autowired
	private TypeService typeserv;

@Autowired
	private ProductssService productserv;







public Product_selection Savedata(Product_selection data) {



 
if (data.getManufacturer() != null) {
			Manufacturer get =  manufacturerserv.getdetailsbyId(data.getManufacturer());
			data.setManufacturername(get.getManufacturer_name());

		}

 



 
if (data.getType() != null) {
			Type get =  typeserv.getdetailsbyId(data.getType());
			data.setTypename(get.getType_name());

		}

 



 
if (data.getProduct() != null) {
			Productss get =  productserv.getdetailsbyId(data.getProduct());
			data.setProductname(get.getProduct_name());

		}

 








	data.setUpdatedBy(getUser().getUserId());
		data.setCreatedBy(getUser().getUserId());
		data.setAccountId(getUser().getAccount().getAccount_id());
Product_selection save = Repository.save(data);
				return save;	
			}


//	get all with pagination
	public Page<Product_selection> getAllWithPagination(Pageable page) {
		return Repository.findAll(page, getUser().getUserId());
	}			
public List<Product_selection> getdetails() {  
		List<Realm> realm = realmService.findByUserId(getUser().getUserId());
List<Product_selection> all = Repository.findAll(getUser().getUserId());
		
		return all ;		}


public Product_selection getdetailsbyId(Integer id) {
	return Repository.findById(id).get();
			}


	public void delete_by_id(Integer id) {
 Repository.deleteById(id);
}


public Product_selection update(Product_selection data,Integer id) {
	Product_selection old = Repository.findById(id).get();
old.setName(data.getName());

old.setManufacturer(data.getManufacturer());

old.setType(data.getType());

old.setProduct(data.getProduct());

old.setPrice(data.getPrice());

old.setQuantity(data.getQuantity());



final Product_selection test = Repository.save(old);
		data.setUpdatedBy(getUser().getUserId());
  return test;} 













//	Insert Action

	@Autowired
	private Product_selectionRepository product_selectioninsertrepository;

public Product_selection insertProduct_selection(Product_selection data) {

data.setUpdatedBy(getUser().getUserId());
		data.setCreatedBy(getUser().getUserId());
		data.setAccountId(getUser().getAccount().getAccount_id());


		final Product_selection save = product_selectioninsertrepository.save(data);
		return save;
	}

 public AppUser getUser() {
		AppUser user = userService.getLoggedInUser();
		return user;

	}}
