package adminservice

import (
	"context"
	"fmt"

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

type AuthGenerator interface {
	CreateAccessToken(benefactor entity.Authenticable) (string, error)
	CreateRefreshToken(benefactor entity.Authenticable) (string, 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 AuthGenerator
}

func New(repo Repository, auth AuthGenerator) Service {
	return Service{
		repo: repo,
		auth: auth,
	}
}

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))
}
