package org.initialde.yakasave.Integration;

import jakarta.transaction.Transactional;
import org.initialde.yakasave.Api.Requests.LoginRequest;
import org.initialde.yakasave.Domain.Entities.Contribution;
import org.initialde.yakasave.Domain.Entities.SavingsFund;
import org.initialde.yakasave.Domain.Enums.TypeSavingsFund;
import org.initialde.yakasave.Domain.ValueObject.Amount;
import org.initialde.yakasave.Infrastructure.Persistence.SavingsFundRepository;
import org.initialde.yakasave.Infrastructure.Persistence.UserRepository;
import org.initialde.yakasave.Infrastructure.authentication.AuthenticationGateway;
import org.initialde.yakasave.UserFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@Transactional
@SpringBootTest
@AutoConfigureMockMvc
public class RemoveContributorFromSavingsFundControllerTest {
    private final String ENDPOINT = "/savings-fund/{reference}/contributors/{contributorId}/remove";

    @Autowired
    private SavingsFundRepository savingsFundRepository;

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private AuthenticationGateway authenticationGateway;

    @Autowired
    private UserRepository userRepository;

    @BeforeEach
    public void setup() {
        savingsFundRepository.deleteAll();
        userRepository.deleteAll();
    }

    @Test
    public void shouldRemoveContributorFromSavingsFundWhenHasNotContributed() throws Exception {
        var reference = UUID.randomUUID().toString();
        final var ownerCredentials = new LoginRequest("owner", "1234");
        var owner = UserFactory.create("owner", "1234");
        var contributor = UserFactory.create("contributor", "1234");

        userRepository.save(owner);
        userRepository.save(contributor);

        var savingsFund = SavingsFund
                .builder()
                .reference(reference)
                .owner(owner)
                .balance(3000)
                .isActive(true)
                .goalAmount(Amount.MINIMUM_GOAL_AMOUNT)
                .type(TypeSavingsFund.COLLECTIVE)
                .deadline(LocalDate.now().plusDays(1))
                .contributors(new ArrayList<>(List.of(owner, contributor)))
                .build();

        savingsFundRepository.save(savingsFund);
        String token = authenticationGateway.authenticate(ownerCredentials);
        this.mockMvc.perform(patch(ENDPOINT, reference, contributor.getId())
                        .header("Authorization", "Bearer " + token)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andReturn();

        var reloadedSavingsFund = savingsFundRepository.findByReference(reference).get();

        assertFalse(reloadedSavingsFund.getContributors().contains(contributor));
    }

    @Test
    public void shouldFailRemoveContributorFromSavingsFundWhenHasContributed() throws Exception {
        var reference = UUID.randomUUID().toString();
        final var ownerCredentials = new LoginRequest("owner", "1234");
        var owner = UserFactory.create("owner", "1234");
        var contributor = UserFactory.create("contributor", "1234");

        userRepository.save(owner);
        userRepository.save(contributor);

        var savingsFund = SavingsFund
                .builder()
                .reference(reference)
                .owner(owner)
                .balance(3000)
                .isActive(true)
                .goalAmount(Amount.MINIMUM_GOAL_AMOUNT)
                .type(TypeSavingsFund.COLLECTIVE)
                .contributors(new ArrayList<>(List.of(owner, contributor)))
                .contributions(new ArrayList<>(List.of(new Contribution(contributor, 1000.0))))
                .deadline(LocalDate.now().plusDays(1))
                .build();

        savingsFundRepository.save(savingsFund);
        String token = authenticationGateway.authenticate(ownerCredentials);
        this.mockMvc.perform(patch(ENDPOINT, reference, contributor.getId())
                        .header("Authorization", "Bearer " + token)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isConflict())
                .andReturn();
    }
}
