import { Timeouts } from '@utility-lib/index';

export class StripeValidation {
  static validateStripeFee2(bookingId: string, amount: string) {
    cy.allure().logStep('Getting amounts from Stripe payment Intents');
    cy.exec(
      `curl -G https://api.stripe.com/v1/payment_intents/search \\
                  -H "Stripe-Version: 2020-08-27" \\
                  -u ${Cypress.env('stripeKey')}: \\
                  -d query="metadata['bookingId']:'${bookingId}'"`
    ).then((result) => {
      expect(
        // JSON.parse(JSON.parse(JSON.stringify(result)).stdout).data[0].amount
        JSON.parse(JSON.parse(JSON.stringify(result)).stdout).data[0].metadata
          .localIssuingAmount[0]
      ).to.equal(amount);
    });
  }

  static validateStripeFee() {
    function formatDollarsToCents(value: string) {
      value = (value + '').replace(/[^\d.-]/g, '');
      if (value && value.includes('.')) {
        value = value.substring(0, value.indexOf('.') + 3);
      }
      return value ? Math.round(parseFloat(value) * 100) : 0;
    }

    cy.task('getDataFromCache', 'bookingID').then((bookingID) => {
      cy.task<string>('getDataFromCache', 'tripFee').then((tripFee) => {
        cy.log(`BokingID and Trip Fee --> ${bookingID} and ${tripFee}`);
        cy.allure().logStep('Getting Fee from Stripe payment Intents');
        cy.exec(
          `curl -G https://api.stripe.com/v1/payment_intents/search -H "Stripe-Version: 2020-08-27" -u ${Cypress.env(
            'stripeKey'
          )}: -d query="metadata['bookingId']:'${bookingID}'"`
        ).then((result) => {
          cy.log(JSON.stringify(result));
          cy.wait(Timeouts.SHORT_TIMEOUT_5_SEC.timeout);
          expect(
            JSON.parse(JSON.parse(JSON.stringify(result)).stdout).data[0].amount
          ).to.equal(formatDollarsToCents(tripFee));
        });
      });
    });
  }
  static validateStripeFeeByBookingID(bookingID: string, tripFee: string) {
    function formatDollarsToCents(value: string) {
      value = (value + '').replace(/[^\d.-]/g, '');
      if (value?.includes('.')) {
        value = value.substring(0, value.indexOf('.') + 3);
      }
      return value ? Math.round(parseFloat(value) * 100) : 0;
    }
    cy.allure().logStep('Getting Fee from Stripe payment Intents');
    cy.log(`BokingID and Trip Fee --> ${bookingID} and ${tripFee}`);
    cy.exec(
      `curl -G https://api.stripe.com/v1/payment_intents/search -H "Stripe-Version: 2020-08-27" -u ${Cypress.env(
        'stripeKey'
      )}: -d query="metadata['bookingId']:'${bookingID}'"`
    ).then((result) => {
      cy.log(JSON.stringify(result));
      cy.wait(Timeouts.SHORT_TIMEOUT_5_SEC.timeout);
      expect(
        JSON.parse(JSON.parse(JSON.stringify(result)).stdout).data[0].amount
      ).to.equal(formatDollarsToCents(tripFee));
    });
  }
}
