package de.assecutor.votianlt.pages.service;

import de.assecutor.votianlt.model.Customer;
import de.assecutor.votianlt.pages.domain.CustomerRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import org.bson.types.ObjectId;
import de.assecutor.votianlt.security.SecurityService;

@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class CustomerService {

    private final CustomerRepository todoRepository;
    private final SecurityService securityService;

    CustomerService(CustomerRepository todoRepository, SecurityService securityService) {
        this.todoRepository = todoRepository;
        this.securityService = securityService;
    }

    public List<Customer> list(org.springframework.data.domain.Pageable pageable) {
        return todoRepository.findAllBy(pageable).toList();
    }

    public List<Customer> findAll() {
        return todoRepository.findAll();
    }

    public List<Customer> findAllForCurrentOwner() {
        ObjectId ownerId = securityService.getCurrentUserId();
        return todoRepository.findByOwnerAndInternalFalse(ownerId);
    }

    public Customer save(Customer customer) {
        return todoRepository.save(customer);
    }

    public Customer findById(ObjectId id) {
        return todoRepository.findById(id).orElse(null);
    }

    public void deleteById(ObjectId id) {
        todoRepository.deleteById(id);
    }

}
