package main

import (
	"fmt"
	"os"

	"github.com/VictorAvelar/mollie-api-go/mollie"
)

var ideal = mollie.PaymentMethod("ideal")

func createMolliePayment(preOrder PreOrder) (mollie.Payment, error) {
	amount := preOrder.CalcTotal() + SHIPPING
	return mollieClient.Payments.Create(mollie.Payment{
		Amount: &mollie.Amount{
			Currency: "EUR",
			Value:    fmt.Sprintf("%.2f", float64(amount)/float64(100)),
		},
		Description: fmt.Sprintf("Flip Noorman - Love It! | CD x %v + verzendkosten", len(preOrder.Items)),
		Method:      &ideal,
		Metadata: map[string]interface{}{
			"preorderID": preOrder.ID,
		},
		RedirectURL: fmt.Sprintf("%s/%v", config.MollieRedirectURL, preOrder.ID),
		WebhookURL:  config.MollieWebhook,
	})
}

func initMollie() (*mollie.Client, error) {
	// API token (`MOLLIE_API_TOKEN`) and the Organization token (`MOLLIE_ORG_TOKEN`)
	os.Setenv("MOLLIE_API_TOKEN", config.MollieToken)
	apiTokenClient, err := mollie.NewClient(nil, mollie.NewConfig(
		config.MollieTesting,
		config.MollieToken,
	))

	if err != nil {
		return nil, err
	}

	return apiTokenClient, nil
}
