import type { FastifyInstance } from 'fastify';
import { z } from 'zod';
import pino from 'pino';

const logger = pino({ name: 'api-approvals' });

const approvalDecisionSchema = z.object({
  decision: z.enum(['approve', 'reject']),
  reason: z.string().max(500).optional(),
});

export function registerApprovalRoutes(app: FastifyInstance) {
  // List pending approvals for tenant
  app.get('/api/v1/approvals', async (req, reply) => {
    // TODO: SELECT from audit_entries WHERE status = 'awaiting_approval'
    return { approvals: [] };
  });

  // Approve or reject a step
  app.post('/api/v1/approvals/:stepId', async (req, reply) => {
    const { stepId } = req.params as { stepId: string };
    const body = approvalDecisionSchema.parse(req.body);

    // TODO: Update audit entry, notify agent via WebSocket/Redis pub-sub
    logger.info({ stepId, decision: body.decision }, 'Approval decision recorded');

    return {
      step_id: stepId,
      decision: body.decision,
      reason: body.reason,
    };
  });
}
