package client

import (
	"context"
	"fmt"
	"log"
	"net/url"

	"go.uber.org/zap"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/client"
)

type VerificationClientDeps struct {
	Logger *zap.Logger
	URLs   *models.VerificationMicroserviceURL
}

type VerificationClient struct {
	logger *zap.Logger
	urls   *models.VerificationMicroserviceURL
}

func NewVerificationClient(deps VerificationClientDeps) *VerificationClient {
	if deps.Logger == nil {
		log.Panicln("logger is nil on <NewVerificationClient>")
	}

	if deps.URLs == nil {
		log.Panicln("urls is nil on <NewVerificationClient>")
	}

	return &VerificationClient{
		logger: deps.Logger,
		urls:   deps.URLs,
	}
}

func (receiver *VerificationClient) GetVerification(ctx context.Context, token, userID string) (*models.Verification, errors.Error) {
	verifURL, err := url.JoinPath(receiver.urls.Verification, userID)
	if err != nil {
		return nil, errors.New(
			fmt.Errorf("failed to join path on <GetVerification> of <VerificationClient>: %w", err),
			errors.ErrInternalError,
		)
	}

	response, err := client.Get[models.Verification, models.FastifyError](ctx, &client.RequestSettings{
		URL:     verifURL,
		Headers: map[string]string{
      "Content-Type": "application/json",
      "Authorization": token,
    },
	})
	if err != nil {
  fmt.Println("AAAAAAAAAAAa", err, verifURL, response)
		if response.StatusCode == 404 {
			return &models.Verification{},nil
		}
		return nil, errors.New(err, errors.ErrInternalError)
	}

	return response.Body, nil
}
