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

describe('Prenotazione e2e test', () => {
  const prenotazionePageUrl = '/prenotazione';
  const prenotazionePageUrlPattern = new RegExp('/prenotazione(\\?.*)?$');
  const username = Cypress.env('E2E_USERNAME') ?? 'user';
  const password = Cypress.env('E2E_PASSWORD') ?? 'user';
  const prenotazioneSample = {};

  let prenotazione;

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

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

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

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

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

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

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

          cy.intercept(
            {
              method: 'GET',
              url: '/api/prenotaziones+(?*|)',
              times: 1,
            },
            {
              statusCode: 200,
              headers: {
                link: '<http://localhost/api/prenotaziones?page=0&size=20>; rel="last",<http://localhost/api/prenotaziones?page=0&size=20>; rel="first"',
              },
              body: [prenotazione],
            },
          ).as('entitiesRequestInternal');
        });

        cy.visit(prenotazionePageUrl);

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

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

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

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

      it('last delete button click should delete instance of Prenotazione', () => {
        cy.get(entityDeleteButtonSelector).last().click();
        cy.getEntityDeleteDialogHeading('prenotazione').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', prenotazionePageUrlPattern);

        prenotazione = undefined;
      });
    });
  });

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

    it('should create an instance of Prenotazione', () => {
      cy.get(`[data-cy="oraInizio"]`).type('2025-12-10T05:00');
      cy.get(`[data-cy="oraInizio"]`).blur();
      cy.get(`[data-cy="oraInizio"]`).should('have.value', '2025-12-10T05:00');

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

      cy.get(`[data-cy="stato"]`).select('ANNULLATA');

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

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

      cy.get(`[data-cy="noteUtente"]`).type('gah while ick');
      cy.get(`[data-cy="noteUtente"]`).should('have.value', 'gah while ick');

      cy.get(entityCreateSaveButtonSelector).click();

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