package e2e_test

import (
	"context"
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/client"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/tests/helpers"
)

func TestBuyTariff(t *testing.T) {
	jwtUtil := helpers.InitializeJWT()

	t.Run("Успешная оплата корзины", func(t *testing.T) {
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

		assert.NotPanics(t, func() {
			token, tokenErr := jwtUtil.Create("64ebda4387392e122e5d411f")
			if isNoError := assert.NoError(t, tokenErr); !isNoError {
				return
			}

			responseAddCart, errAddCart := client.Patch[models.Account, models.ResponseErrorHTTP](ctx, &client.RequestSettings{
				URL:         "http://localhost:8082/cart",
				Headers:     map[string]string{"Authorization": fmt.Sprintf("Bearer %s", token)},
				QueryParams: map[string]string{"id": "64e6105384368b75221a5c3e"},
			})
			if isNoError := assert.NoError(t, errAddCart); !isNoError {
				return
			}
			if isNoRequestError := assert.Nil(t, responseAddCart.Error); !isNoRequestError {
				return
			}

			isUserIDValid := assert.Equal(t, "64ebda4387392e122e5d411f", responseAddCart.Body.UserID)
			isCartLengthValid := assert.Equal(t, 1, len(responseAddCart.Body.Cart))
			isCartItemValid := assert.Equal(t, "64e6105384368b75221a5c3e", responseAddCart.Body.Cart[0])

			if isUserIDValid && isCartItemValid && isCartLengthValid {
				responsePay, errPay := client.Post[models.Account, models.ResponseErrorHTTP](ctx, &client.RequestSettings{
					URL:     "http://localhost:8082/cart/pay",
					Headers: map[string]string{"Authorization": fmt.Sprintf("Bearer %s", token)},
				})
				if isNoError := assert.NoError(t, errPay); !isNoError {
					return
				}
				if isNoRequestError := assert.Nil(t, responsePay.Error); !isNoRequestError {
					return
				}

				assert.Equal(t, "64ebda4387392e122e5d411f", responsePay.Body.UserID)
				assert.Equal(t, 0, len(responsePay.Body.Cart))
				//assert.Equal(t, responseAddCart.Body.Wallet.Cash-5000, responsePay.Body.Wallet.Cash)
				//assert.Equal(t, responseAddCart.Body.Wallet.Money-5000, responsePay.Body.Wallet.Money)
				//assert.Equal(t, responseAddCart.Body.Wallet.Spent+5000, responsePay.Body.Wallet.Spent)
				assert.Equal(t, responseAddCart.Body.Wallet.PurchasesAmount, responsePay.Body.Wallet.PurchasesAmount)
				assert.Equal(t, "RUB", responsePay.Body.Wallet.Currency)
			}
		})
	})
}
