describe('Body - Period (身体-经期)', () => {
  beforeEach(() => {
    cy.visit('/body', {
      onBeforeLoad(win) {
        win.localStorage.setItem('sp_login_expires', String(Date.now() + 86400000))
      }
    })
    cy.get('.sub-tab').contains('经期').click()
  })

  it('shows period section with add button', () => {
    cy.contains('经期记录').should('be.visible')
    cy.get('.btn-accent').should('contain', '记录')
  })

  it('opens add period form', () => {
    cy.get('.btn-accent').contains('记录').click()
    cy.get('.edit-form').should('be.visible')
    cy.get('.edit-form input[type="date"]').should('have.length', 2)
  })

  it('creates a period record', () => {
    cy.get('.btn-accent').contains('记录').click()
    cy.get('.edit-form input[type="date"]').first().type('2026-04-01')
    cy.get('.edit-form input[type="date"]').eq(1).type('2026-04-05')
    cy.get('.btn-accent').contains('保存').click()
    cy.get('.record-card').should('contain', '2026-04-01')
    cy.get('.record-card').should('contain', '2026-04-05')
  })

  it('creates period record without end date', () => {
    cy.get('.btn-accent').contains('记录').click()
    cy.get('.edit-form input[type="date"]').first().type('2026-04-07')
    cy.get('.btn-accent').contains('保存').click()
    cy.get('.record-card').should('contain', '进行中')
  })

  it('deletes a period record', () => {
    cy.get('.btn-accent').contains('记录').click()
    cy.get('.edit-form input[type="date"]').first().type('2026-03-01')
    cy.get('.btn-accent').contains('保存').click()
    cy.get('.record-card').contains('2026-03-01').parent().find('.remove-btn').click()
    cy.get('.record-card').should('not.contain', '2026-03-01')
  })
})
