import type { FastifyPluginAsync } from 'fastify'
import { TaxService } from '../../services/tax.service.js'

export const taxRoutes: FastifyPluginAsync = async (app) => {
  app.get('/tax/lookup/:zip', { preHandler: [app.authenticate, app.requirePermission('pos.view')] }, async (request, reply) => {
    const { zip } = request.params as { zip: string }

    if (!/^\d{5}(-\d{4})?$/.test(zip)) {
      return reply.status(400).send({ error: { message: 'Invalid zip code format', statusCode: 400 } })
    }

    const result = await TaxService.lookupByZip(zip)
    return reply.send(result)
  })
}
