import { Timeouts } from '@utility-lib/constants';
import { LoginPage } from '@admin-lib/index';
import { HotelInterceptApis } from '@hotel-lib/index';
import { createBookHotelData } from '@fixtures/hotel/booking_data';
import { User_Type } from '@support-onboard/index';
import { ChatApiIntercept, ChatBox } from '@chat-lib/index';
import { HelpCenter } from '@helpcenter-lib/helpcenter';

const csatSelectors = {
  csatScore: (score: number) => `[data-testid="csat"] [aria-label="${score}"]`,
  csatEmojiScore: (score: number) =>
    `[data-testid="csat"] [data-role="ta-chat-ratings"] div button:nth-child(${score})`,
  csatRemarkInput: '[data-testid="textArea__input"]',
  csatRemarkSubmitButton: '[data-testid="textArea"] [type="submit"]',
  csatScoreSubmitted: '[data-testid="labelMessage"]',
  csatScoreButtons: '[data-role="ta-chat-ratings-level"]',
  chatRatingButtonParent: '[data-role="ta-chat-ratings"]',
  skipCsatButton: '[data-testid="csat"] button'
};

export class CSat {
  static submitCSatScore(score: number) {
    cy.wait(Timeouts.SHORT_TIMEOUT_5_SEC.timeout);
    cy.allure().logStep(`Submit CSAT score ${score}`);
    cy.get(csatSelectors.csatScore(score)).click();
    cy.wait('@waitForCsatScore').then((Interception) => {
      expect(Interception.response?.statusCode).to.equal(200);
      expect(Interception.response?.body.score).to.equal(score);
    });
  }

  static submitCSatEmojiScore(score: number) {
    cy.wait(Timeouts.SHORT_TIMEOUT_5_SEC.timeout);
    cy.allure().logStep(`Submit CSAT emoji score ${score}`);
    cy.get(csatSelectors.csatEmojiScore(score)).click();
    cy.wait('@waitForCsatScore').then((Interception) => {
      expect(Interception.response?.statusCode).to.equal(200);
      expect(Interception.response?.body.score).to.equal(score);
    });
  }

  static expectRatingButtonsToBeDisabled() {
    cy.get(csatSelectors.chatRatingButtonParent).within(() => {
      cy.get(csatSelectors.csatScoreButtons).should('be.disabled');
    });
    cy.allure().logStep(`Rating buttons are disabled`);
  }

  static expectCSatScoreSubmitted(score: number) {
    cy.get(csatSelectors.csatScoreSubmitted).should(
      'contain.text',
      `You gave Ava ${score} points`
    );
    cy.allure().logStep(`CSAT score submitted`);
  }

  static expectCSatEmojiScoreSubmitted() {
    cy.get(csatSelectors.csatScoreSubmitted).should(
      'contain.text',
      'You rated your experience with Ava as'
    );
    cy.allure().logStep(`CSAT emoji score submitted`);
  }

  static expectCSatRemarkSubmitted() {
    cy.get(csatSelectors.csatRemarkInput).type('Great work, Ava!');
    cy.get(csatSelectors.csatRemarkSubmitButton).click();
    cy.wait('@waitForCsatRemark').then((Interception) => {
      expect(Interception.response?.statusCode).to.equal(200);
      expect(Interception.response?.body.remark).to.equal('Great work, Ava!');
    });
    cy.allure().logStep(`CSAT remark submitted`);
  }

  static prepareBookingAndCompanyCreation() {
    HotelInterceptApis.interceptHotelApis();
    ChatApiIntercept.interceptChatApis();
    LoginPage.createNewCompanyAndLoginWithUserByType({
      users: [User_Type.ADMIN],
      companySuffix: '-avacsate2etest'
    });
    LoginPage.createHotelBookingForOnBoardingCompany(createBookHotelData());
  }

  static prepareMsgForCsatFeedback() {
    HelpCenter.visitPage();
    ChatBox.openChatWithAva();
    ChatBox.replyToAva('No');
    ChatBox.replyToAva('Upcoming trips');
    ChatBox.replyToAva('Invoice and charges');
    ChatBox.replyToAva(`I’m done`);
  }

  static skipAndEndChat() {
    cy.get(csatSelectors.skipCsatButton).contains('Skip and end chat').click();
    cy.wait('@waitForCsatSkip');
    cy.allure().logStep(`Skip CSAT score`);
  }

  static skipAndConnetWithAgent() {
    cy.get(csatSelectors.skipCsatButton)
      .contains('Skip and connect with an agent')
      .click();
    cy.wait('@waitForCsatSkip');
    cy.allure().logStep(`Skip CSAT score`);
  }
}
