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

describe('ModelloLiberatoria e2e test', () => {
  const modelloLiberatoriaPageUrl = '/modello-liberatoria';
  const modelloLiberatoriaPageUrlPattern = new RegExp('/modello-liberatoria(\\?.*)?$');
  const username = Cypress.env('E2E_USERNAME') ?? 'user';
  const password = Cypress.env('E2E_PASSWORD') ?? 'user';
  const modelloLiberatoriaSample = {};

  let modelloLiberatoria;

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

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

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

  it('ModelloLiberatorias menu should load ModelloLiberatorias page', () => {
    cy.visit('/');
    cy.clickOnEntityMenuItem('modello-liberatoria');
    cy.wait('@entitiesRequest').then(({ response }) => {
      if (response?.body.length === 0) {
        cy.get(entityTableSelector).should('not.exist');
      } else {
        cy.get(entityTableSelector).should('exist');
      }
    });
    cy.getEntityHeading('ModelloLiberatoria').should('exist');
    cy.url().should('match', modelloLiberatoriaPageUrlPattern);
  });

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

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

    describe('with existing value', () => {
      beforeEach(() => {
        cy.authenticatedRequest({
          method: 'POST',
          url: '/api/modello-liberatorias',
          body: modelloLiberatoriaSample,
        }).then(({ body }) => {
          modelloLiberatoria = body;

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

        cy.visit(modelloLiberatoriaPageUrl);

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

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

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

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

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

        modelloLiberatoria = undefined;
      });
    });
  });

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

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

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

      cy.setFieldImageAsBytesOfEntity('documento', 'integration-test.png', 'image/png');

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

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

      // since cypress clicks submit too fast before the blob fields are validated
      cy.wait(200); // eslint-disable-line cypress/no-unnecessary-waiting
      cy.get(entityCreateSaveButtonSelector).click();

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