describe('admin login page', () => {
  it('renders the login form with email, password, and submit button', () => {
    cy.visit('/login')

    cy.get('[data-cy="login-email"]').should('be.visible')
    cy.get('[data-cy="login-password"]').should('be.visible')
    cy.get('[data-cy="login-submit"]').should('be.visible')
  })

  it('is not linked from the home page', () => {
    cy.visit('/')

    cy.contains('login').should('not.exist')
    cy.contains('Login').should('not.exist')
    cy.contains('admin').should('not.exist')
    cy.contains('Admin').should('not.exist')
    cy.get('a[href="/login"]').should('not.exist')
  })

  it('shows an error on failed login', () => {
    cy.visit('/login')

    cy.get('[data-cy="login-email"]').type('bad@example.com')
    cy.get('[data-cy="login-password"]').type('wrongpassword')
    cy.get('[data-cy="login-submit"]').click()

    cy.get('[data-cy="login-error"]').should('be.visible')
  })

  it('is not linked from any navigation element on the home page', () => {
    cy.visit('/')

    cy.get('header a').each(($link) => {
      cy.wrap($link).should('not.have.attr', 'href', '/login')
    })
  })

  it('logs in and logs out from the home page navbar', () => {
    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')
    cy.get('[data-cy="navbar-logout"]').should('be.visible')
    cy.get('[data-cy="navbar-logout"]').click()

    cy.getCookie('auth_token').should('not.exist')
    cy.get('[data-cy="navbar-logout"]').should('not.exist')
  })
})
