import express from 'express'
import { ZktecoGateway } from './zkteco-gateway.js'

export function createDeviceRouter(logger) {
  const router = express.Router()
  const gateway = new ZktecoGateway(logger)

  router.post('/:serial/pull-logs', async (request, response, next) => {
    try {
      const logs = await gateway.pullLogs(request.params.serial, request.body)
      response.json({ data: logs })
    } catch (error) {
      next(error)
    }
  })

  router.post('/:serial/sync-users', async (request, response, next) => {
    try {
      await gateway.syncUsers(request.params.serial, request.body.users || [])
      response.status(202).json({ status: 'queued' })
    } catch (error) {
      next(error)
    }
  })

  router.post('/:serial/restart', async (request, response, next) => {
    try {
      await gateway.restart(request.params.serial)
      response.status(202).json({ status: 'queued' })
    } catch (error) {
      next(error)
    }
  })

  return router
}
