import { Router } from 'express'
import { requireRenterAuth } from '../../middleware/requireRenterAuth'
import { parseBody } from '../../http/validate'
import { ok } from '../../http/respond'
import { renterFcmTokenSchema, renterUpdateSchema } from './auth.renter.schemas'
import * as service from './auth.renter.service'

const router = Router()

router.post('/signup', (_req, res) => {
  service.signupDisabled()
  res.end()
})

router.post('/login', (_req, res) => {
  service.loginDisabled()
  res.end()
})

router.get('/me', requireRenterAuth, async (req, res, next) => {
  try {
    ok(res, await service.getMe(req.renterId))
  } catch (err) { next(err) }
})

router.patch('/me', requireRenterAuth, async (req, res, next) => {
  try {
    const body = parseBody(renterUpdateSchema, req)
    ok(res, await service.updateMe(req.renterId, body))
  } catch (err) { next(err) }
})

router.post('/me/fcm-token', requireRenterAuth, async (req, res, next) => {
  try {
    const { fcmToken } = parseBody(renterFcmTokenSchema, req)
    ok(res, await service.updateFcmToken(req.renterId, fcmToken))
  } catch (err) { next(err) }
})

export default router
