package cache

import (
	"jannex/telegram-bot-manager/modules/structs"
	"sync"
)

var tempVerifyCodes = make(map[string]structs.TempVerifyCode)
var tMu sync.RWMutex

func AddTempVerifyCode(tempVerifyCode structs.TempVerifyCode) {
	tMu.Lock()
	defer tMu.Unlock()

	tempVerifyCodes[tempVerifyCode.UserId] = tempVerifyCode
}

func GetTempVerifyByCode(code string) (tempVerify structs.TempVerifyCode, found bool) {
	tMu.RLock()
	defer tMu.RUnlock()

	for _, v := range tempVerifyCodes {
		if v.Code == code {
			return v, true
		}
	}

	return structs.TempVerifyCode{}, false
}

func GetTempVerifyCode(userId string) (string, bool) {
	tMu.RLock()
	defer tMu.RUnlock()

	if v, ok := tempVerifyCodes[userId]; ok {
		return v.Code, true
	}

	return "", false
}

func RemoveTempVerifyCode(userId string) {
	tMu.Lock()
	defer tMu.Unlock()

	delete(tempVerifyCodes, userId)
}

func GetTempVerifyCodes() []structs.TempVerifyCode {
	tMu.RLock()
	defer tMu.RUnlock()

	var tempVerifyCodesSlice []structs.TempVerifyCode

	for _, v := range tempVerifyCodes {
		tempVerifyCodesSlice = append(tempVerifyCodesSlice, v)
	}

	return tempVerifyCodesSlice
}

func RemoveTempVerifyCodes() {
	tMu.Lock()
	defer tMu.Unlock()

}
