package usersvc

import (
	"fmt"
	authdomain "legalgo-BE-go/internal/domain/auth"
	userdomain "legalgo-BE-go/internal/domain/user"
	"legalgo-BE-go/internal/utilities/utils"

	"github.com/google/uuid"
)

func (i *impl) LoginAsUser(spec userdomain.UserLogin) (string, error) {
	user, err := i.userRepo.GetUserByEmail(spec.Email)
	if err != nil {
		return "", err
	}

	matchPassword := utils.ComparePassword(user.Password, spec.Password)
	if !matchPassword {
		return "", fmt.Errorf("wrong password")
	}

	authToken := authdomain.AuthToken{
		Email:     user.Email,
		SessionID: uuid.NewString(),
		Role:      "user",
		ID:        user.ID,
	}

	token, err := utils.GenerateToken(authToken)
	if err != nil {
		return "", err
	}

	return token, nil
}
