package org.initialde.yakasave.Application;

import org.initialde.yakasave.Domain.Entities.SavingsFund;
import org.initialde.yakasave.Infrastructure.Persistence.SavingsFundRepository;
import org.initialde.yakasave.Infrastructure.authentication.AuthenticationGateway;
import org.springframework.stereotype.Service;

@Service
public class JoinSavingsFund {
    private final SavingsFundRepository savingsFundRepository;
    private final AuthenticationGateway authenticationGateway;

    public JoinSavingsFund(SavingsFundRepository savingsFundRepository,
                           AuthenticationGateway authenticationGateway) {
        this.savingsFundRepository = savingsFundRepository;
        this.authenticationGateway = authenticationGateway;
    }

    public void join(SavingsFund savingsFund) {
        var authenticatedUser = authenticationGateway.getAuthenticatedUser();
        savingsFund.addContributor(authenticatedUser);
        this.savingsFundRepository.save(savingsFund);
    }
}
