const router = require('express').Router()

const localNavMiddleware = require('../../middleware/local-nav')

const { shared } = require('./middleware')
const { getBriefInvestmentSummary } = require('./middleware/team')
const {
  createStep1,
  createStep2,
  archive,
  audit,
  details,
  edit,
  evaluation,
  team,
  interactions,
} = require('./controllers')

const detailsFormMiddleware = require('./middleware/forms/details')
const valueFormMiddleware = require('./middleware/forms/value')
const requirementsMiddleware = require('./middleware/forms/requirements')
const interactionsMiddleware = require('./middleware/forms/interactions')
const projectManagementForm = require('./middleware/forms/project-management')
const { renderInvestmentList } = require('./controllers/list')
const { setDefaults, getInvestmentProjectsCollection } = require('./middleware/collection')

const LOCAL_NAV = [
  { path: '../details', label: 'Project details' },
  { path: '../team', label: 'Project team' },
  { path: '../interactions', label: 'Interactions' },
  { path: '../evaluation', label: 'Evaluations' },
  { path: '../audit', label: 'Audit history' },
]

router.use(localNavMiddleware(LOCAL_NAV))

router.param('id', shared.getInvestmentDetails)
router.param('interactionId', shared.getInteractionDetails)

router.get('/', setDefaults, getInvestmentProjectsCollection, renderInvestmentList)

router.post('/:id/details', archive.archiveInvestmentProjectHandler, details.detailsGetHandler)
router.get('/:id/unarchive', archive.unarchiveInvestmentProjectHandler)

router.get('/:id/audit', audit.getInvestmentAudit)

router.get('/create', (req, res) => { res.redirect('create/1') })

router
  .route('/create/1')
  .get(createStep1.getHandler)
  .post(createStep1.postHandler)

router
  .route('/create/2')
  .get(detailsFormMiddleware.populateForm, createStep2.createGetHandler)
  .post(detailsFormMiddleware.populateForm, detailsFormMiddleware.handleFormPost, createStep2.createPostHandler)

router.get('/:id', details.redirectToDetails)
router.get('/:id/details', details.detailsGetHandler)

router
  .route('/:id/edit-details')
  .get(detailsFormMiddleware.populateForm, edit.editDetailsGet)
  .post(detailsFormMiddleware.populateForm, detailsFormMiddleware.handleFormPost, edit.editDetailsPost)

router
  .route('/:id/edit-value')
  .get(valueFormMiddleware.populateForm, edit.editValueGet)
  .post(valueFormMiddleware.populateForm, valueFormMiddleware.handleFormPost, edit.editValuePost)

router
  .route('/:id/edit-requirements')
  .get(requirementsMiddleware.populateForm, edit.editRequirementsGet)
  .post(requirementsMiddleware.populateForm, requirementsMiddleware.handleFormPost, edit.editRequirementsPost)

router.get('/:id/team', team.details.getDetailsHandler)
router
  .route('/:id/edit-project-management')
  .get(getBriefInvestmentSummary, projectManagementForm.populateForm, team.editProjectManagement.getHandler)
  .post(
    getBriefInvestmentSummary,
    projectManagementForm.populateForm,
    projectManagementForm.handleFormPost,
    team.editProjectManagement.postHandler,
    team.editProjectManagement.getHandler
  )

router.get('/:id/interactions', interactions.list.indexGetHandler)

router
  .route('/:id/interactions/create')
  .get(interactionsMiddleware.populateForm, interactions.create.createGetInteractionHandler)
  .post(
    interactionsMiddleware.populateForm,
    interactionsMiddleware.handleFormPost,
    interactions.create.createPostInteractionHandler,
    interactions.create.createGetInteractionHandler
  )

router.get('/:id/evaluation', evaluation.renderEvaluationPage)

router
  .route('/:id/interactions/:interactionId/edit')
  .get(interactionsMiddleware.populateForm, interactions.edit.editGetInteractionHandler)
  .post(
    interactionsMiddleware.populateForm,
    interactionsMiddleware.handleFormPost,
    interactions.edit.editPostInteractionHandler,
    interactions.edit.editGetInteractionHandler
  )

module.exports = router

@@ -500,32 +500,36 @@
 nst requirements
+Form
 Middleware = req
@@ -578,32 +578,36 @@
 nst interactions
+Form
 Middleware = req
@@ -673,16 +673,26 @@
 mentForm
+Middleware
  = requi
@@ -2554,32 +2554,36 @@
 get(requirements
+Form
 Middleware.popul
@@ -2629,32 +2629,36 @@
 ost(requirements
+Form
 Middleware.popul
@@ -2678,16 +2678,20 @@
 irements
+Form
 Middlewa
@@ -2888,32 +2888,42 @@
 ctManagementForm
+Middleware
 .populateForm, t
@@ -3025,16 +3025,26 @@
 mentForm
+Middleware
 .populat
@@ -3075,16 +3075,26 @@
 mentForm
+Middleware
 .handleF
@@ -3316,32 +3316,36 @@
 get(interactions
+Form
 Middleware.popul
@@ -3419,32 +3419,36 @@
     interactions
+Form
 Middleware.popul
@@ -3464,32 +3464,36 @@
     interactions
+Form
 Middleware.handl
@@ -3746,32 +3746,36 @@
 get(interactions
+Form
 Middleware.popul
@@ -3845,32 +3845,36 @@
     interactions
+Form
 Middleware.popul
@@ -3898,16 +3898,20 @@
 ractions
+Form
 Middlewa

improve variable names