import { FastifyInstance } from 'fastify';
import {
  createAzureConfig,
  getAzureConfig,
  updateAzureConfig,
  deleteAzureConfig,
  testAzureConnection,
} from './repository.js';
import { CreateAzureConfigSchema, UpdateAzureConfigSchema } from './types.js';

export async function azureConfigRoutes(fastify: FastifyInstance) {
  // Get Azure configuration
  fastify.get('/azure-config', async (request, reply) => {
    try {
      const config = await getAzureConfig();
      if (!config) {
        return reply.code(404).send({ error: 'Azure configuration not found' });
      }
      // Don't return the actual client secret in the response
      const { clientSecret, ...safeConfig } = config;
      return reply.send({ ...safeConfig, hasClientSecret: !!clientSecret });
    } catch (error) {
      fastify.log.error(error, 'Failed to get Azure config');
      return reply.code(500).send({ error: 'Failed to get Azure configuration' });
    }
  });

  // Create Azure configuration
  fastify.post('/azure-config', async (request, reply) => {
    try {
      const data = CreateAzureConfigSchema.parse(request.body);
      const config = await createAzureConfig(data);
      const { clientSecret, ...safeConfig } = config;
      return reply.code(201).send({ ...safeConfig, hasClientSecret: true });
    } catch (error) {
      fastify.log.error(error, 'Failed to create Azure config');
      return reply.code(500).send({ error: 'Failed to create Azure configuration' });
    }
  });

  // Update Azure configuration
  fastify.put('/azure-config/:id', async (request, reply) => {
    try {
      const { id } = request.params as { id: string };
      const updates = UpdateAzureConfigSchema.parse(request.body);
      const config = await updateAzureConfig(id, updates);
      if (!config) {
        return reply.code(404).send({ error: 'Azure configuration not found' });
      }
      const { clientSecret, ...safeConfig } = config;
      return reply.send({ ...safeConfig, hasClientSecret: !!clientSecret });
    } catch (error) {
      fastify.log.error(error, 'Failed to update Azure config');
      return reply.code(500).send({ error: 'Failed to update Azure configuration' });
    }
  });

  // Delete Azure configuration
  fastify.delete('/azure-config/:id', async (request, reply) => {
    try {
      const { id } = request.params as { id: string };
      const success = await deleteAzureConfig(id);
      if (!success) {
        return reply.code(404).send({ error: 'Azure configuration not found' });
      }
      return reply.code(204).send();
    } catch (error) {
      fastify.log.error(error, 'Failed to delete Azure config');
      return reply.code(500).send({ error: 'Failed to delete Azure configuration' });
    }
  });

  // Test Azure connection
  fastify.post('/azure-config/test', async (request, reply) => {
    try {
      const result = await testAzureConnection();
      return reply.send(result);
    } catch (error) {
      fastify.log.error(error, 'Failed to test Azure connection');
      return reply.code(500).send({ success: false, error: 'Failed to test Azure connection' });
    }
  });
}
