package authsvc

import (
	"errors"

	authdomain "legalgo-BE-go/internal/domain/auth"
	staffdomain "legalgo-BE-go/internal/domain/staff"
	"legalgo-BE-go/internal/utilities/utils"

	"github.com/google/uuid"
)

func (a *impl) RegisterStaff(spec staffdomain.StaffRegister) (string, error) {
	_, err := a.staffRepo.GetStaffByEmail(spec.Email)
	if err == nil {
		return "", errors.New("this email address is already in use")
	}
	hashedPwd, err := HashPassword(spec.Password)
	if err != nil {
		return "", err
	}

	staff := staffdomain.Staff{
		ID:             uuid.NewString(),
		Email:          spec.Email,
		Password:       hashedPwd,
		Name:           spec.Name,
		ProfilePicture: spec.ProfilePicture,
	}

	err = a.staffRepo.Create(staff)
	if err != nil {
		return "", errors.New(err.Error())
	}

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

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