package org.initialde.yakasave.Application;

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

import java.util.List;

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

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

    public List<SavingsFund> retrieveAll(String type) {
        User user = this.authenticationGateway.getAuthenticatedUser();
        return switch (type) {
            case "collective" -> savingsFundRepository.findAllByTypeAndOwner(TypeSavingsFund.COLLECTIVE, user);
            case "personal" -> savingsFundRepository.findAllByTypeAndOwner(TypeSavingsFund.PERSONAL, user);
            default -> throw new RuntimeException("Le type n'existe pas");
        };
    }
}
