import {
  PaginationQueryParameters,
  CountQueryParameters,
} from "hooks/usePagination";
import { Maybe } from "types/utilities";
import { ISegmentService } from "./SegmentService";
import {
  CreateSegmentResponse,
  DeleteSegmentResponse,
  GetPhoneNumberCodesResponse,
  Segment,
  SegmentGetSearchParams,
  SegmentWithCursor,
  Segments,
  UpdateSegmentResponse,
} from "@komi-app/fans-sdk";
import { getSDKConfig } from "config";
import { PaginationDirection } from "@komi-app/fans-sdk";

export class SegmentServiceV1 implements ISegmentService {
  private segments: Segments;
  constructor() {    
    this.segments = new Segments(getSDKConfig('fans'));
  }

  init(profileId: string) {
    this.segments.init(profileId);
  }

  async countSegmentQueryById(segmentId: string): Promise<Maybe<number>> {
    return this.segments.getCount(segmentId);
  }

  async countSegmentQuery(segment: Segment): Promise<Maybe<number>> {
    return this.segments.previewCount(segment);
  }

  async countMultiSegments(
    segmentIds: string[],
    cutoff?: Date,
  ): Promise<Maybe<number>> {
    return this.segments.getEmailRecipientCount(segmentIds, cutoff);
  }

  async getSegments(
    queryParams: SegmentGetSearchParams = {},
  ): Promise<Segment[]> {
    return this.segments.getAll(queryParams);
  }

  async getSegmentsWithCursor(
    queryParams: PaginationQueryParameters,
    searchTerm: string,
  ): Promise<SegmentWithCursor[]> {
    return this.segments.getAllPaginated({
      ...queryParams,
      direction: queryParams.direction as PaginationDirection,
      searchTerm,
    });
  }

  async getSegmentsCount(
    countParams: CountQueryParameters,
    searchTerm: string,
  ): Promise<number> {
    return this.segments.getSearchedCount({
      cutoff: countParams.cutoff,
      searchTerm,
    });
  }

  async getSegment(segmentId: string): Promise<Maybe<Segment>> {
    return this.segments.getById(segmentId);
  }

  async getSegmentByName(name: string): Promise<Maybe<Segment>> {
    return this.segments.getByName({ name });
  }

  async createSegment(segment: Segment): Promise<Maybe<CreateSegmentResponse>> {
    const segmentId = await this.segments.create({ segment });

    return {
      segmentId,
    };
  }
  async updateSegment(segment: Segment): Promise<Maybe<UpdateSegmentResponse>> {
    const segmentId = await this.segments.update(segment);

    return {
      segmentId,
    };
  }
  async deleteSegment(id: string): Promise<Maybe<DeleteSegmentResponse>> {
    const segmentId = await this.segments.deleteSegment(id);
    return {
      segmentId,
    };
  }
  async exists(name: string): Promise<Maybe<boolean>> {
    const segment = await this.segments.getByName({ name });
    return Boolean(segment);
  }

  async getManyByIds(ids: string[]) {
    if (ids.length === 0) {
      return [];
    }

    const list = await this.segments.getManyByIds({
      ids,
    });

    return list;
  }

  async getPhoneNumberCountryCodeOptions(): Promise<GetPhoneNumberCodesResponse> {
    return this.segments.getPhoneNumberCodeOptions();
  }
}
