import { Api } from "./api";
import {
  FanResponseSchema,
  FanTableSchema,
  FanWithCursorResponseSchema,
} from "pages/AudienceAndCampaigns/Pages/Contacts/Utils";
import { OneSchemaSuccessEvent } from "pages/AudienceAndCampaigns/Pages/Contacts/Types";
import { CountQueryParameters, PaginationQueryParameters } from "hooks/usePagination";
import { FanImportStatusChange, MarketingPermission } from "@komi-app/fans-sdk";
import { OkResponse } from "redux/Common/types";

export interface IFansService {
  getFans(
    pageNo: number,
    pageSize: number,
    filter: MarketingPermission,
    searchTerm: string
  ): Promise<FanResponseSchema[]>;
  getFansWithCursor(
    queryParams: PaginationQueryParameters,
    filter: MarketingPermission,
    searchTerm: string
  ): Promise<FanWithCursorResponseSchema[]>;
  getTotalFanCount(
    filter: MarketingPermission,
    searchTerm: string,
  ): Promise<number>;
  getTotalFanCountWithCursor(
    countParams: CountQueryParameters,
    filter: MarketingPermission,
    searchTerm: string,
  ): Promise<number>;
  deleteFans(fans: Partial<FanTableSchema>[], deletionType: string): Promise<any>;
  exportFansAndGetURL(
    fans: FanTableSchema[],
    exportType: string
  ): Promise<OkResponse<string>>;
  getDCMorPresavesSubmissions(): Promise<any>;
  getOneSchemaToken(): Promise<OkResponse<string>>;
  getFansImportInProgress(): Promise<OkResponse<FanImportStatusChange>>;
  startFanImport(
    importEvent: OneSchemaSuccessEvent
  ): Promise<OkResponse<void>>;
}

export default class FansService implements IFansService {
  constructor(private readonly api: Api) {}

  async getFans(
    pageNo: number,
    pageSize: number,
    filter: MarketingPermission,
    searchTerm: string
  ): Promise<FanResponseSchema[]> {
    const res: any = await this.api.getFans(
      pageNo,
      pageSize,
      filter,
      searchTerm
    );
    return res.response;
  }
  async getFansWithCursor(
    queryParams: PaginationQueryParameters,
    filter: MarketingPermission,
    searchTerm: string
  ): Promise<FanWithCursorResponseSchema[]> {
    const res: any = await this.api.getFansWithCursor(
      queryParams,
      filter,
      searchTerm
    );
    return res.response;
  }
  async getTotalFanCount(filter: MarketingPermission, searchTerm: string) {
    const res: any = await this.api.getTotalFanCount(filter, searchTerm);
    return res.response.count;
  }
  async getTotalFanCountWithCursor(
    countParams: CountQueryParameters,
    filter: MarketingPermission,
    searchTerm: string
  ) {
    const res: any = await this.api.getTotalFanCountWithCursor(
      countParams,
      filter,
      searchTerm
    );
    return res.response.count;
  }
  async deleteFans(fans: Partial<FanTableSchema>[], deletionType: string) {
    return await this.api.deleteFans(fans, deletionType);
  }
  async exportFansAndGetURL(
    fans: FanTableSchema[],
    exportType: string
  ): Promise<OkResponse<string>> {
    const response: any = await this.api.exportFansAndGetURL(fans, exportType);
    return response;
  }

  async getDCMorPresavesSubmissions() {
    const res: any = await this.api.getDCMorPresavesSubmissions();
    return res.ok ? res.response : false;
  }

  async getOneSchemaToken(): Promise<OkResponse<string>> {
    const res: any = await this.api.getOneSchemaToken();
    if ("ok" in res) {
      return { ok: res.ok, response: res.response };
    }
    return { ok: false, response: res };
  }

  async getFansImportInProgress(): Promise<OkResponse<FanImportStatusChange>> {
    const res: any = await this.api.getFansImportInProgress();
    if ("ok" in res) {
      return { ok: res.ok, response: res.response };
    }
    return { ok: false, response: res };
  }
  async startFanImport(
    importEvent: OneSchemaSuccessEvent
  ): Promise<OkResponse<void>> {
    const res: any = await this.api.startFanImport(importEvent);
    if ("ok" in res) {
      return { ok: res.ok, response: res.response };
    }
    return { ok: false, response: res };
  }
}
