package org.initialde.yakasave.Integration;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.initialde.yakasave.Api.Requests.LoginRequest;
import org.initialde.yakasave.Api.Responses.RetrievedCollectiveSavingsFund;
import org.initialde.yakasave.Api.Responses.RetrievedPersonalSavingsFund;
import org.initialde.yakasave.Domain.Enums.TypeSavingsFund;
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.Unit.SavingsFundFactory;
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 org.springframework.test.web.servlet.MvcResult;

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

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

@SpringBootTest
@AutoConfigureMockMvc
public class RetrieveMySavingsFundControllerTests {
    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private SavingsFundRepository savingsFundRepository;

    @Autowired
    private AuthenticationGateway authenticationGateway;

    @Autowired
    private UserRepository userRepository;

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

    @Test
    public void shouldRetrieveMyCollectiveSavingsFund() throws Exception {
        final var userCredentials = new LoginRequest("John Doe", "toto1234");
        final var user = UserFactory.create("John Doe", "toto1234");
        userRepository.save(user);

        String token = authenticationGateway.authenticate(userCredentials);

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

        savingsFundRepository.save(savingsFund);

        MvcResult result = this.mockMvc.perform(get("/savings-fund?type=collective")
                        .header("Authorization", "Bearer " + token)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andReturn();

        String json = result.getResponse().getContentAsString();
        List<RetrievedCollectiveSavingsFund> collectiveSavingsFund = objectMapper.readValue(json, new TypeReference<>() {
        });
        assertEquals(1, collectiveSavingsFund.size());
    }

    // Reproduces a bug: HTTP 409 Conflict is returned when attempting to retrieve
    // collective funds that include one with a launch date in the past.
    @Test
    public void shouldRetrieveMyCollectiveFundLaunchedBeforeNow() throws Exception {
        final var userCredentials = new LoginRequest("John Doe", "toto1234");
        var user = UserFactory.create("John Doe", "toto1234");
        userRepository.save(user);

        String token = authenticationGateway.authenticate(userCredentials);
        String reference = UUID.randomUUID().toString();
        var savingsFund = SavingsFundFactory.createSavingsFund(reference,
                user, TypeSavingsFund.COLLECTIVE, LocalDate.now().minusDays(2));

        savingsFundRepository.save(savingsFund);

        MvcResult result = this.mockMvc.perform(get("/savings-fund?type=collective")
                        .header("Authorization", "Bearer " + token)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andReturn();

        String json = result.getResponse().getContentAsString();
        List<RetrievedCollectiveSavingsFund> collectiveSavingsFund = objectMapper.readValue(json, new TypeReference<>() {
        });
        assertEquals(1, collectiveSavingsFund.size());
    }

    @Test
    public void shouldRetrieveMyPersonalSavingsFund() throws Exception {
        final var userCredentials = new LoginRequest("John Doe", "toto1234");
        var user = UserFactory.create("John Doe", "toto1234");
        userRepository.save(user);

        String token = authenticationGateway.authenticate(userCredentials);
        String reference = UUID.randomUUID().toString();
        var savingsFund = SavingsFundFactory.createSavingsFund(reference,
                authenticationGateway.getAuthenticatedUser(),
                TypeSavingsFund.PERSONAL);
        savingsFundRepository.save(savingsFund);

        MvcResult result = this.mockMvc.perform(get("/savings-fund?type=personal")
                        .header("Authorization", "Bearer " + token)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andReturn();

        String json = result.getResponse().getContentAsString();
        List<RetrievedPersonalSavingsFund> personalSavingsFund = objectMapper.readValue(json, new TypeReference<>() {
        });
        assertEquals(1, personalSavingsFund.size());
    }
}
