package com.realnet.basicp1.Services;
import com.realnet.basicp1.Repository.Customer_informationRepository;
import com.realnet.basicp1.Entity.Customer_information
;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.basicp1.Entity.Manufacturer;
import com.realnet.basicp1.Services.ManufacturerService;











import com.realnet.basicp1.Entity.Deployment_type;
import com.realnet.basicp1.Services.Deployment_typeService;

















	import org.springframework.stereotype.Service;

@Service
 public class Customer_informationService {
@Autowired
private Customer_informationRepository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;



@Autowired
	private ManufacturerService manufacturerserv;









@Autowired
	private Deployment_typeService deployment_typeserv;















public Customer_information Savedata(Customer_information data) {





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

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

		}

 











 
if (data.getDeployment_type() != null) {
		try {	
  int deployment_typeId = Integer.valueOf(data.getDeployment_type());
Deployment_type get =  deployment_typeserv.getdetailsbyId(deployment_typeId);
			if (get != null) {

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

		}

 
















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


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


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


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


public Customer_information update(Customer_information data,Integer id) {
	Customer_information old = Repository.findById(id).get();
old.setCompany_name(data.getCompany_name());

old.setContact_person(data.getContact_person());

old.setManufacturer(data.getManufacturer());

old.setType(data.getType());




old.setProduct(data.getProduct());




old.setEmail_address(data.getEmail_address());

old.setCustomer_address(data.getCustomer_address());

old.setDeployment_type(data.getDeployment_type());

  
 
old.setBusbar_monitoring(data.getBusbar_monitoring());
   

 
old.setActive_energy(data.getActive_energy());
   

 
old.setDg_monitoring___with_fule(data.getDg_monitoring___with_fule());
   

 
old.setDg_monitoring___without_fule(data.getDg_monitoring___without_fule());
   

 
old.setUps_monitoring(data.getUps_monitoring());
   

 
old.setApfc_panel_monitoring(data.getApfc_panel_monitoring());
   

 
old.setLt_panel_monitoring(data.getLt_panel_monitoring());
   

 
old.setHt_panel_monitoring(data.getHt_panel_monitoring());
   

 
old.setCompressed_air__gag_monitoring(data.getCompressed_air__gag_monitoring());
   

 
old.setCompressed_air___lpg_n2_o2_co2(data.getCompressed_air___lpg_n2_o2_co2());
   

 
old.setWater_management_stp_etp(data.getWater_management_stp_etp());
   

 
old.setWater_management_water_monitoring(data.getWater_management_water_monitoring());
   


  

old.setQuantity(data.getQuantity());

old.setPrice(data.getPrice());

old.setTotal(data.getTotal());

old.setDescription(data.getDescription());



old.setPhone_number(data.getPhone_number());

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






























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

	}}
