import {
  entityConfirmDeleteButtonSelector,
  entityCreateButtonSelector,
  entityCreateCancelButtonSelector,
  entityCreateSaveButtonSelector,
  entityDeleteButtonSelector,
  entityDetailsBackButtonSelector,
  entityDetailsButtonSelector,
  entityEditButtonSelector,
  entityTableSelector,
} from '../../support/entity';

describe('Messaggio e2e test', () => {
  const messaggioPageUrl = '/messaggio';
  const messaggioPageUrlPattern = new RegExp('/messaggio(\\?.*)?$');
  const username = Cypress.env('E2E_USERNAME') ?? 'user';
  const password = Cypress.env('E2E_PASSWORD') ?? 'user';
  const messaggioSample = {};

  let messaggio;

  beforeEach(() => {
    cy.login(username, password);
  });

  beforeEach(() => {
    cy.intercept('GET', '/api/messaggios+(?*|)').as('entitiesRequest');
    cy.intercept('POST', '/api/messaggios').as('postEntityRequest');
    cy.intercept('DELETE', '/api/messaggios/*').as('deleteEntityRequest');
  });

  afterEach(() => {
    if (messaggio) {
      cy.authenticatedRequest({
        method: 'DELETE',
        url: `/api/messaggios/${messaggio.id}`,
      }).then(() => {
        messaggio = undefined;
      });
    }
  });

  it('Messaggios menu should load Messaggios page', () => {
    cy.visit('/');
    cy.clickOnEntityMenuItem('messaggio');
    cy.wait('@entitiesRequest').then(({ response }) => {
      if (response?.body.length === 0) {
        cy.get(entityTableSelector).should('not.exist');
      } else {
        cy.get(entityTableSelector).should('exist');
      }
    });
    cy.getEntityHeading('Messaggio').should('exist');
    cy.url().should('match', messaggioPageUrlPattern);
  });

  describe('Messaggio page', () => {
    describe('create button click', () => {
      beforeEach(() => {
        cy.visit(messaggioPageUrl);
        cy.wait('@entitiesRequest');
      });

      it('should load create Messaggio page', () => {
        cy.get(entityCreateButtonSelector).click();
        cy.url().should('match', new RegExp('/messaggio/new$'));
        cy.getEntityCreateUpdateHeading('Messaggio');
        cy.get(entityCreateSaveButtonSelector).should('exist');
        cy.get(entityCreateCancelButtonSelector).click();
        cy.wait('@entitiesRequest').then(({ response }) => {
          expect(response?.statusCode).to.equal(200);
        });
        cy.url().should('match', messaggioPageUrlPattern);
      });
    });

    describe('with existing value', () => {
      beforeEach(() => {
        cy.authenticatedRequest({
          method: 'POST',
          url: '/api/messaggios',
          body: messaggioSample,
        }).then(({ body }) => {
          messaggio = body;

          cy.intercept(
            {
              method: 'GET',
              url: '/api/messaggios+(?*|)',
              times: 1,
            },
            {
              statusCode: 200,
              body: [messaggio],
            },
          ).as('entitiesRequestInternal');
        });

        cy.visit(messaggioPageUrl);

        cy.wait('@entitiesRequestInternal');
      });

      it('detail button click should load details Messaggio page', () => {
        cy.get(entityDetailsButtonSelector).first().click();
        cy.getEntityDetailsHeading('messaggio');
        cy.get(entityDetailsBackButtonSelector).click();
        cy.wait('@entitiesRequest').then(({ response }) => {
          expect(response?.statusCode).to.equal(200);
        });
        cy.url().should('match', messaggioPageUrlPattern);
      });

      it('edit button click should load edit Messaggio page and go back', () => {
        cy.get(entityEditButtonSelector).first().click();
        cy.getEntityCreateUpdateHeading('Messaggio');
        cy.get(entityCreateSaveButtonSelector).should('exist');
        cy.get(entityCreateCancelButtonSelector).click();
        cy.wait('@entitiesRequest').then(({ response }) => {
          expect(response?.statusCode).to.equal(200);
        });
        cy.url().should('match', messaggioPageUrlPattern);
      });

      it('edit button click should load edit Messaggio page and save', () => {
        cy.get(entityEditButtonSelector).first().click();
        cy.getEntityCreateUpdateHeading('Messaggio');
        cy.get(entityCreateSaveButtonSelector).click();
        cy.wait('@entitiesRequest').then(({ response }) => {
          expect(response?.statusCode).to.equal(200);
        });
        cy.url().should('match', messaggioPageUrlPattern);
      });

      it('last delete button click should delete instance of Messaggio', () => {
        cy.get(entityDeleteButtonSelector).last().click();
        cy.getEntityDeleteDialogHeading('messaggio').should('exist');
        cy.get(entityConfirmDeleteButtonSelector).click();
        cy.wait('@deleteEntityRequest').then(({ response }) => {
          expect(response?.statusCode).to.equal(204);
        });
        cy.wait('@entitiesRequest').then(({ response }) => {
          expect(response?.statusCode).to.equal(200);
        });
        cy.url().should('match', messaggioPageUrlPattern);

        messaggio = undefined;
      });
    });
  });

  describe('new Messaggio page', () => {
    beforeEach(() => {
      cy.visit(messaggioPageUrl);
      cy.get(entityCreateButtonSelector).click();
      cy.getEntityCreateUpdateHeading('Messaggio');
    });

    it('should create an instance of Messaggio', () => {
      cy.get(`[data-cy="spedito"]`).type('2025-12-10T09:25');
      cy.get(`[data-cy="spedito"]`).blur();
      cy.get(`[data-cy="spedito"]`).should('have.value', '2025-12-10T09:25');

      cy.get(`[data-cy="testo"]`).type('for');
      cy.get(`[data-cy="testo"]`).should('have.value', 'for');

      cy.get(`[data-cy="letto"]`).type('2025-12-10T02:14');
      cy.get(`[data-cy="letto"]`).blur();
      cy.get(`[data-cy="letto"]`).should('have.value', '2025-12-10T02:14');

      cy.get(entityCreateSaveButtonSelector).click();

      cy.wait('@postEntityRequest').then(({ response }) => {
        expect(response?.statusCode).to.equal(201);
        messaggio = response.body;
      });
      cy.wait('@entitiesRequest').then(({ response }) => {
        expect(response?.statusCode).to.equal(200);
      });
      cy.url().should('match', messaggioPageUrlPattern);
    });
  });
});
