describe('Projects Flow', () => {
  const ADMIN_TOKEN = 'c86ae7afbe10fabe3c1d5e1a7fee74feaadfd5dc7be2ab62'
  const authHeaders = { Authorization: `Bearer ${ADMIN_TOKEN}` }
  let testProjectId = null

  it('creates a project', () => {
    cy.request({
      method: 'POST', url: '/api/projects', headers: authHeaders,
      body: {
        name: 'Cypress测试项目',
        ingredients: JSON.stringify([{ oil: '薰衣草', drops: 5 }, { oil: '乳香', drops: 3 }]),
        pricing: 100,
        note: 'E2E test project'
      }
    }).then(res => {
      expect(res.status).to.be.oneOf([200, 201])
      testProjectId = res.body.id
    })
  })

  it('lists projects', () => {
    cy.request({ url: '/api/projects', headers: authHeaders }).then(res => {
      expect(res.body).to.be.an('array')
      const found = res.body.find(p => p.name === 'Cypress测试项目')
      expect(found).to.exist
      testProjectId = found.id
    })
  })

  it('updates the project pricing', () => {
    cy.request({ url: '/api/projects', headers: authHeaders }).then(res => {
      const found = res.body.find(p => p.name === 'Cypress测试项目')
      testProjectId = found.id
      cy.request({
        method: 'PUT', url: `/api/projects/${testProjectId}`, headers: authHeaders,
        body: { pricing: 200, note: 'updated pricing' }
      }).then(r => expect(r.status).to.eq(200))
    })
  })

  it('verifies update', () => {
    cy.request({ url: '/api/projects', headers: authHeaders }).then(res => {
      const found = res.body.find(p => p.name === 'Cypress测试项目')
      expect(found.pricing).to.eq(200)
    })
  })

  it('project profit calculation is correct', () => {
    // Fetch oils to calculate expected cost
    cy.request('/api/oils').then(oilRes => {
      const oilMap = {}
      oilRes.body.forEach(o => { oilMap[o.name] = o.bottle_price / o.drop_count })

      cy.request({ url: '/api/projects', headers: authHeaders }).then(res => {
        const proj = res.body.find(p => p.name === 'Cypress测试项目')
        const ings = JSON.parse(proj.ingredients)
        const cost = ings.reduce((s, i) => s + (oilMap[i.oil] || 0) * i.drops, 0)
        const profit = proj.pricing - cost
        expect(profit).to.be.gt(0) // pricing(200) > cost
        expect(cost).to.be.gt(0)
      })
    })
  })

  it('deletes the project', () => {
    cy.request({ url: '/api/projects', headers: authHeaders }).then(res => {
      const found = res.body.find(p => p.name === 'Cypress测试项目')
      if (found) {
        cy.request({
          method: 'DELETE', url: `/api/projects/${found.id}`, headers: authHeaders
        }).then(r => expect(r.status).to.eq(200))
      }
    })
  })

  after(() => {
    cy.request({ url: '/api/projects', headers: authHeaders, failOnStatusCode: false }).then(res => {
      if (res.status === 200 && Array.isArray(res.body)) {
        res.body.filter(p => p.name && p.name.includes('Cypress')).forEach(p => {
          cy.request({ method: 'DELETE', url: `/api/projects/${p.id}`, headers: authHeaders, failOnStatusCode: false })
        })
      }
    })
  })
})
