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

describe('Liberatoria e2e test', () => {
  const liberatoriaPageUrl = '/liberatoria';
  const liberatoriaPageUrlPattern = new RegExp('/liberatoria(\\?.*)?$');
  const username = Cypress.env('E2E_USERNAME') ?? 'user';
  const password = Cypress.env('E2E_PASSWORD') ?? 'user';
  const liberatoriaSample = {};

  let liberatoria;

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

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

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

  it('Liberatorias menu should load Liberatorias page', () => {
    cy.visit('/');
    cy.clickOnEntityMenuItem('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('Liberatoria').should('exist');
    cy.url().should('match', liberatoriaPageUrlPattern);
  });

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

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

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

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

        cy.visit(liberatoriaPageUrl);

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

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

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

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

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

        liberatoria = undefined;
      });
    });
  });

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

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

      cy.get(entityCreateSaveButtonSelector).click();

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