import { MarketingChannel } from "@prisma/client";
import { ContactListsRepository, ContactListWithActiveCount } from "../repositories/contact-lists-repository";

const MAX_PAGE_SIZE = 100;

interface GetContactListsPaginatedUseCaseRequest {
  userId: string;
  page: number;
  pageSize: number;
  search?: string;
  marketingChannel?: MarketingChannel;
  createdFrom?: Date;
  createdTo?: Date;
}

interface GetContactListsPaginatedUseCaseResponse {
  items: ContactListWithActiveCount[];
  total: number;
  page: number;
  pageSize: number;
}

export class GetContactListsPaginatedUseCase {
  constructor(private contactListsRepository: ContactListsRepository) {}

  async execute({
    page,
    pageSize,
    ...filters
  }: GetContactListsPaginatedUseCaseRequest): Promise<GetContactListsPaginatedUseCaseResponse> {
    const clampedPage = Math.max(page, 1);
    const clampedPageSize = Math.min(Math.max(pageSize, 1), MAX_PAGE_SIZE);

    const { items, total } = await this.contactListsRepository.findAllPaginatedByUserId({
      page: clampedPage,
      pageSize: clampedPageSize,
      ...filters,
    });

    return { items, total, page: clampedPage, pageSize: clampedPageSize };
  }
}
