package org.initialde.yakasave.Unit;

import org.initialde.yakasave.Application.RetrieveOwnerSavingsFunds;
import org.initialde.yakasave.Domain.Entities.SavingsFund;

import org.initialde.yakasave.UserFactory;
import org.initialde.yakasave.Domain.Enums.TypeSavingsFund;
import org.initialde.yakasave.Infrastructure.Persistence.Fake.InMemorySavingsFundRepository;
import org.initialde.yakasave.Infrastructure.authentication.FakeAuthenticationGateway;
import org.initialde.yakasave.Infrastructure.authentication.Jwt.fake.FakeJwtService;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertFalse;

public class RetrieveOwnerSavingsFundTests {
    @Test
    public void shouldRetrieveOwnerCollectiveSavingsFund() {
        var tokenGenerator = new FakeJwtService();

        var authenticationGateway = new FakeAuthenticationGateway(tokenGenerator);
        var user = UserFactory.create( "John Doe", "toto1234");
        authenticationGateway.authenticate(user);

        String reference = UUID.randomUUID().toString();
        var savingsFund = SavingsFundFactory.createSavingsFund(reference, user, TypeSavingsFund.COLLECTIVE);

        var savingsFundRepository = new InMemorySavingsFundRepository();
        savingsFundRepository.save(savingsFund);

        RetrieveOwnerSavingsFunds retrieveOwnerCollectiveSavingsFunds = new RetrieveOwnerSavingsFunds(savingsFundRepository, authenticationGateway);
        List<SavingsFund> expectedCollectiveSavingsFunds = retrieveOwnerCollectiveSavingsFunds.retrieveAll("collective");

        assertFalse(expectedCollectiveSavingsFunds.isEmpty());
    }

    @Test
    public void shouldRetrieveOwnerPersonalSavingsFund() {
        var tokenGenerator = new FakeJwtService();

        var authenticationGateway = new FakeAuthenticationGateway(tokenGenerator);
        var user = UserFactory.create( "John Doe", "toto1234");
        authenticationGateway.authenticate(user);

        String reference = UUID.randomUUID().toString();
        var savingsFund = SavingsFundFactory.createSavingsFund(reference, user, TypeSavingsFund.PERSONAL);

        var savingsFundRepository = new InMemorySavingsFundRepository();
        savingsFundRepository.save(savingsFund);

        RetrieveOwnerSavingsFunds retrieveOwnerSavingsFunds = new RetrieveOwnerSavingsFunds(savingsFundRepository, authenticationGateway);
        List<SavingsFund> expectedPersonalSavingsFunds = retrieveOwnerSavingsFunds.retrieveAll("personal");

        assertFalse(expectedPersonalSavingsFunds.isEmpty());
    }
}
