package com.realnet.taxi_rental.Services;
import com.realnet.taxi_rental.Repository.ProductsRepository;
import com.realnet.taxi_rental.Entity.Products
;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.taxi_rental.Entity.Car_category;
import com.realnet.taxi_rental.Services.Car_categoryService;





import com.realnet.taxi_rental.Entity.Feature;
import com.realnet.taxi_rental.Services.FeatureService;



	import org.springframework.stereotype.Service;

@Service
 public class ProductsService {
@Autowired
private ProductsRepository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;





@Autowired
	private Car_categoryService categoryserv;



@Autowired
	private FeatureService featuresserv;

public Products Savedata(Products data) {







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

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

		}

 





 
if (data.getFeatures() != null) {
		try {	
  int featuresId = Integer.valueOf(data.getFeatures());
Feature get =  featuresserv.getdetailsbyId(featuresId);
			if (get != null) {

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

		}

 


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


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


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


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


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

old.setDescription(data.getDescription());

old.setActive (data.getActive());

old.setCategory(data.getCategory());

 

old.setFeatures(data.getFeatures());

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












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

	}}
