import type { FastifyRequest, FastifyReply } from 'fastify';
import * as productService from '../services/product.service.js';

export async function listHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { search, category_id, page, limit } = request.query as { search?: string; category_id?: string; page?: number; limit?: number };
  return productService.list(search, category_id, page, limit);
}

export async function getHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id } = request.params as { id: string };
  return productService.get(id);
}

export async function createHandler(request: FastifyRequest, _reply: FastifyReply) {
  const data = request.body as Record<string, unknown>;
  return productService.create(data, request.user.id);
}

export async function updateHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id } = request.params as { id: string };
  const data = request.body as Record<string, unknown>;
  return productService.updateProduct(id, data, request.user.id);
}

export async function deleteHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id } = request.params as { id: string };
  await productService.remove(id, request.user.id);
  return { ok: true };
}
