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

describe('Struttura e2e test', () => {
  const strutturaPageUrl = '/struttura';
  const strutturaPageUrlPattern = new RegExp('/struttura(\\?.*)?$');
  const username = Cypress.env('E2E_USERNAME') ?? 'user';
  const password = Cypress.env('E2E_PASSWORD') ?? 'user';
  const strutturaSample = {};

  let struttura;

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

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

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

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

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

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

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

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

        cy.visit(strutturaPageUrl);

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

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

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

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

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

        struttura = undefined;
      });
    });
  });

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

    it('should create an instance of Struttura', () => {
      cy.get(`[data-cy="nome"]`).type('finally how');
      cy.get(`[data-cy="nome"]`).should('have.value', 'finally how');

      cy.get(`[data-cy="descrizione"]`).type('immediately supposing');
      cy.get(`[data-cy="descrizione"]`).should('have.value', 'immediately supposing');

      cy.get(`[data-cy="indirizzo"]`).type('elevation brush yieldingly');
      cy.get(`[data-cy="indirizzo"]`).should('have.value', 'elevation brush yieldingly');

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

      cy.get(`[data-cy="attiva"]`).should('not.be.checked');
      cy.get(`[data-cy="attiva"]`).click();
      cy.get(`[data-cy="attiva"]`).should('be.checked');

      cy.get(`[data-cy="fotoUrl"]`).type('where mmm');
      cy.get(`[data-cy="fotoUrl"]`).should('have.value', 'where mmm');

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

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

      cy.get(entityCreateSaveButtonSelector).click();

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