package staffsvc

import (
	"fmt"
	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) Register(spec staffdomain.StaffRegister) (string, error) {
	_, err := a.staffRepo.GetStaffByEmail(spec.Email)
	if err == nil {
		return "", fmt.Errorf("this email address is already in use")
	}
	hashedPwd, err := utils.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 "", err
	}

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

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