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 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;











public Product_selection Savedata(Product_selection data) {



 
if (data.getManufacturer() != null) {
		try {	
  int manufacturerId = Integer.valueOf(data.getManufacturer());
Manufacturer get =  manufacturerserv.getdetailsbyId(manufacturerId);
			if (get != null) {

			data.setManufacturername(get.getManufacture());
}
} catch (NumberFormatException e) {
				System.out.println(" manufacturerId is not integer..");
				// Invalid integer string — ignore or log
			}

		}

 












	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());

old.setTotal(data.getTotal());

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














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

	}}
