import { UniqueID } from "@/core/common/domain";
import { Collection, Result } from "@repo/rdx-utils";
import { IUserRepository, User } from "..";
import { IUserService } from "./user-service.interface";

export class UserService implements IUserService {
  constructor(private readonly userRepository: IUserRepository) {}

  async findUsers(transaction?: any): Promise<Result<Collection<User>, Error>> {
    const usersOrError = await this.userRepository.findAll(transaction);
    if (usersOrError.isFailure) {
      return Result.fail(usersOrError.error);
    }

    // Solo devolver usuarios activos
    const activeUsers = usersOrError.data.filter((user) => user.isActive);
    return Result.ok(new Collection(activeUsers));
  }

  async findUserById(userId: UniqueID, transaction?: any): Promise<Result<User>> {
    return await this.userRepository.findById(userId, transaction);
  }

  /*public async createUser(
    data: { name: string; email: EmailAddress },
    transaction?: Transaction
  ): Promise<Result<User>> {
    // Evitar duplicados por email
    const existingUser = await this.userRepository.findByEmail(data.email);
    if (existingUser.isSuccess) {
      return Result.fail(new Error("El correo ya está registrado."));
    }

    const newUser = User.create({
      email,
      username
    })
    return await this.userRepository.save(newUser, transaction);
  }*/
}
