import ShopifyService from "./ShopifyService";
import { Api } from "./api";
import { ReorderShopifyStoreRequest } from "models/talent/talent-profile-module.model";
import Shopify from "@komi-app/shopify-sdk";

import config from "config";
import { FLAGS, isOn } from "@komi-app/flags-sdk";

// NOTE: komi's shopify service is currently not supported in v1

export default class ShopifyServiceV1 extends ShopifyService {
  private sdk: Shopify;

  constructor(api: Api) {
    super(api);

    const useApiGateway = isOn(FLAGS.USE_API_GATEWAY)
    const shopifyUrl = useApiGateway
      ? config.shopify.gatewayUrl
      : config.shopify.url
    // const authUrl = useApiGateway
    //   ? config.auth.gatewayUrl
    //   : config.auth.url

    this.sdk = new Shopify({      
      serviceName: config.service.name,
      serviceVersion: config.service.version,
      targetUrl: shopifyUrl
    });
  }

  init = (profileId: string) => {
    this.sdk.init(profileId);
  };

  getAuthToken = (): Promise<string> => {
    return this.sdk.rye.getToken();
  };

  getRyeShopifyStores = async (talentProfileId: string): Promise<any> => {
    const stores = await this.sdk.rye.getStores();
    return {
      ok: true,
      response: {
        result: {
          stores,
        },
      },
    };
  };

  reorderRyeShopifyStore = (
    payload: ReorderShopifyStoreRequest[]
  ): Promise<any> => {
    throw new Error("Not implemented");
    //return this.api.putRequest(`/rye/shopify/token/re-order`, payload);
  };

  createRyeShopifyStore = async (
    talentProfileId: string,
    storeDomain: string
  ): Promise<any> => {
    await this.sdk.rye.createStore(storeDomain);

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

  updateRyeShopifyStoreStatus = async (
    talentProfileId: string,
    storeDomain: string,
    connected: boolean
  ): Promise<any> => {
    await this.sdk.rye.updateStore(storeDomain, {
      connected,
    });

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

  updateRyeShopifyStoreName = async (
    talentProfileId: string,
    storeDomain: string,
    name: string
  ): Promise<any> => {
    await this.sdk.rye.updateStore(storeDomain, {
      name,
    });

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

  removeRyeShopifyStore = async (
    talentProfileId: string,
    storeDomain: string
  ): Promise<any> => {
    await this.sdk.rye.deleteStore(storeDomain);

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