/**
 * Document Retention Routes
 * Retention policies and disposal workflows
 */

import { FastifyInstance } from 'fastify';
import {
  createRetentionPolicy,
  getRetentionPolicy,
  listRetentionPolicies,
  applyRetentionPolicy,
  getDocumentRetentionRecord,
  getExpiredRetentionRecords,
  disposeDocument,
  extendRetention,
  placeOnLegalHold,
  getRetentionStatistics,
} from '@the-order/database';
import { authenticateJWT, requireRole } from '@the-order/shared';

export async function registerRetentionRoutes(server: FastifyInstance): Promise<void> {
  server.post('/retention/policies', {
    preHandler: [authenticateJWT, requireRole('admin')],
    schema: {
      body: {
        type: 'object',
        required: ['name', 'retention_period_years'],
        properties: {
          name: { type: 'string' },
          description: { type: 'string' },
          retention_period_years: { type: 'number' },
          retention_trigger: { type: 'string' },
          disposal_action: { type: 'string' },
        },
      },
      tags: ['retention'],
    },
  }, async (request, reply) => {
    const body = request.body as any;
    const user = (request as any).user;
    const policy = await createRetentionPolicy({ ...body, created_by: user?.id });
    return reply.code(201).send(policy);
  });

  server.get('/retention/policies', {
    preHandler: [authenticateJWT],
    schema: { tags: ['retention'] },
  }, async (request, reply) => {
    const policies = await listRetentionPolicies();
    return reply.send({ policies });
  });

  server.post('/documents/:id/retention', {
    preHandler: [authenticateJWT, requireRole('admin')],
    schema: {
      params: { type: 'object', properties: { id: { type: 'string' } } },
      body: { type: 'object', required: ['policy_id'], properties: { policy_id: { type: 'string' } } },
      tags: ['retention'],
    },
  }, async (request, reply) => {
    const { id } = request.params as { id: string };
    const { policy_id } = request.body as { policy_id: string };
    const record = await applyRetentionPolicy(id, policy_id);
    return reply.send(record);
  });

  server.get('/documents/:id/retention', {
    preHandler: [authenticateJWT],
    schema: { params: { type: 'object', properties: { id: { type: 'string' } } }, tags: ['retention'] },
  }, async (request, reply) => {
    const { id } = request.params as { id: string };
    const record = await getDocumentRetentionRecord(id);
    return reply.send(record || {});
  });

  server.get('/retention/expired', {
    preHandler: [authenticateJWT, requireRole('admin')],
    schema: { tags: ['retention'] },
  }, async (request, reply) => {
    const records = await getExpiredRetentionRecords();
    return reply.send({ records });
  });

  server.post('/documents/:id/dispose', {
    preHandler: [authenticateJWT, requireRole('admin')],
    schema: {
      params: { type: 'object', properties: { id: { type: 'string' } } },
      body: { type: 'object', properties: { notes: { type: 'string' } } },
      tags: ['retention'],
    },
  }, async (request, reply) => {
    const { id } = request.params as { id: string };
    const { notes } = request.body as { notes?: string };
    const user = (request as any).user;
    const record = await disposeDocument(id, user.id, notes);
    return reply.send(record);
  });

  server.get('/retention/statistics', {
    preHandler: [authenticateJWT, requireRole('admin')],
    schema: { tags: ['retention'] },
  }, async (request, reply) => {
    const stats = await getRetentionStatistics();
    return reply.send(stats);
  });
}

