import { SuppressedEmail, SuppressionReason } from "@prisma/client";
import { SuppressedEmailsRepository } from "./suppressed-emails-repository";

export class InMemorySuppressedEmailsRepository implements SuppressedEmailsRepository {
  public items: SuppressedEmail[] = [];

  async isSuppressed(email: string): Promise<boolean> {
    return this.items.some((item) => item.email === email);
  }

  async add(email: string, reason: SuppressionReason, source: string): Promise<SuppressedEmail> {
    const existing = this.items.find((item) => item.email === email);
    if (existing) {
      existing.reason = reason;
      existing.source = source;
      return existing;
    }

    const suppressed: SuppressedEmail = {
      id: `suppressed-${this.items.length + 1}`,
      email,
      reason,
      source,
      createdAt: new Date(),
    };
    this.items.push(suppressed);
    return suppressed;
  }

  async remove(email: string): Promise<void> {
    this.items = this.items.filter((item) => item.email !== email);
  }

  async findAll(): Promise<SuppressedEmail[]> {
    return [...this.items].sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
  }
}
