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 (sv *impl) Login(spec staffdomain.StaffLogin) (string, error) {
	staff, err := sv.staffRepo.GetStaffByEmail(spec.Email)
	if err != nil {
		return "", err
	}

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

	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
}
