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

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

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

export async function createHandler(request: FastifyRequest, _reply: FastifyReply) {
  const data = request.body as Record<string, unknown>;
  return employeeService.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 employeeService.updateEmployee(id, data, request.user.id);
}

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