/* eslint no-console:"off" */

/**
 * This script demonstrates how to create and update asset
 *
 * Run script with: `DEBUG=@ocap/client node examples/asset.js`
 */

const { fromRandom } = require('@ocap/wallet');
const GraphQLClient = require('../lib/node');

const endpoint = process.env.OCAP_API_HOST || 'http://127.0.0.1:4000'; // testnet

const client = new GraphQLClient(`${endpoint}/api`);
const sleep = (timeout) =>
  new Promise((resolve) => {
    setTimeout(resolve, timeout);
  });

(async () => {
  try {
    const owner = fromRandom();
    console.log({ owner: owner.address });

    // 1. declare owner
    let hash = await client.declare({ moniker: 'owner', wallet: owner });
    console.log('declare.owner.result', hash);
    console.log('view owner account', `${endpoint}/explorer/accounts/${owner.address}`);

    // 2. create asset for owner
    let assetAddress;
    [hash, assetAddress] = await client.createAsset({
      moniker: 'asset',
      readonly: false, // if we want to update the asset, we should set this to false
      transferrable: false,
      data: {
        type: 'json',
        value: {
          key: 'value',
          sn: Math.random(),
        },
      },
      wallet: owner,
    });
    console.log('view asset state', `${endpoint}/explorer/assets/${assetAddress}`);
    console.log('create asset tx', `${endpoint}/explorer/txs/${hash}`);

    // wait for asset state consolidates
    await sleep(5000);

    // 3. read asset
    const { state } = await client.getAssetState({ address: assetAddress });
    console.log('asset state', state);

    // 4. update asset
    hash = await client.updateAsset({
      address: assetAddress,
      moniker: 'asset_updated',
      data: {
        type: 'json',
        value: {
          key: 'value updated',
          sn: Math.random(),
        },
      },
      wallet: owner,
    });
    console.log('view asset state', `${endpoint}/explorer/assets/${assetAddress}`);
    console.log('update asset tx', `${endpoint}/explorer/txs/${hash}`);
  } catch (err) {
    console.error(err);
    console.log(JSON.stringify(err.errors));
  }
})();
