// Code generated by candi v1.3.1.

package usecase

import (
	"context"
	"errors"

	"monorepo/sdk"
	authservice "monorepo/sdk/auth-service"
	"monorepo/services/user-service/internal/modules/auth/domain"
	shareddomain "monorepo/services/user-service/pkg/shared/domain"
	"monorepo/services/user-service/pkg/shared/repository"

	"pkg.agungdp.dev/candi/codebase/factory/dependency"
	"pkg.agungdp.dev/candi/codebase/interfaces"
	"pkg.agungdp.dev/candi/logger"
	"pkg.agungdp.dev/candi/tracer"
)

var (
	errorsMemberNotFound = errors.New("Member not found")
)

type authUsecaseImpl struct {
	cache     interfaces.Cache
	repoMongo *repository.RepoMongo
	sdk       sdk.SDK
}

// NewAuthUsecase usecase impl constructor
func NewAuthUsecase(deps dependency.Dependency) AuthUsecase {
	return &authUsecaseImpl{
		cache:     deps.GetRedisPool().Cache(),
		repoMongo: repository.GetSharedRepoMongo(),
		sdk:       sdk.GetSDK(),
	}
}

func (uc *authUsecaseImpl) Login(ctx context.Context, req *domain.LoginRequest) (resp domain.LoginResponse, err error) {
	trace := tracer.StartTrace(ctx, "AuthUsecase:Login")
	defer trace.Finish()
	ctx = trace.Context()

	member := shareddomain.Member{Username: req.Username}
	if err := uc.repoMongo.MemberRepo.Find(ctx, &member); err != nil {
		return resp, errorsMemberNotFound
	}
	if member.Password != req.Password {
		return resp, errorsMemberNotFound
	}

	token, err := uc.sdk.AuthService().GenerateToken(ctx, authservice.PayloadGenerateToken{
		UserID: member.ID, Username: member.Username,
	})
	if err != nil {
		return resp, err
	}

	resp.UserApps, err = uc.sdk.MasterService().GetUserApps(ctx, member.ID)
	if err != nil {
		logger.LogE(err.Error())
	}

	resp.Profile = member
	resp.Token = token.Token
	resp.RefreshToken = token.RefreshToken
	return
}
