describe('Advanced Search Features', () => {
  beforeEach(() => {
    cy.visit('/')
    cy.get('.recipe-card', { timeout: 10000 }).should('have.length.gte', 1)
  })

  it('search input accepts text and app stays stable', () => {
    cy.get('input[placeholder*="搜索"]').type('酸痛')
    cy.wait(500)
    // App should remain functional
    cy.get('.main').should('be.visible')
    cy.get('input[placeholder*="搜索"]').should('have.value', '酸痛')
  })

  it('searches by partial recipe name', () => {
    cy.get('input[placeholder*="搜索"]').type('安睡')
    cy.wait(500)
    cy.get('.recipe-card').should('have.length.gte', 0)
  })

  it('returns fewer results for nonsense query', () => {
    cy.get('.recipe-card').then($all => {
      const total = $all.length
      cy.get('input[placeholder*="搜索"]').type('xyzabcnonexistent')
      cy.wait(500)
      // Should show empty state or fewer results
      cy.get('.recipe-card').should('have.length.lte', total)
    })
  })

  it('search is case-insensitive for latin chars', () => {
    cy.get('input[placeholder*="搜索"]').type('doterra')
    cy.wait(500)
    // Just verify no crash
    cy.get('.main').should('be.visible')
  })

  it('handles special characters in search', () => {
    cy.get('input[placeholder*="搜索"]').type('（）【】')
    cy.wait(300)
    cy.get('.main').should('be.visible')
  })

  it('rapid typing updates results without crash', () => {
    const input = cy.get('input[placeholder*="搜索"]')
    input.type('薰')
    cy.wait(100)
    input.type('衣')
    cy.wait(100)
    input.type('草')
    cy.wait(300)
    cy.get('.recipe-card').should('have.length.gte', 0)
    cy.get('.main').should('be.visible')
  })

  it('clearing search with button restores all recipes', () => {
    cy.get('.recipe-card').then($initial => {
      const count = $initial.length
      cy.get('input[placeholder*="搜索"]').type('薰衣草')
      cy.wait(300)
      // Clear
      cy.get('input[placeholder*="搜索"]').clear()
      cy.wait(300)
      cy.get('.recipe-card').should('have.length', count)
    })
  })
})
