package auth

import (
	"errors"
	"sync"

	"abuse_registration_poc/internal/models"
)

type UserStore struct {
	mu    sync.RWMutex
	users map[string]models.User
}

func NewStaticUserStore() *UserStore {
	return &UserStore{users: map[string]models.User{
		"reader": {ID: 1, UserName: "reader", Password: "reader-password", Role: models.RoleReader},
		"admin":  {ID: 2, UserName: "admin", Password: "admin-password", Role: models.RoleAdmin},
	}}
}

func (s *UserStore) ValidateCredentials(user *models.ValidateUser) error {
	s.mu.RLock()
	defer s.mu.RUnlock()

	stored, ok := s.users[user.UserName]
	if !ok || stored.Password != user.Password {
		return errors.New("credentials invalid")
	}

	user.ID = stored.ID
	user.Role = stored.Role
	return nil
}

func (s *UserStore) FindByID(id int64) (models.User, bool) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	for _, user := range s.users {
		if user.ID == id {
			return user, true
		}
	}
	return models.User{}, false
}
