import { UniqueID } from "@/core/common/domain";
import { Collection, Result } from "@repo/rdx-utils";
import { Contact } from "../aggregates";
import { IContactRepository } from "../repositories";
import { IContactService } from "./contact-service.interface";

export class ContactService implements IContactService {
  constructor(private readonly contactRepository: IContactRepository) {}

  async findContact(transaction?: any): Promise<Result<Collection<Contact>, Error>> {
    const contactsOrError = await this.contactRepository.findAll(transaction);
    if (contactsOrError.isFailure) {
      return Result.fail(contactsOrError.error);
    }

    // Solo devolver usuarios activos
    const activeContacts = contactsOrError.data.filter((contact) => contact.isActive);
    return Result.ok(new Collection(activeContacts));
  }

  async findContactById(contactId: UniqueID, transaction?: any): Promise<Result<Contact>> {
    return await this.contactRepository.findById(contactId, transaction);
  }
}
