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.UserFactory;
import org.initialde.yakasave.Domain.Entities.WithdrawalApproval;
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.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.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@Transactional
@SpringBootTest
@AutoConfigureMockMvc
public class ApproveWithdrawFromSavingsFundControllerTest {
    private final String ENDPOINT = "/savings-fund/{reference}/approve-withdraw";
    @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 shouldApproveWithdrawFromSavingsFunWhichNeedsApproval() throws Exception {
        var reference = UUID.randomUUID().toString();

        final var owner = UserFactory.create( "owner", "1234");
        userRepository.save(owner);

        final var contributorCredentials = new LoginRequest("contributor", "1234");
        final var contributor = UserFactory.create( "contributor", "1234");
        userRepository.save(contributor);

        final var contributionOfContributor = new Contribution(contributor, 1000.0);

        final var savingsFund = SavingsFund
                .builder()
                .reference(reference)
                .owner(owner)
                .goalAmount(Amount.MINIMUM_GOAL_AMOUNT)
                .deadline(LocalDate.now().plusDays(1))
                .needsApproval(true)
                .type(TypeSavingsFund.COLLECTIVE)
                .maxAllowedMembers(3)
                .contributors(new ArrayList<>(List.of(owner, contributor)))
                .contributions(new ArrayList<>(List.of(contributionOfContributor)))
                .build();
        savingsFundRepository.save(savingsFund);

        String token = authenticationGateway.authenticate(contributorCredentials);

        this.mockMvc.perform(post(ENDPOINT, reference)
                        .header("Authorization", "Bearer " + token)
                        .contentType(MediaType.APPLICATION_JSON))                .andExpect(status().isOk())
                .andReturn();
    }

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

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

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

        savingsFundRepository.save(savingsFund);

        userRepository.save(contributor);
        String token = authenticationGateway.authenticate(contributorCredentials);

        this.mockMvc.perform(post(ENDPOINT, reference)
                        .header("Authorization", "Bearer " + token)
                        .contentType(MediaType.APPLICATION_JSON))                .andExpect(status().isConflict())
                .andReturn();

    }

    @Test
    public void shouldFailApproveWithdrawFromSavingsFunWhichNeedsApprovalWhenContributorAlreadyApproved() throws Exception {
        var reference = UUID.randomUUID().toString();
        var owner = UserFactory.create( "owner", "1234");
        final var contributorCredentials = new LoginRequest("contributor", "1234");
        final var contributor = UserFactory.create( "contributor", "1234");
        var contributionOfContributor = new Contribution(contributor, 1000.0);
        var contributorApproval = new WithdrawalApproval(contributor);

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

        var savingsFund = SavingsFund
                .builder()
                .reference(reference)
                .owner(owner)
                .goalAmount(Amount.MINIMUM_GOAL_AMOUNT)
                .deadline(LocalDate.now().plusDays(1))
                .needsApproval(true)
                .type(TypeSavingsFund.COLLECTIVE)
                .maxAllowedMembers(3)
                .contributors(new ArrayList<>(List.of(owner, contributor)))
                .contributions(new ArrayList<>(List.of(contributionOfContributor)))
                .receiptApprovals(new ArrayList<>(List.of(contributorApproval)))
                .build();


        savingsFundRepository.save(savingsFund);

        userRepository.save(contributor);
        String token = authenticationGateway.authenticate(contributorCredentials);

        this.mockMvc.perform(post(ENDPOINT, reference)
                        .header("Authorization", "Bearer " + token)
                        .contentType(MediaType.APPLICATION_JSON))                .andExpect(status().isConflict())
                .andReturn();
    }

    @Test
    public void shouldFailApproveWithdrawFromSavingsFunWhichNeedsApprovalWhenContributorIsOwner() throws Exception {
        var reference = UUID.randomUUID().toString();
        final var ownerCredentials = new LoginRequest( "owner", "1234");
        final 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)
                .goalAmount(Amount.MINIMUM_GOAL_AMOUNT)
                .deadline(LocalDate.now().plusDays(1))
                .needsApproval(true)
                .type(TypeSavingsFund.COLLECTIVE)
                .maxAllowedMembers(3)
                .contributors(new ArrayList<>(List.of(owner, contributor)))
                .build();

        savingsFundRepository.save(savingsFund);

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