describe('Audit Log Advanced', () => {
  const ADMIN_TOKEN = 'c86ae7afbe10fabe3c1d5e1a7fee74feaadfd5dc7be2ab62'
  const authHeaders = { Authorization: `Bearer ${ADMIN_TOKEN}` }

  it('fetches audit logs with pagination', () => {
    cy.request({ url: '/api/audit-log?limit=10&offset=0', headers: authHeaders }).then(res => {
      expect(res.status).to.eq(200)
      expect(res.body).to.be.an('array')
      expect(res.body.length).to.be.lte(10)
    })
  })

  it('audit log entries have required fields', () => {
    cy.request({ url: '/api/audit-log?limit=5&offset=0', headers: authHeaders }).then(res => {
      if (res.body.length > 0) {
        const entry = res.body[0]
        expect(entry).to.have.property('action')
        expect(entry).to.have.property('created_at')
      }
    })
  })

  it('pagination works (offset returns different records)', () => {
    cy.request({ url: '/api/audit-log?limit=5&offset=0', headers: authHeaders }).then(res1 => {
      if (res1.body.length < 5) return // not enough data
      cy.request({ url: '/api/audit-log?limit=5&offset=5', headers: authHeaders }).then(res2 => {
        if (res2.body.length > 0) {
          // First record of page 2 should differ from page 1
          expect(res2.body[0].id).to.not.eq(res1.body[0].id)
        }
      })
    })
  })

  it('creating a recipe generates an audit log entry', () => {
    // Create a recipe
    cy.request({
      method: 'POST', url: '/api/recipes', headers: authHeaders,
      body: { name: 'Cypress审计测试', note: '', ingredients: [{ oil_name: '薰衣草', drops: 1 }], tags: [] }
    }).then(createRes => {
      const recipeId = createRes.body.id
      // Check audit log
      cy.request({ url: '/api/audit-log?limit=5&offset=0', headers: authHeaders }).then(res => {
        const entry = res.body.find(e => e.action === 'share_recipe' && e.target_name === 'Cypress审计测试')
        expect(entry).to.exist
      })
      // Cleanup
      cy.request({ method: 'DELETE', url: `/api/recipes/${recipeId}`, headers: authHeaders, failOnStatusCode: false })
    })
  })

  it('deleting a recipe generates audit log entry', () => {
    cy.request({ url: '/api/audit-log?limit=10&offset=0', headers: authHeaders }).then(res => {
      const deleteEntries = res.body.filter(e => e.action === 'delete_recipe')
      // Should have at least one delete entry (from our previous test cleanup)
      expect(deleteEntries.length).to.be.gte(0) // may or may not exist
    })
  })
})
