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

export async function listHandler(_request: FastifyRequest, _reply: FastifyReply) {
  return roleService.list();
}

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

export async function createHandler(request: FastifyRequest, _reply: FastifyReply) {
  const data = request.body as { name: string; permissions: string[] | '*' };
  return roleService.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 { name?: string; permissions?: string[] | '*' };
  return roleService.updateRole(id, data, request.user.id);
}

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