package integration_test

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

var customerServiceBase = os.Getenv("CUSTOMER_SERVICE")

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

	t.Run("Успешная установка статуса верификации <no> аккаунту", func(t *testing.T) {
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

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

			response, getAccountErr := client.Patch[models.Account, models.ResponseErrorHTTP](ctx, &client.RequestSettings{
				URL:     "http://" + customerServiceBase + "/account/807f1f77bcf81cd799439077",
				Body:    models.SetAccountStatus{Status: models.AccountStatusNo},
				Headers: map[string]string{"Authorization": fmt.Sprintf("Bearer %s", token)},
			})
			if isNoError := assert.NoError(t, getAccountErr); !isNoError {
				return
			}
			if isNoRequestError := assert.Nil(t, response.Error); !isNoRequestError {
				return
			}

			assert.Equal(t, "807f1f77bcf81cd799439077", response.Body.UserID)
			assert.Equal(t, models.AccountStatusNo, response.Body.Status)
		})
	})
}

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

	t.Run("Успешная установка статуса верификации <org> аккаунту", func(t *testing.T) {
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

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

			response, getAccountErr := client.Patch[models.Account, models.ResponseErrorHTTP](ctx, &client.RequestSettings{
				URL:     "http://" + customerServiceBase + "/account/807f1f77bcf81cd799439077",
				Body:    models.SetAccountStatus{Status: models.AccountStatusOrg},
				Headers: map[string]string{"Authorization": fmt.Sprintf("Bearer %s", token)},
			})
			if isNoError := assert.NoError(t, getAccountErr); !isNoError {
				return
			}
			if isNoRequestError := assert.Nil(t, response.Error); !isNoRequestError {
				return
			}

			assert.Equal(t, "807f1f77bcf81cd799439077", response.Body.UserID)
			assert.Equal(t, models.AccountStatusOrg, response.Body.Status)
		})
	})
}

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

	t.Run("Успешная установка статуса верификации <nko> аккаунту", func(t *testing.T) {
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

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

			response, getAccountErr := client.Patch[models.Account, models.ResponseErrorHTTP](ctx, &client.RequestSettings{
				URL:     "http://" + customerServiceBase + "/account/807f1f77bcf81cd799439077",
				Body:    models.SetAccountStatus{Status: models.AccountStatusNko},
				Headers: map[string]string{"Authorization": fmt.Sprintf("Bearer %s", token)},
			})
			if isNoError := assert.NoError(t, getAccountErr); !isNoError {
				return
			}
			if isNoRequestError := assert.Nil(t, response.Error); !isNoRequestError {
				return
			}

			assert.Equal(t, "807f1f77bcf81cd799439077", response.Body.UserID)
			assert.Equal(t, models.AccountStatusNko, response.Body.Status)
		})
	})
}

func TestSetAccountVerificationStatusFailure(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("807f1f77bcf81cd799439077")
			if isNoError := assert.NoError(t, tokenErr); !isNoError {
				return
			}

			response, getAccountErr := client.Patch[models.Account, models.ResponseErrorHTTP](ctx, &client.RequestSettings{
				URL:     "http://" + customerServiceBase + "/account/807f1f77bcf81cd799439077",
				Body:    models.SetAccountStatus{Status: "radnom-status"},
				Headers: map[string]string{"Authorization": fmt.Sprintf("Bearer %s", token)},
			})
			if isNoError := assert.NoError(t, getAccountErr); !isNoError {
				return
			}
			if isNotNil := assert.NotNil(t, response.Error); isNotNil {
				assert.Equal(t, 400, response.StatusCode)
			}
		})
	})
}
