import {
  PaginationQueryParameters,
  CountQueryParameters,
} from "hooks/usePagination";
import { OneSchemaSuccessEvent } from "pages/AudienceAndCampaigns/Pages/Contacts/Types";
import {
  FanResponseSchema,
  FanWithCursorResponseSchema,
  FanTableSchema,
} from "pages/AudienceAndCampaigns/Pages/Contacts/Utils";
import { IFansService } from "./FansService";
import {
  FanImportStatusChange,
  FanSubmission,
  Fans,
  MarketingPermission,
  PaginatedFanSubmission,
  PaginationDirection,
} from "@komi-app/fans-sdk";
import { getSDKConfig } from "config";
import { OkResponse } from "redux/Common/types";
import { fansServiceLegacy } from "services";

export class FansServiceV1 implements IFansService {
  private fans: Fans;
  constructor() {    
    this.fans = new Fans(getSDKConfig('fans'));
  }

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

  async getFans(
    pageNo: number,
    pageSize: number,
    filter: MarketingPermission,
    searchTerm: string,
  ): Promise<FanResponseSchema[]> {
    return this.fans
      .getFans({
        pageNo: String(pageNo),
        pageSize: String(pageSize),
        filter,
        searchTerm,
      })
      .then(this.fanSubmissionsToLegacyResponse);
  }

  async getFansWithCursor(
    queryParams: PaginationQueryParameters,
    filter: MarketingPermission,
    searchTerm: string,
  ): Promise<FanWithCursorResponseSchema[]> {
    return this.fans
      .getFansV2({
        ...queryParams,
        direction: queryParams.direction as PaginationDirection,
        filter,
        searchTerm,
      })
      .then(this.fanSubmissionsToLegacyResponse);
  }

  async getTotalFanCount(
    filter: MarketingPermission,
    searchTerm: string,
  ): Promise<number> {
    return this.fans.getCount(filter, searchTerm);
  }

  async getTotalFanCountWithCursor(
    countParams: CountQueryParameters,
    filter: MarketingPermission,
    searchTerm: string,
  ): Promise<number> {
    return this.fans.getCountPaginated({
      ...countParams,
      filter,
      searchTerm,
    });
  }
  async deleteFans(
    fans: Partial<FanTableSchema>[],
    deletionType: string,
  ): Promise<any> {
    // TODO: FIX RETURN TYPE ONCE OLD FANS SERVICE DEPRECATED
    return this.fans.deleteMany({ fans, deletionType });
  }
  async exportFansAndGetURL(
    fans: FanTableSchema[],
    exportType: string,
  ): Promise<OkResponse<string>> {
    // TODO: FIX RETURN TYPE ONCE OLD FANS SERVICE DEPRECATED
    const url = await this.fans.export({ fans, exportType });

    return {
      ok: true,
      response: url,
    };
  }
  async getOneSchemaToken(): Promise<OkResponse<string>> {
    // TODO: FIX RETURN TYPE ONCE OLD FANS SERVICE DEPRECATED
    const res = await this.fans.import.oneSchema.getToken();
    return {
      ok: true,
      response: res,
    };
  }
  async getFansImportInProgress(): Promise<OkResponse<FanImportStatusChange>> {
    // TODO: FIX RETURN TYPE ONCE OLD FANS SERVICE DEPRECATED
    const res = await this.fans.import.getProgress();
    return {
      ok: true,
      response: res,
    };
  }
  async startFanImport(
    importEvent: OneSchemaSuccessEvent,
  ): Promise<OkResponse<void>> {
    // TODO: FIX RETURN TYPE ONCE OLD FANS SERVICE DEPRECATED
    const res = await this.fans.import.start({
      count: importEvent.count,
      embed_id: String(importEvent.embed_id),
      sheet_id: String(importEvent.sheet_id),
    });

    return {
      ok: true,
      response: res,
    };
  }

  private fanSubmissionsToLegacyResponse<
    // TODO: DEPRECATE ONCE OLD FANS SERVICE DEPRECATED + TYPES MOVED TO SDK
    T extends FanSubmission | PaginatedFanSubmission,
  >(submissions: Array<T>) {
    return submissions.map((s) => ({
      ...s,
      country_code: s.country_code || "",
      fan_id: s.id || "",
      module_id: s.module_id || "",
      phone_number: s.phone_number || "",
      email: s.email || "",
      date_of_birth: s.date_of_birth || "",
      full_name: s.full_name || "",
    }));
  }

  /**
   * Deprecated
   * @deprecated
   */
  getDCMorPresavesSubmissions(): Promise<any> {
    return fansServiceLegacy.getDCMorPresavesSubmissions();
  }
}
