package de.assecutor.votianlt.pages.service;

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

@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class AddCustomerService {
    private final AddCustomerRepository addCustomerRepository;
    private final SecurityService securityService;

    AddCustomerService(AddCustomerRepository addCustomerRepository, SecurityService securityService) {
        this.addCustomerRepository = addCustomerRepository;
        this.securityService = securityService;
    }

    public void addCustomer(Customer customer) {
        // Setze den aktuellen Benutzer als Ersteller - jetzt direkt aus der Session
        de.assecutor.votianlt.model.User currentUser = securityService.getCurrentDatabaseUser();
        customer.setCreatedBy(currentUser.getId());
        customer.setOwner(currentUser.getId());

        addCustomerRepository.save(customer);
    }
}
