import { describe, it, expect, beforeEach } from "vitest";
import { InMemoryContactListsRepository } from "../repositories/in-memory-contact-lists-repository";
import { AssignContactsToListsUseCase } from "./assign-contacts-to-lists-use-case";
import { AppError } from "@/core/errors/app-error";

let contactListsRepository: InMemoryContactListsRepository;
let sut: AssignContactsToListsUseCase;

describe("Assign Contacts To Lists Use Case", () => {
  beforeEach(() => {
    contactListsRepository = new InMemoryContactListsRepository();
    sut = new AssignContactsToListsUseCase(contactListsRepository);
  });

  it("should associate contacts to the given lists", async () => {
    const list = await contactListsRepository.create({ name: "Lista A", userId: "user-1" });

    await sut.execute({
      userId: "user-1",
      body: { contactIds: ["contact-1", "contact-2"], listIds: [list.id] },
    });

    expect(contactListsRepository.memberships).toHaveLength(2);
  });

  it("should not allow assigning contacts to another user's list", async () => {
    const list = await contactListsRepository.create({ name: "Lista A", userId: "user-1" });

    await expect(
      sut.execute({ userId: "user-2", body: { contactIds: ["contact-1"], listIds: [list.id] } })
    ).rejects.toBeInstanceOf(AppError);
  });
});
