function loginAsAdmin(): void {
  cy.visit('/login')

  cy.get('[data-cy="login-email"]').type('admin@rabbigerzi.test')
  cy.get('[data-cy="login-password"]').type('password123!@#')
  cy.get('[data-cy="login-submit"]').click()

  cy.url().should('eq', Cypress.config().baseUrl + '/')
  cy.getCookie('auth_token').should('exist')
}

describe('media page sets', () => {
  it('fetches and renders seeded set cards', () => {
    const introductionAudioTitle =
      'Fundamentals # 1: Living Our Way to a Wholesome Fulfilled Life (Audio)'

    cy.visit('/media')

    cy.get('header.site-header').within(() => {
      cy.contains('Torah Media').should('be.visible')
      cy.contains('About').should('be.visible')
      cy.contains('Donate').should('be.visible')
      cy.contains('a', 'Contact').should('not.exist')
    })
    cy.contains('h1', 'Torah Media').should('be.visible')
    cy.get('[data-cy="media-set-grid"]').should('be.visible')
    cy.get('[data-cy="media-set-card"]', { timeout: 10000 })
      .should('have.length', 2)

    cy.contains('[data-cy="media-set-card"]', 'Baderech HaAvodah')
      .as('baderechCard')
      .should('have.attr', 'href', '/element/1')
      .within(() => {
        cy.get('img[data-cy="media-set-icon"]')
          .should('be.visible')
          .and('have.attr', 'src')
          .and('include', '/assets/baderech-haavodah-icon.png')
        cy.contains('h2', 'Baderech HaAvodah').should('be.visible')
        cy.contains('a structured path for inner and outer growth')
          .should('be.visible')
      })

    cy.contains('[data-cy="media-set-card"]', 'Daily Learning')
      .as('dailyLearningCard')
      .should('match', 'article')
      .and('not.have.attr', 'href')

    cy.get('@dailyLearningCard')
      .within(() => {
        cy.contains('h2', 'Daily Learning').should('be.visible')
        cy.contains('Daily learning for steady growth').should('be.visible')
      })

    cy.get('@baderechCard').click()
    cy.location('pathname').should('eq', '/element/1')
    cy.get('[data-cy="element-page"]').should('be.visible')
    cy.contains('h1', 'Baderech HaAvodah').should('be.visible')
    cy.get('[data-cy="element-icon"]')
      .should('be.visible')
      .and('have.attr', 'src')
      .and('include', '/storage/element-icons/baderech-haavodah-icon.png')
    cy.get('[data-cy="element-rich-text"]').should('not.exist')
    cy.get('[data-cy="element-youtube-embed"]')
      .should('not.exist')
    cy.get('[data-cy="element-short-pdf-viewer-button"]').should('not.exist')
    cy.get('[data-cy="element-long-pdf-viewer-button"]').should('not.exist')
    cy.get('[data-cy="child-element-list"]').should('be.visible')
    cy.get('[data-cy="child-element-list"]')
      .should(
        'not.contain.text',
        'Begin with a clear map for avodah growth.',
      )
    cy.contains('[data-cy="child-element-link"]', '1. Introduction')
      .as('introductionLink')
      .should('have.attr', 'href', '/element/2')
    cy.get('@introductionLink')
      .should(
        'contain.text',
        'Your beliefs influence your thinking, and your thinking drives',
      )
    cy.contains('[data-cy="child-element-link"]', '2. Foundations')
      .should(
        'contain.text',
        'Focal points for thriving in a chaotic world.',
      )
    cy.contains('[data-cy="child-element-link"]', '3. Divine Plan')
      .should(
        'contain.text',
        'Understanding the unique greatness of our generation',
      )
    cy.contains(
      '[data-cy="child-element-link"]',
      '4. Architecture of the Soul',
    )
      .should(
        'contain.text',
        "Becoming a Baal Da'at - A Master of Inner Awareness",
      )
    cy.contains('[data-cy="child-element-link"]', '5. Arba Yesodot')
      .should('contain.text', 'Physical & Financial Health')
    cy.contains('[data-cy="child-element-link"]', '6. Fluid Integration')
      .should('contain.text', 'engender a shift in mindset')
      .and('have.attr', 'href', '/element/7')

    cy.contains('[data-cy="child-element-link"]', '1. Introduction')
      .click()
    cy.location('pathname').should('eq', '/element/2')
    cy.contains('h1', '1. Introduction').should('be.visible')
    cy.get('[data-cy="element-icon"]').should('not.exist')
    cy.get('[data-cy="element-rich-text"]')
      .should('contain.text', 'What is Baderech HaAvoda?')
      .and(
        'contain.text',
        'Why We Need a Path of Spiritual Practice',
      )
      .and(
        'contain.text',
        "A 'Baderech HaAvoda' is a way of living",
      )
      .and('contain.text', 'Direction')
      .and('contain.text', 'How do I grow?')
      .and('contain.text', 'The Challenge of Our Generation')
      .and('contain.text', 'So many are Hashkafikly homeless')
      .and('contain.text', 'the life we were created for.')
    cy.contains(
      '[data-cy="element-rich-text"] h2',
      'What is Baderech HaAvoda?',
    )
      .should('have.css', 'text-align', 'center')
    cy.get('[data-cy="element-youtube-embed"]').should('not.exist')
    cy.contains(
      '[data-cy="element-short-pdf-viewer-button"]',
      'View Short PDF',
    )
      .as('shortPdfButton')
      .should('be.visible')
    cy.get('[data-cy="element-long-pdf-viewer-button"]').should('not.exist')
    cy.get('@shortPdfButton').click()
    cy.get('[data-cy="pdf-viewer-modal"]').should('be.visible')
    cy.contains('[data-cy="pdf-viewer-title"]', 'Short PDF').should(
      'be.visible',
    )
    cy.get('[data-cy="pdf-viewer-document"]').should('be.visible')
    cy.get('[data-cy="pdf-viewer-page-status"]').should('be.visible')
    cy.get('[data-cy="pdf-viewer-previous-page"]').should('be.visible')
    cy.get('[data-cy="pdf-viewer-next-page"]').should('be.visible')
    cy.get('[data-cy="pdf-viewer-zoom-out"]').should('be.visible')
    cy.get('[data-cy="pdf-viewer-zoom-in"]').should('be.visible')
    cy.get('[data-cy="pdf-viewer-search-input"]').should('be.visible')
    cy.get('[data-cy="pdf-viewer-download"]').should('be.visible')
    cy.get('[data-cy="pdf-viewer-print"]').should('be.visible')
    cy.get('[data-cy="pdf-viewer-open-link"]')
      .should('be.visible')
      .and('have.attr', 'target', '_blank')
      .and('have.attr', 'href')
      .and('include', '/storage/element-pdfs/short/baderech.pdf')
    cy.get('[data-cy="pdf-viewer-close"]').click()
    cy.get('[data-cy="pdf-viewer-modal"]').should('not.exist')
    cy.contains('[data-cy="child-element-link"]', introductionAudioTitle)
      .as('introductionAudioLink')
      .should('have.attr', 'href', '/element/8')

    cy.get('@introductionAudioLink').click()
    cy.location('pathname').should('eq', '/element/8')
    cy.contains('h1', introductionAudioTitle).should('be.visible')
    cy.get('[data-cy="element-icon"]').should('not.exist')
    cy.get('[data-cy="element-rich-text"]').should('not.exist')
    cy.get('[data-cy="element-short-pdf-viewer-button"]').should('not.exist')
    cy.get('[data-cy="element-long-pdf-viewer-button"]').should('not.exist')
    cy.get('[data-cy="child-element-list"]').should('not.exist')
    cy.get('[data-cy="element-youtube-embed"]')
      .should('be.visible')
      .and('have.attr', 'src')
      .and('include', 'https://www.youtube.com/embed/yHx-r4p6hHU')
      .and('include', 'start=1')
  })

  it('does not show the create set card to logged-out users', () => {
    cy.visit('/media')

    cy.get('[data-cy="media-create-set-card"]').should('not.exist')
  })

  it('does not show set management actions to logged-out users', () => {
    cy.visit('/media')

    cy.get('[data-cy="media-set-edit"]').should('not.exist')
    cy.get('[data-cy="media-set-delete"]').should('not.exist')
  })

  it('creates a set from the logged-in media page modal', () => {
    loginAsAdmin()
    cy.visit('/media')
    cy.intercept('POST', /\/api\/sets$/).as('createSet')

    cy.get('[data-cy="media-create-set-card"]')
      .should('be.visible')
      .and('contain.text', 'Create another set')
      .click()
    cy.get('[data-cy="create-set-modal"]').should('be.visible')
    cy.get('[data-cy="create-set-name"]').type('New Cypress Set')
    cy.get('[data-cy="create-set-description"]')
      .type('Created through the media page modal')
    cy.get('[data-cy="create-set-icon"]')
      .selectFile('public/assets/baderech-haavodah-icon.png')
    cy.get('[data-cy="create-set-submit"]').click()
    cy.wait('@createSet')

    cy.location('pathname').should('match', /^\/admin\/element\/\d+$/)
    cy.get('[data-cy="admin-element-title"]')
      .should('have.value', 'New Cypress Set')
    cy.get('[data-cy="admin-element-description"]')
      .should('have.value', 'Created through the media page modal')
  })

  it('edits a set from the logged-in media page modal', () => {
    cy.resetDb()
    loginAsAdmin()
    cy.visit('/media')
    cy.intercept('POST', /\/api\/sets\/1\/update$/).as('updateSet')

    cy.contains('[data-cy="media-set-card"]', 'Baderech HaAvodah')
      .within(() => {
        cy.get('[data-cy="media-set-edit"]').click()
      })
    cy.get('[data-cy="edit-set-modal"]').should('be.visible')
    cy.get('[data-cy="edit-set-name"]')
      .clear()
      .type('Edited Cypress Set')
    cy.get('[data-cy="edit-set-description"]')
      .clear()
      .type('Edited through the set modal')
    cy.get('[data-cy="edit-set-submit"]').click()
    cy.wait('@updateSet')

    cy.get('[data-cy="edit-set-modal"]').should('not.exist')
    cy.contains('[data-cy="media-set-card"]', 'Edited Cypress Set')
      .should('be.visible')
      .and('contain.text', 'Edited through the set modal')
      .click()
    cy.location('pathname').should('eq', '/element/1')
    cy.contains('h1', 'Baderech HaAvodah').should('be.visible')

    cy.resetDb()
  })

  it('warns before deleting a set from the logged-in media page', () => {
    cy.resetDb()
    loginAsAdmin()
    cy.visit('/media')
    cy.intercept('DELETE', /\/api\/sets\/2$/).as('deleteSet')

    cy.contains('[data-cy="media-set-card"]', 'Daily Learning')
      .within(() => {
        cy.get('[data-cy="media-set-delete"]').click()
      })
    cy.get('[data-cy="delete-set-modal"]')
      .should('be.visible')
      .and('contain.text', 'Daily Learning')
      .and('contain.text', 'all elements and uploaded files')
    cy.get('[data-cy="delete-set-cancel"]').click()
    cy.get('[data-cy="delete-set-modal"]').should('not.exist')
    cy.contains('[data-cy="media-set-card"]', 'Daily Learning')
      .should('be.visible')

    cy.contains('[data-cy="media-set-card"]', 'Daily Learning')
      .within(() => {
        cy.get('[data-cy="media-set-delete"]').click()
      })
    cy.get('[data-cy="delete-set-confirm"]').click()
    cy.wait('@deleteSet')
    cy.get('[data-cy="delete-set-modal"]').should('not.exist')
    cy.contains('[data-cy="media-set-card"]', 'Daily Learning')
      .should('not.exist')

    cy.resetDb()
  })
})
