package com.example.FinanceTracker.Controllers;

import java.time.LocalDate;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestClient;
import com.example.FinanceTracker.DTOs.Investments.CurrRate;
import com.example.FinanceTracker.DTOs.Investments.Currency;
import com.example.FinanceTracker.DTOs.Investments.ExchangeRate;

@Controller
public class InvestmentsController {

    @Autowired
    private RestClient restClient;

    @GetMapping("/investments")
    public String investments(Model model) {

        // Populate Historical Rates Dropdown
        List<Currency> allCurrencies = getAllCurrencies();
        model.addAttribute("allCurrencies", allCurrencies);

        // Create Currency Pills
        List<CurrRate> allRates = getCurrRates();
        model.addAttribute("currRates", allRates);

        // footer
        model.addAttribute("footerDetails", "Copyright © FinanceTracker " + LocalDate.now().getYear());

        return "investments";
    }

    private List<CurrRate> getCurrRates() {
        return List.of("EUR", "USD", "JPY", "GBP", "CAD", "AUD").stream()
                .map(code -> {
                    Double rate = restClient.get()
                            .uri(uriBuilder -> uriBuilder
                                    .path("/exchangeRate/pair")
                                    .queryParam("from", code)
                                    .queryParam("to", "ZAR")
                                    .build())
                            .retrieve()
                            .body(Double.class);

                    return new CurrRate(code, rate.toString(), getChange(code));
                })
                .toList();
    }

    private String getChange(String targetCurr) {

        final String effectiveTargetCurr = targetCurr.toUpperCase().contains("USD") ? "ZAR" : targetCurr;

        List<ExchangeRate> exchangeRate = restClient.get()
                .uri(uriBuilder -> uriBuilder
                        .path("exchangeRate/Historical")
                        .queryParam("targetCurrency", effectiveTargetCurr)
                        .queryParam("fromDate", LocalDate.now().minusDays(1))
                        .queryParam("toDate", LocalDate.now().plusDays(1))
                        .build())
                .retrieve()
                .body(new ParameterizedTypeReference<List<ExchangeRate>>() {
                });

        if (exchangeRate.get(0).getRate().compareTo(exchangeRate.get(1).getRate()) > 0) {
            return "UP";
        } else {
            return "DOWN";
        }

    }

    private List<Currency> getAllCurrencies() {

        return restClient.get()
                .uri("/currency/currencies")
                .retrieve()
                .body(new ParameterizedTypeReference<List<Currency>>() {
                });
    }

}
