import type { FastifyInstance } from 'fastify';
import { listHandler, createHandler, updateHandler, deleteHandler } from '../controllers/weight.controller.js';
import { authorize, authorizeAny } from '../plugins/authorize.js';
import { PERMISSIONS } from '../constants/permissions.js';
import { createWeightSchema, updateWeightSchema } from '../schemas/weight.schema.js';

const CAN_BROWSE_PRODUCTS = [PERMISSIONS.PRODUCTS_VIEW, PERMISSIONS.ORDERS_VIEW, PERMISSIONS.ORDERS_CREATE, PERMISSIONS.ORDERS_EDIT];

export default async function weightRoutes(fastify: FastifyInstance) {
  fastify.get('/api/weights', { preHandler: [authorizeAny(CAN_BROWSE_PRODUCTS)] }, listHandler);
  fastify.post('/api/weights', { preHandler: [authorize(PERMISSIONS.PRODUCTS_CREATE)], schema: createWeightSchema }, createHandler);
  fastify.put('/api/weights/:id', { preHandler: [authorize(PERMISSIONS.PRODUCTS_EDIT)], schema: updateWeightSchema }, updateHandler);
  fastify.delete('/api/weights/:id', { preHandler: [authorize(PERMISSIONS.PRODUCTS_DELETE)] }, deleteHandler);
}
