package com.realnet.basicp1.Services;
import com.realnet.basicp1.Repository.Add_new_subscriptionRepository;
import com.realnet.basicp1.Entity.Add_new_subscription
;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.Duration_type;
import com.realnet.basicp1.Services.Duration_typeService;



import com.realnet.basicp1.Entity.Subscription_type;
import com.realnet.basicp1.Services.Subscription_typeService;



import com.realnet.basicp1.Entity.Duration_type;
import com.realnet.basicp1.Services.Duration_typeService;







	import org.springframework.stereotype.Service;

@Service
 public class Add_new_subscriptionService {
@Autowired
private Add_new_subscriptionRepository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;





@Autowired
	private Duration_typeService durationserv;

@Autowired
	private Subscription_typeService typeserv;

@Autowired
	private Duration_typeService categoryserv;





public Add_new_subscription Savedata(Add_new_subscription data) {







 
if (data.getDuration() != null) {
		try {	
  int durationId = Integer.valueOf(data.getDuration());
Duration_type get =  durationserv.getdetailsbyId(durationId);
			if (get != null) {

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

		}

 



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

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

		}

 



 
if (data.getCategory() != null) {
		try {	
  int categoryId = Integer.valueOf(data.getCategory());
Duration_type get =  categoryserv.getdetailsbyId(categoryId);
			if (get != null) {

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

		}

 






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


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


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


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


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

old.setPrice(data.getPrice());

old.setRenewal_date(data.getRenewal_date());

old.setDuration(data.getDuration());

old.setType(data.getType());

old.setCategory(data.getCategory());

  
 
old.setEmail(data.isEmail());
   


  

old.setEmail_field(data.getEmail_field());

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
















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

	}}
