package ocr

import (
	"context"
	"errors"

	"github.com/carlmjohnson/requests"
)

func DecodeGeminiCaptcha(ctx context.Context, image []byte, typ string) (string, error) {
	var captchaResult string
	var errBody string

	url := "https://notvis.notitek.com.tr" + "/api/captcha/gemini"

	err := requests.
		URL(url).
		Header("X-Api-Key", "3c43c4eb-4b5d-4636-8dd5-3d573a7a0a27").
		//Header("Content-Type", "application/json").
		//Header("Authorization", "Bearer "+l.sessionID).
		BodyBytes(image).
		ToString(&captchaResult).
		Method("POST").
		AddValidator(
			requests.ValidatorHandler(
				requests.DefaultValidator,
				requests.ToString(&errBody),
			)).
		Fetch(ctx)

	if err != nil {
		if errors.Is(err, requests.ErrInvalidHandled) {
			err = errors.New(errBody)
		}
	}
	return captchaResult, err
}

func DecodeTwoCaptcha(ctx context.Context, image []byte, typ string) (string, error) {
	var captchaResult string
	var errBody string

	url := "https://notvis.notitek.com.tr" + "/api/captcha/twoCaptcha"

	err := requests.
		URL(url).
		Header("X-Api-Key", "3c43c4eb-4b5d-4636-8dd5-3d573a7a0a27").
		BodyBytes(image).
		ToString(&captchaResult).
		Method("POST").
		AddValidator(
			requests.ValidatorHandler(
				requests.DefaultValidator,
				requests.ToString(&errBody),
			)).
		Fetch(ctx)

	if err != nil {
		if errors.Is(err, requests.ErrInvalidHandled) {
			err = errors.New(errBody)
		}
		return "", err
	}
	return captchaResult, err
}
