package e2e_test

import (
	"context"
	"fmt"
	"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"
	"testing"
)

func TestGetAccount(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
			}

			responseGetAccount, errGetAccount := client.Get[models.Account, models.ResponseErrorHTTP](ctx, &client.RequestSettings{
				URL:     "http://localhost:8082/account",
				Headers: map[string]string{"Authorization": fmt.Sprintf("Bearer %s", token)},
			})
			if isNoError := assert.NoError(t, errGetAccount); !isNoError {
				return
			}
			if isNoRequestError := assert.Nil(t, responseGetAccount.Error); !isNoRequestError {
				return
			}

			assert.Equal(t, "64ebda4387392e122e5d411f", responseGetAccount.Body.UserID)
		})
	})
	t.Run("Получение аккаунта юзера по id", func(t *testing.T) {
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()
		assert.NotPanics(t, func() {
			token, tokenErr := jwtUtil.Create("64e5e1ca87392e122e5d3de7")
			if isNoError := assert.NoError(t, tokenErr); !isNoError {
				return
			}

			responseGetAccount, errGetAccount := client.Get[models.Account, models.ResponseErrorHTTP](ctx, &client.RequestSettings{
				URL:     "http://localhost:8083/account/64e5e1ca87392e122e5d3de7",
				Headers: map[string]string{"Authorization": fmt.Sprintf("Bearer %s", token)},
			})
			if isNoError := assert.NoError(t, errGetAccount); !isNoError {
				return
			}
			if isNoRequestError := assert.Nil(t, responseGetAccount.Error); !isNoRequestError {
				return
			}

			assert.Equal(t, "64e5e1ca87392e122e5d3de7", responseGetAccount.Body.UserID)
		})
	})
	t.Run("Получение аккаунтов с пагинацией", func(t *testing.T) {
		token, tokenErr := jwtUtil.Create("64e5e1ca87392e122e5d3de7")
		if isNoError := assert.NoError(t, tokenErr); !isNoError {
			return
		}
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()
		assert.NotPanics(t, func() {

			page := 0
			limit := 3

			params := struct {
				Page  int
				Limit int
			}{
				Page:  page,
				Limit: limit,
			}

			responseGetAccount, errGetAccount := client.Get[models.PaginationResponse[models.Account], models.ResponseErrorHTTP](ctx, &client.RequestSettings{
				URL:     "http://localhost:8083/accounts",
				Body:    params,
				Headers: map[string]string{"Authorization": fmt.Sprintf("Bearer %s", token)},
			})
			if isNoError := assert.NoError(t, errGetAccount); !isNoError {
				return
			}
			if isNoRequestError := assert.Nil(t, responseGetAccount.Error); !isNoRequestError {
				return
			}

			fmt.Println(responseGetAccount.Body)
		})
	})
}
