package com.realnet.orderestimate.Services;
import com.realnet.orderestimate.Repository.Order_estimationRepository;
import com.realnet.orderestimate.Entity.Order_estimation
;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.orderestimate.Entity.Manufacturer;
import com.realnet.orderestimate.Services.ManufacturerService;



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









	import org.springframework.stereotype.Service;

@Service
 public class Order_estimationService {
@Autowired
private Order_estimationRepository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;













@Autowired
	private ManufacturerService manufacturerserv;

@Autowired
	private TypeService typeserv;







public Order_estimation Savedata(Order_estimation data) {















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

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

		}

 



 
if (data.getType() != null) {
		try {	
  int typeId = Integer.valueOf(data.getType());
Type get =  typeserv.getdetailsbyId(typeId);
			if (get != null) {

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

		}

 








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


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


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


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


public Order_estimation update(Order_estimation data,Integer id) {
	Order_estimation old = Repository.findById(id).get();
old.setCustomer_name(data.getCustomer_name());

old.setContact_person(data.getContact_person());

old.setEmail_address(data.getEmail_address());

old.setPhone_number(data.getPhone_number());

old.setCustomer_address(data.getCustomer_address());

old.setDeployment_type(data.getDeployment_type());

  
 
old.setApfc_panel_monitoring(data.isApfc_panel_monitoring());
   

 
old.setActive_energy(data.isActive_energy());
   

 
old.setDg_monitoring___with_fuel(data.isDg_monitoring___with_fuel());
   

 
old.setDg_monitoring___without_fuel(data.isDg_monitoring___without_fuel());
   

 
old.setWater_management__water_quality(data.isWater_management__water_quality());
   

 
old.setUps_monitoring(data.isUps_monitoring());
   

 
old.setBusbar_monitoring(data.isBusbar_monitoring());
   

 
old.setWater_management___stp_etp(data.isWater_management___stp_etp());
   

 
old.setLt_panel_monitoring(data.isLt_panel_monitoring());
   

 
old.setHt_panel_monitoring(data.isHt_panel_monitoring());
   

 
old.setCompressed_air___gas_monitoring(data.isCompressed_air___gas_monitoring());
   

 
old.setCompressed_air___lpg_n2_o2_co2(data.isCompressed_air___lpg_n2_o2_co2());
   

 
old.setWater_management___water_monitoring(data.isWater_management___water_monitoring());
   


  

old.setManufacturer(data.getManufacturer());

old.setType(data.getType());

old.setProduct(data.getProduct());




old.setPrice(data.getPrice());

old.setQuantity(data.getQuantity());

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
























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

	}}
