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

let contactListsRepository: InMemoryContactListsRepository;
let sut: CreateContactListUseCase;

describe("Create Contact List Use Case", () => {
  beforeEach(() => {
    contactListsRepository = new InMemoryContactListsRepository();
    sut = new CreateContactListUseCase(contactListsRepository);
  });

  it("should be able to create a new contact list", async () => {
    const { list } = await sut.execute({ userId: "user-1", body: { name: "Lista Principal" } });

    expect(list.name).toBe("Lista Principal");
    expect(contactListsRepository.items).toHaveLength(1);
  });

  it("should not allow duplicate list names for the same user", async () => {
    await sut.execute({ userId: "user-1", body: { name: "Lista Principal" } });

    await expect(
      sut.execute({ userId: "user-1", body: { name: "Lista Principal" } })
    ).rejects.toBeInstanceOf(AppError);
  });

  it("should default marketingChannel to EMAIL when not provided", async () => {
    const { list } = await sut.execute({ userId: "user-1", body: { name: "Lista Principal" } });

    expect(list.marketingChannel).toBe("EMAIL");
  });

  it("should allow setting marketingChannel to SMS explicitly", async () => {
    const { list } = await sut.execute({
      userId: "user-1",
      body: { name: "Lista SMS", marketingChannel: "SMS" },
    });

    expect(list.marketingChannel).toBe("SMS");
  });
});
