package benefactorservice

import (
	"context"
	"time"

	smscontract "git.gocasts.ir/ebhomengo/niki/contract/sms"
	"git.gocasts.ir/ebhomengo/niki/entity"
	"git.gocasts.ir/ebhomengo/niki/service/auth"
	benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
)

type Config struct {
	LengthOfOtpCode int           `koanf:"length_of_otp_code"`
	OtpChars        string        `koanf:"otp_chars"`
	OtpExpireTime   time.Duration `koanf:"otp_expire_time"`
}

type Repository interface {
	IsExistBenefactorByPhoneNumber(ctx context.Context, phoneNumber string) (bool, entity.Benefactor, error)
	CreateBenefactor(ctx context.Context, benefactor entity.Benefactor) (entity.Benefactor, error)
	IsExistBenefactorByID(ctx context.Context, id uint) (bool, error)
	GetByID(ctx context.Context, benefactorID uint) (entity.Benefactor, error)
}

type AuthService interface {
	CreateAccessToken(admin entity.Authenticable) (string, error)
	CreateRefreshToken(admin entity.Authenticable) (string, error)
	ParseRefreshToken(refreshToken string) (*auth.Claims, error)
}

type RedisOtp interface {
	IsExistPhoneNumber(ctx context.Context, phoneNumber string) (bool, error)
	SaveCodeWithPhoneNumber(ctx context.Context, phoneNumber string, code string, expireTime time.Duration) error
	GetCodeByPhoneNumber(ctx context.Context, phoneNumber string) (string, error)
	DeleteCodeByPhoneNumber(ctx context.Context, phoneNumber string) (bool, error)
}

type Service struct {
	config     Config
	redisOtp   RedisOtp
	smsAdapter smscontract.SmsAdapter
	auth       AuthService
	repo       Repository
	vld        benefactorvalidator.Validator
}

func New(cfg Config, redisOtp RedisOtp, smsAdapter smscontract.SmsAdapter,
	auth AuthService, repo Repository, vld benefactorvalidator.Validator,
) Service {
	return Service{
		config:     cfg,
		redisOtp:   redisOtp,
		smsAdapter: smsAdapter,
		auth:       auth,
		repo:       repo,
		vld:        vld,
	}
}
