package e2e_test

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

	"github.com/stretchr/testify/assert"
)

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

			updateRequest := models.Name{
				Middlename: "Aloha",
				FirstName:  "Holla",
				Orgname:    "Adios payasos",
				Secondname: "payasos",
			}

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

			assert.Equal(t, "64ebda4387392e122e5d411f", responseChangeAccount.Body.UserID)
			assert.Equal(t, "Aloha", responseChangeAccount.Body.Name.Middlename)
			assert.Equal(t, "Holla", responseChangeAccount.Body.Name.FirstName)
			assert.Equal(t, "Adios payasos", responseChangeAccount.Body.Name.Orgname)
			assert.Equal(t, "payasos", responseChangeAccount.Body.Name.Secondname)
		})
	})
	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
			}

			statusRequest := models.SetAccountStatus{
				Status: models.AccountStatusOrg,
			}

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

			assert.Equal(t, statusRequest.Status, responseStatusAccount.Body.Status)
		})
	})
}
