package com.realnet.angulardatatype.Services;
import com.realnet.angulardatatype.Repository.Ad6Repository;
import com.realnet.angulardatatype.Entity.Ad6
;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 Ad6Service {
@Autowired
private Ad6Repository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;





@Autowired
	private CountryService countryserv;





public Ad6 Savedata(Ad6 data) {







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

			data.setCountryidentifier(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());
Ad6 save = Repository.save(data);
				return save;	
			}


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


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


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


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

old.setDescription(data.getDescription());



old.setCountry(data.getCountry());

old.setState(data.getState());




old.setDistrict(data.getDistrict());




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












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

	}}
