package org.initialde.yakasave.Integration;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.initialde.yakasave.Api.Responses.RetrievedCollectiveSavingsFund;
import org.initialde.yakasave.Api.Responses.RetrievedPersonalSavingsFund;

import org.initialde.yakasave.UserFactory;
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.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 org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

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

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

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

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private SavingsFundRepository savingsFundRepository;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Autowired
    private AuthenticationGateway authenticationGateway;

    @Autowired
    private UserRepository userRepository;

    @BeforeEach
    public void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

        savingsFundRepository.deleteAll();
        userRepository.deleteAll();
    }

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

        authenticationGateway.authenticate(user);

        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")
                .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 shouldRetrieveCollectiveFundLaunchedBeforeNow() throws Exception {
        var user = UserFactory.create( "John Doe", "toto1234");
        userRepository.save(user);

        authenticationGateway.authenticate(user);

        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")
                        .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 shouldRetrieveOwnerPersonalSavingsFund() throws Exception {
        var user = UserFactory.create( "John Doe", "toto1234");
        userRepository.save(user);

        authenticationGateway.authenticate(user);

        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")
                        .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());
    }
}
