package com.realnet.testdata.Services;
import com.realnet.testdata.Repository.DemoregistrationRepository;
import com.realnet.testdata.Entity.Demoregistration
;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.testdata.Entity.Country;
import com.realnet.testdata.Services.CountryService;







	import org.springframework.stereotype.Service;

@Service
 public class DemoregistrationService {
@Autowired
private DemoregistrationRepository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;





@Autowired
	private CountryService countryserv;





public Demoregistration Savedata(Demoregistration data) {







 
if (data.getCountry() != null) {
		try {	
  int countryId = Integer.valueOf(data.getCountry());
Country get =  countryserv.getdetailsbyId(countryId);
			if (get != null) {

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

		}

 






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


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


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


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


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

old.setDescription(data.getDescription());

old.setActive (data.isActive());

old.setCountry(data.getCountry());

old.setState(data.getState());




old.setDistrict(data.getDistrict());




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












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

	}}
