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

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

export async function createHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { name, color, idleThresholdMinutes } = request.body as { name: string; color: string; idleThresholdMinutes?: number };
  return service.create(name, color, request.user.id, idleThresholdMinutes);
}

export async function updateHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { id } = request.params as { id: string };
  const { name, color, idleThresholdMinutes } = request.body as { name: string; color: string; idleThresholdMinutes?: number };
  return service.updateItem(id, name, color, request.user.id, idleThresholdMinutes);
}

export async function reorderHandler(request: FastifyRequest, _reply: FastifyReply) {
  const { orderedIds } = request.body as { orderedIds: string[] };
  return service.reorder(orderedIds, request.user.id);
}

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