/*
 This file is part of GNU Taler
 (C) 2020 Taler Systems S.A.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

/**
 * Imports.
 */
import {
  Duration,
  TalerExchangeHttpClient,
  TalerMerchantApi,
  TalerMerchantInstanceHttpClient,
  j2s,
  succeedOrThrow,
} from "@gnu-taler/taler-util";
import { createSimpleTestkudosEnvironmentV3 } from "../harness/environments.js";
import { GlobalTestState } from "../harness/harness.js";

/**
 * Test APIs related to merchant wire transfers.
 */
export async function runMerchantBankBadWireTargetTest(t: GlobalTestState) {
  // Set up test environment

  const { exchange, merchant, merchantAdminAccessToken } =
    await createSimpleTestkudosEnvironmentV3(t);

  // Create instance with bad wire target.
  // x-taler-bank will work but iban doesn't since
  // exchange only allows x-taler-bank
  const { accessToken } = await merchant.addInstanceWithWireAccount(
    {
      id: "minst2",
      name: "minst2",
      // paytoUris: [`payto://x-taler-bank/localhost/2?receiver-name=random`],
      paytoUris: [`payto://iban/DE1231231231?receiver-name=random`],
      defaultWireTransferDelay: Duration.toTalerProtocolDuration(
        Duration.fromSpec({ minutes: 1 }),
      ),
    },
    { adminAccessToken: merchantAdminAccessToken },
  );

  const exchangeClient = new TalerExchangeHttpClient(exchange.baseUrl);

  const keys = succeedOrThrow(await exchangeClient.getKeys());

  // KYC should be disabled
  t.assertTrue(!keys.kyc_enabled);

  const merchantClient = new TalerMerchantInstanceHttpClient(
    merchant.makeInstanceBaseUrl("minst2"),
  );

  const d = await merchantClient.getCurrentInstanceKycStatus(accessToken);
  t.assertDeepEqual(d.type, "ok");
  const kycStatus = d.body;

  t.assertTrue(!!kycStatus);

  console.log(j2s(kycStatus));
  t.assertDeepEqual(kycStatus.kyc_data.length, 1);
  t.assertDeepEqual(
    kycStatus.kyc_data[0].status,
    TalerMerchantApi.MerchantAccountKycStatus.UNSUPPORTED_ACCOUNT,
  );
}

runMerchantBankBadWireTargetTest.suites = ["merchant"];
