package user

import (
	"context"

	"github.com/rede5/gohorsejobs/backend/internal/core/dto"
	"github.com/rede5/gohorsejobs/backend/internal/core/ports"
)

type ListUsersUseCase struct {
	userRepo ports.UserRepository
}

func NewListUsersUseCase(uRepo ports.UserRepository) *ListUsersUseCase {
	return &ListUsersUseCase{
		userRepo: uRepo,
	}
}

func (uc *ListUsersUseCase) Execute(ctx context.Context, tenantID string) ([]dto.UserResponse, error) {
	users, err := uc.userRepo.FindAllByTenant(ctx, tenantID)
	if err != nil {
		return nil, err
	}

	var response []dto.UserResponse
	for _, u := range users {
		roles := make([]string, len(u.Roles))
		for i, r := range u.Roles {
			roles[i] = r.Name
		}

		response = append(response, dto.UserResponse{
			ID:        u.ID,
			Name:      u.Name,
			Email:     u.Email,
			Roles:     roles,
			Status:    u.Status,
			CreatedAt: u.CreatedAt,
		})
	}

	return response, nil
}
