/**
 * Shield AI Safety Layer Routes
 * Handles Shield health, configuration, and safety checks
 */

import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import { Shield } from '@4runr/shield';
import { requireAuth } from '../middleware/auth.js';
import { readRateLimit, writeRateLimit } from '../middleware/rateLimit.js';

/**
 * Register Shield routes
 */
export async function shieldRoutes(fastify: FastifyInstance) {
  const shield = Shield.getInstance();

  /**
   * GET /api/shield/health
   */
  fastify.get(
    '/api/shield/health',
    {
      preHandler: [requireAuth, readRateLimit],
    },
    async (_request: FastifyRequest, reply: FastifyReply) => {
      try {
        const health = shield.getHealth();
        return {
          success: true,
          ...health,
          timestamp: new Date().toISOString(),
        };
      } catch (error: unknown) {
        const msg = error instanceof Error ? error.message : String(error);
        return reply.status(500).send({
          error: 'Failed to get Shield health',
          details: msg,
        });
      }
    }
  );

  /**
   * GET /api/shield/config
   */
  fastify.get(
    '/api/shield/config',
    {
      preHandler: [requireAuth, readRateLimit],
    },
    async (_request: FastifyRequest, reply: FastifyReply) => {
      try {
        const config = shield.getConfig();
        return {
          success: true,
          config,
        };
      } catch (error: unknown) {
        const msg = error instanceof Error ? error.message : String(error);
        return reply.status(500).send({
          error: 'Failed to get Shield config',
          details: msg,
        });
      }
    }
  );

  /**
   * POST /api/shield/check-input
   */
  fastify.post(
    '/api/shield/check-input',
    {
      preHandler: [requireAuth, writeRateLimit],
    },
    async (request: FastifyRequest, reply: FastifyReply) => {
      try {
        const body = request.body as any;
        const input = body.input || body;

        const decision = shield.checkInput(input);

        return {
          success: true,
          decision,
        };
      } catch (error: unknown) {
        const msg = error instanceof Error ? error.message : String(error);
        return reply.status(500).send({
          error: 'Failed to check input',
          details: msg,
        });
      }
    }
  );

  /**
   * POST /api/shield/check-output
   */
  fastify.post(
    '/api/shield/check-output',
    {
      preHandler: [requireAuth, writeRateLimit],
    },
    async (request: FastifyRequest, reply: FastifyReply) => {
      try {
        const body = request.body as any;
        const input = body.input || {};
        const output = body.output || body;

        const decision = shield.checkOutput(input, output);

        return {
          success: true,
          decision,
        };
      } catch (error: unknown) {
        const msg = error instanceof Error ? error.message : String(error);
        return reply.status(500).send({
          error: 'Failed to check output',
          details: msg,
        });
      }
    }
  );
}
