/*
 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 {
  AmountString,
  Duration,
  FlightRecordEvent,
  TalerWireGatewayHttpClient,
} from "@gnu-taler/taler-util";
import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
import { defaultCoinConfig } from "../harness/denomStructures.js";
import {
  applyTimeTravelV2,
  createSimpleTestkudosEnvironmentV3,
} from "../harness/environments.js";
import { GlobalTestState } from "../harness/harness.js";

export async function runWalletWithdrawalRedenominateTest(t: GlobalTestState) {
  // Set up test environment

  const {
    walletClient,
    bankClient,
    bank,
    exchange,
    merchant,
    merchantAdminAccessToken,
    exchangeBankAccount,
  } = await createSimpleTestkudosEnvironmentV3(
    t,
    defaultCoinConfig.map((x) => x("TESTKUDOS")),
    {
      walletConfig: {
        testing: {
          devModeActive: true,
        },
      },
    },
  );

  {
    await walletClient.call(WalletApiOperation.ClearDb, {});
    const recs = await walletClient.call(
      WalletApiOperation.TestingGetFlightRecords,
      {},
    );
    t.assertDeepEqual(recs.flightRecords.length, 0);
  }

  {
    const wres = await walletClient.call(
      WalletApiOperation.AcceptManualWithdrawal,
      {
        exchangeBaseUrl: exchange.baseUrl,
        amount: "TESTKUDOS:10" as AmountString,
      },
    );

    await walletClient.call(
      WalletApiOperation.TestingCorruptWithdrawalCoinSel,
      {
        transactionId: wres.transactionId,
      },
    );

    const reservePub: string = wres.reservePub;
    const user = await bankClient.createRandomBankUser();

    const wireGatewayApiClient = new TalerWireGatewayHttpClient(
      exchangeBankAccount.wireGatewayApiBaseUrl,
    );

    await wireGatewayApiClient.addIncoming({
      auth: bank.getAdminAuth(),
      body: {
        amount: "TESTKUDOS:10",
        debit_account: user.accountPaytoUri,
        reserve_pub: reservePub,
      },
    });

    await walletClient.call(
      WalletApiOperation.TestingWaitTransactionsFinal,
      {},
    );

    const recs = await walletClient.call(
      WalletApiOperation.TestingGetFlightRecords,
      {},
    );

    const myRec = recs.flightRecords.find(
      (x) => x.event === FlightRecordEvent.WithdrawalRedenominate,
    );
    t.assertTrue(myRec != null);
  }

  {
    await walletClient.call(WalletApiOperation.ClearDb, {});
    const recs = await walletClient.call(
      WalletApiOperation.TestingGetFlightRecords,
      {},
    );
    t.assertDeepEqual(recs.flightRecords.length, 0);
  }

  {
    const wres = await walletClient.call(
      WalletApiOperation.AcceptManualWithdrawal,
      {
        exchangeBaseUrl: exchange.baseUrl,
        amount: "TESTKUDOS:10" as AmountString,
      },
    );

    const reservePub: string = wres.reservePub;
    const user = await bankClient.createRandomBankUser();

    const wireGatewayApiClient = new TalerWireGatewayHttpClient(
      exchangeBankAccount.wireGatewayApiBaseUrl,
    );

    await applyTimeTravelV2(
      Duration.toMilliseconds(
        Duration.fromSpec({
          days: 14,
        }),
      ),
      {
        exchange,
        merchant,
        walletClient,
      },
    );

    await wireGatewayApiClient.addIncoming({
      auth: bank.getAdminAuth(),
      body: {
        amount: "TESTKUDOS:10",
        debit_account: user.accountPaytoUri,
        reserve_pub: reservePub,
      },
    });

    await walletClient.call(
      WalletApiOperation.TestingWaitTransactionsFinal,
      {},
    );

    const recs = await walletClient.call(
      WalletApiOperation.TestingGetFlightRecords,
      {},
    );

    const myRec = recs.flightRecords.find(
      (x) => x.event === FlightRecordEvent.WithdrawalRedenominate,
    );
    t.assertTrue(myRec != null);
  }
}

runWalletWithdrawalRedenominateTest.suites = ["wallet"];
