package com.realnet.basicp1.Services;
import com.realnet.basicp1.Repository.Adv4Repository;
import com.realnet.basicp1.Entity.Adv4
;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.Support;

import com.realnet.basicp1.Repository.SupportRepository;


import com.realnet.basicp1.Entity.Child;

import com.realnet.basicp1.Repository.ChildRepository;




	import org.springframework.stereotype.Service;

@Service
 public class Adv4Service {
@Autowired
private Adv4Repository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;









public Adv4 Savedata(Adv4 data) {










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


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


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


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


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

old.setChild(data.getChild());





old.setSurvey_form(data.getSurvey_form());

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





//	Insert Action

	@Autowired
	private SupportRepository supportinsertrepository;

public Support insertSupport(Support data) {

data.setUpdatedBy(getUser().getUserId());
		data.setCreatedBy(getUser().getUserId());
		data.setAccountId(getUser().getAccount().getAccount_id());


		final Support save = supportinsertrepository.save(data);
		return save;
	}


//	update Action

	@Autowired
	private ChildRepository childupdaterepository;


	public ResponseEntity<?> updateChild(Integer id, Child data) {
		Integer i = 0;
		Adv4 s = Repository.findById(id).get();

		List<Child> list = new ArrayList<>();

		Object childObject = s.getChild();

		if (childObject instanceof List<?>) {
			// If it's a list, cast and add all elements to the list
			list.addAll((List<Child>) childObject);
		} else if (childObject instanceof Child) {
			// If it's a single Child object, add it to the list
			list.add((Child) childObject);
		}
		

		for (Child li : list) {
			Child old = childupdaterepository.findById(li.getId()).get();

		
			
			final Child childdata = childupdaterepository.save(old);
			i++;
		}
		return new ResponseEntity<>(i + " updated", HttpStatus.OK);
	}



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

	}}
