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

describe('Notifica e2e test', () => {
  const notificaPageUrl = '/notifica';
  const notificaPageUrlPattern = new RegExp('/notifica(\\?.*)?$');
  const username = Cypress.env('E2E_USERNAME') ?? 'user';
  const password = Cypress.env('E2E_PASSWORD') ?? 'user';
  const notificaSample = {};

  let notifica;

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

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

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

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

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

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

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

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

        cy.visit(notificaPageUrl);

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

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

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

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

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

        notifica = undefined;
      });
    });
  });

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

    it('should create an instance of Notifica', () => {
      cy.get(`[data-cy="tipoCanale"]`).select('EMAIL');

      cy.get(`[data-cy="tipoEvento"]`).select('PRENOTAZIONE_RIFIUTATA');

      cy.get(`[data-cy="messaggio"]`).type('brr ha');
      cy.get(`[data-cy="messaggio"]`).should('have.value', 'brr ha');

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

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

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

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

      cy.get(entityCreateSaveButtonSelector).click();

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