import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import { authMiddleware, AuthRequest } from './auth.middleware';
import { voiceprintRoutes } from './voiceprint.routes';
import { spamshieldRoutes } from './spamshield.routes';
import { darkwatchRoutes } from './darkwatch.routes';

export async function routes(fastify: FastifyInstance) {
  // Authenticated routes group
  fastify.register(
    async (authenticated) => {
      // Add auth requirement
      authenticated.addHook('onRequest', async (request: FastifyRequest, reply: FastifyReply) => {
        await fastify.requireAuth(request as AuthRequest);
      });

      // Example authenticated endpoint
      authenticated.get('/user/me', async (request: FastifyRequest, reply: FastifyReply) => {
        const authReq = request as AuthRequest;
        return {
          user: authReq.user,
          authType: authReq.authType,
        };
      });

      // Example service endpoint
      authenticated.get('/services', async (request: FastifyRequest, reply: FastifyReply) => {
        return {
          services: [
            {
              name: 'user-service',
              url: '/api/v1/services/user',
              status: 'healthy',
            },
            {
              name: 'billing-service',
              url: '/api/v1/services/billing',
              status: 'healthy',
            },
            {
              name: 'notification-service',
              url: '/api/v1/services/notifications',
              status: 'healthy',
            },
          ],
        };
      });
    },
    { prefix: '/auth' }
  );

  // Public API routes
  fastify.register(
    async (publicRouter) => {
      // Version info
      publicRouter.get('/info', async () => {
        return {
          version: '1.0.0',
          environment: process.env.NODE_ENV || 'development',
          build: process.env.npm_package_version || 'unknown',
        };
      });

      // API documentation
      publicRouter.get('/docs', async () => {
        return {
          title: 'FrenoCorp API Gateway',
          version: '1.0.0',
          endpoints: {
            public: [
              { method: 'GET', path: '/', description: 'Root endpoint' },
              { method: 'GET', path: '/health', description: 'Health check' },
              { method: 'GET', path: '/api/v1/info', description: 'API version info' },
              { method: 'GET', path: '/api/v1/docs', description: 'API documentation' },
            ],
            authenticated: [
              { method: 'GET', path: '/api/v1/auth/user/me', description: 'Get current user' },
              { method: 'GET', path: '/api/v1/auth/services', description: 'List available services' },
            ],
          },
        };
      });
    },
    { prefix: '/api/v1' }
  );

  // Service proxy placeholder (for future microservice routing)
  fastify.register(
    async (services) => {
      services.get('/services/user', async (request, reply) => {
        // In production, proxy to actual user service
        return {
          service: 'user-service',
          message: 'User service endpoint',
          timestamp: new Date().toISOString(),
        };
      });

      services.get('/services/billing', async (request, reply) => {
        // In production, proxy to actual billing service
        return {
          service: 'billing-service',
          message: 'Billing service endpoint',
          timestamp: new Date().toISOString(),
        };
      });

      services.get('/services/notifications', async (request, reply) => {
        // In production, proxy to actual notification service
        return {
          service: 'notification-service',
          message: 'Notification service endpoint',
          timestamp: new Date().toISOString(),
        };
      });
    },
    { prefix: '/api/v1/services' }
  );

  // VoicePrint service routes
  fastify.register(
    async (voiceprintRouter) => {
      await voiceprintRoutes(voiceprintRouter);
    },
    { prefix: '/voiceprint' }
  );

  // SpamShield service routes
  fastify.register(
    async (spamshieldRouter) => {
      await spamshieldRoutes(spamshieldRouter);
    },
    { prefix: '/spamshield' }
  );

  // DarkWatch service routes
  fastify.register(
    async (darkwatchRouter) => {
      await darkwatchRoutes(darkwatchRouter);
    },
    { prefix: '/darkwatch' }
  );
}
