import { addDays, format, subDays } from 'date-fns'

const assertEstimatedLandDate = ({ index, date, countdown, colour }) => {
  cy.get('@projectListItems')
    .eq(index)
    .find('[data-test="project-details"]')
    .click()
    .find('[data-test="estimated-land-date"]')
    .as('estimatedLandDate')
    .should('have.css', 'background-color', colour)

  cy.get('@estimatedLandDate')
    .find('[data-test="estimated-land-date-countdown"]')
    .should('have.text', countdown)

  cy.get('@estimatedLandDate')
    .find('[data-test="estimated-land-date-date"]')
    .should('have.text', format(date, 'E, dd MMM yyyy'))
}

describe('Dashboard items - estimated land date', () => {
  before(() => {
    cy.setUserFeatures(['personalised-dashboard'])
    cy.visit('/')
  })

  after(() => {
    cy.resetUser()
  })

  beforeEach(() => {
    cy.get('[data-test="projects-list-item"]').as('projectListItems')
  })

  context('My project list items - estimated land date', () => {
    it('should show a grey panel when the project has under 0 days remaining', () => {
      assertEstimatedLandDate({
        index: 0,
        date: subDays(new Date(), 14),
        countdown: '-14 days',
        colour: 'rgba(191, 193, 195, 0.5)',
      })
    })

    it('should show a red panel when the project has 0 days remaining', () => {
      assertEstimatedLandDate({
        index: 1,
        date: new Date(),
        countdown: '0 days',
        colour: 'rgba(212, 53, 28, 0.4)',
      })
    })

    it('should show a red panel when the project has less than 30 days remaining', () => {
      assertEstimatedLandDate({
        index: 3,
        date: addDays(new Date(), 28),
        countdown: '28 days',
        colour: 'rgba(212, 53, 28, 0.4)',
      })
    })

    it('should show an amber panel when the project has between 30 and 89 days remaining', () => {
      assertEstimatedLandDate({
        index: 4,
        date: addDays(new Date(), 42),
        countdown: '42 days',
        colour: 'rgba(255, 221, 0, 0.5)',
      })
    })

    it('should show a green panel when the project has more than 89 days remaining', () => {
      assertEstimatedLandDate({
        index: 8,
        date: addDays(new Date(), 98),
        countdown: '98 days',
        colour: 'rgba(0, 112, 60, 0.3)',
      })
    })
  })
})

@@ -44,16 +44,90 @@
 ate-fns'
+%0Aimport %7B investmentProjectFaker %7D from '../../fakers/investment-projects'
 %0A%0Aconst 
@@ -750,16 +750,263 @@
 () =%3E %7B%0A
+  const today = new Date()%0A  const myProjects = %5B%0A    subDays(today, 14),%0A    today,%0A    addDays(today, 29),%0A    addDays(today, 89),%0A    addDays(today, 90),%0A  %5D.map((estimated_land_date) =%3E%0A    investmentProjectFaker(%7B estimated_land_date %7D)%0A  )%0A%0A
   before
@@ -1087,21 +1087,21 @@
 /')%0A
-  %7D)%0A%0A  after
+%0A  beforeEach
 (() 
@@ -1116,46 +1116,225 @@
  cy.
-resetUser()%0A  %7D)%0A%0A  beforeEach(() =%3E %7B
+intercept('POST', '/api-proxy/v3/search/investment_project', %7B%0A      body: %7B%0A        count: myProjects.length,%0A        results: myProjects,%0A      %7D,%0A    %7D).as('apiRequest')%0A    cy.visit('/')%0A    cy.wait('@apiRequest')
 %0A   
@@ -1406,16 +1406,57 @@
 )%0A  %7D)%0A%0A
+  after(() =%3E %7B%0A    cy.resetUser()%0A  %7D)%0A%0A
   contex
@@ -2162,17 +2162,17 @@
  index: 
-3
+2
 ,%0A      
@@ -2200,17 +2200,17 @@
 ate(), 2
-8
+9
 ),%0A     
@@ -2225,17 +2225,17 @@
 down: '2
-8
+9
  days',%0A
@@ -2439,17 +2439,17 @@
  index: 
-4
+3
 ,%0A      
@@ -2476,18 +2476,18 @@
 Date(), 
-42
+89
 ),%0A     
@@ -2505,10 +2505,10 @@
 n: '
-42
+89
  day
@@ -2710,17 +2710,17 @@
  index: 
-8
+4
 ,%0A      
@@ -2748,17 +2748,17 @@
 ate(), 9
-8
+0
 ),%0A     
@@ -2773,17 +2773,17 @@
 down: '9
-8
+0
  days',%0A

Use intercept and fake data for estimated land date test