import { Maybe } from 'types/utilities'

import { Api } from './api'
import { getApiResponse } from 'utils/api'
import {
  CountQueryParameters,
  PaginationQueryParameters
} from 'hooks/usePagination'
import {
  CreateSegmentResponse,
  DeleteSegmentResponse,
  Segment,
  SegmentWithCursor,
  UpdateSegmentResponse
} from '@komi-app/fans-sdk'

export interface ISegmentService {
  countSegmentQueryById(segmentId: string): Promise<Maybe<number>>
  countSegmentQuery(segment: Segment): Promise<Maybe<number>>
  getSegments(): Promise<Segment[]>
  getSegmentsWithCursor(
    queryParams: PaginationQueryParameters,
    searchTerm: string
  ): Promise<SegmentWithCursor[]>
  getSegmentsCount(
    countParams: CountQueryParameters,
    searchTerm: string
  ): Promise<number>
  getSegment(segmentId: string): Promise<Maybe<Segment>>
  getSegmentByName(name: string): Promise<Maybe<Segment>>
  createSegment(segment: Segment): Promise<Maybe<CreateSegmentResponse>>
  updateSegment(segment: Segment): Promise<Maybe<UpdateSegmentResponse>>
  deleteSegment(segmentId: string): Promise<Maybe<DeleteSegmentResponse>>
  exists(name: string): Promise<Maybe<boolean>>
}

export default class SegmentService implements ISegmentService {
  constructor(private readonly api: Api) {}

  async countSegmentQueryById(segmentId: string): Promise<Maybe<number>> {
    const res = await getApiResponse(this.api.countSegmentQueryById(segmentId))
    return res?.count
  }

  async countSegmentQuery(segment: Segment): Promise<Maybe<number>> {
    const res = await getApiResponse(this.api.countSegmentQuery(segment))
    return res?.count
  }

  async getSegments(): Promise<Segment[]> {
    const res = await getApiResponse(this.api.getSegments())
    return res?.segments || []
  }

  async getSegmentsWithCursor(
    queryParams: PaginationQueryParameters,
    searchTerm: string
  ): Promise<SegmentWithCursor[]> {
    const res = await getApiResponse(
      this.api.getSegmentsWithCursor(queryParams, searchTerm)
    )
    return res?.segments || []
  }

  async getSegmentsCount(
    countParams: CountQueryParameters,
    searchTerm: string
  ): Promise<number> {
    const res = await getApiResponse(
      this.api.getSegmentsCount(countParams, searchTerm)
    )
    return res?.count || 0
  }

  async getSegment(segmentId: string): Promise<Maybe<Segment>> {
    const res = await getApiResponse(this.api.getSegment(segmentId))
    return res?.segment
  }

  async getSegmentByName(name: string): Promise<Maybe<Segment>> {
    const res = await getApiResponse(this.api.getSegmentByName(name))
    return res?.segment
  }

  async createSegment(segment: Segment): Promise<Maybe<CreateSegmentResponse>> {
    return getApiResponse(this.api.createSegment(segment))
  }

  async updateSegment(segment: Segment): Promise<Maybe<UpdateSegmentResponse>> {
    return await getApiResponse(this.api.updateSegment(segment))
  }

  async deleteSegment(
    segmentId: string
  ): Promise<Maybe<DeleteSegmentResponse>> {
    return getApiResponse(this.api.deleteSegment(segmentId))
  }

  async exists(name: string): Promise<Maybe<boolean>> {
    const res = await getApiResponse(this.api.segmentExists(name))
    return res?.exists
  }
}
