package adminservice

import (
	"context"
	"fmt"

	"git.gocasts.ir/ebhomengo/niki/config"
	"git.gocasts.ir/ebhomengo/niki/entity"
	"git.gocasts.ir/ebhomengo/niki/service/auth"
	validator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
	"golang.org/x/crypto/bcrypt"
)

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

type Repository interface {
	AddAdmin(ctx context.Context, admin entity.Admin) (entity.Admin, error)
	GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error)
	GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error)
}

type Service struct {
	repo Repository
	auth AuthService
	vld  validator.Validator
}

func New(repo Repository, auth AuthService, vld validator.Validator) Service {
	return Service{
		repo: repo,
		auth: auth,
		vld:  vld,
	}
}

func GenerateHash(password *string) error {
	hashedPassword, bErr := bcrypt.GenerateFromPassword([]byte(*password), config.BcryptCost)
	if bErr != nil {
		return fmt.Errorf("bcrypt error: %w", bErr)
	}
	*password = string(hashedPassword)

	return nil
}

func CompareHash(hashedPassword, password string) error {
	return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
}
