/**
 * Pause Queue HTTP endpoints.
 *
 * GET    /pause              — current pause state + queue size
 * PUT    /pause              — set pause state { paused: boolean }
 * GET    /pause/queue        — list all paused items
 * POST   /pause/queue/:id/release — release item with transformed content
 * POST   /pause/queue/:id/edit    — edit content and release { content: string }
 * POST   /pause/queue/:id/drop    — drop item (empty response to client)
 * POST   /pause/release-all       — release all paused items
 */
import type { FastifyInstance } from 'fastify';
import { pauseQueue } from '../proxymodel/pause-queue.js';

export function registerPauseEndpoint(app: FastifyInstance): void {
  // GET /pause — state
  app.get('/pause', async (_request, reply) => {
    reply.send({
      paused: pauseQueue.paused,
      queueSize: pauseQueue.size,
    });
  });

  // PUT /pause — toggle
  app.put<{ Body: { paused: boolean } }>('/pause', async (request, reply) => {
    const { paused } = request.body ?? {};
    if (typeof paused !== 'boolean') {
      reply.code(400).send({ error: 'paused must be a boolean' });
      return;
    }
    pauseQueue.setPaused(paused);
    reply.send({
      paused: pauseQueue.paused,
      queueSize: pauseQueue.size,
    });
  });

  // GET /pause/queue — list items
  app.get('/pause/queue', async (_request, reply) => {
    reply.send({
      paused: pauseQueue.paused,
      items: pauseQueue.getItems(),
    });
  });

  // POST /pause/queue/:id/release
  app.post<{ Params: { id: string } }>('/pause/queue/:id/release', async (request, reply) => {
    const ok = pauseQueue.releaseOne(request.params.id);
    if (!ok) {
      reply.code(404).send({ error: `Item '${request.params.id}' not found in queue` });
      return;
    }
    reply.send({ released: true, queueSize: pauseQueue.size });
  });

  // POST /pause/queue/:id/edit — edit and release
  app.post<{
    Params: { id: string };
    Body: { content: string };
  }>('/pause/queue/:id/edit', async (request, reply) => {
    const { content } = request.body ?? {};
    if (typeof content !== 'string') {
      reply.code(400).send({ error: 'content must be a string' });
      return;
    }
    const ok = pauseQueue.editAndRelease(request.params.id, content);
    if (!ok) {
      reply.code(404).send({ error: `Item '${request.params.id}' not found in queue` });
      return;
    }
    reply.send({ edited: true, queueSize: pauseQueue.size });
  });

  // POST /pause/queue/:id/drop
  app.post<{ Params: { id: string } }>('/pause/queue/:id/drop', async (request, reply) => {
    const ok = pauseQueue.dropOne(request.params.id);
    if (!ok) {
      reply.code(404).send({ error: `Item '${request.params.id}' not found in queue` });
      return;
    }
    reply.send({ dropped: true, queueSize: pauseQueue.size });
  });

  // POST /pause/release-all
  app.post('/pause/release-all', async (_request, reply) => {
    const count = pauseQueue.releaseAll();
    reply.send({ released: count, queueSize: 0 });
  });
}
