// AS4 Member Directory API Routes

import { Router } from 'express';
import { memberDirectoryService } from './member-directory.service';
import { certificateManagerService } from './certificate-manager.service';
import { DbisError, ErrorCode } from '@/shared/types';

const router = Router();

/**
 * GET /api/v1/as4/directory/members/:memberId
 * Get member by ID
 */
router.get('/members/:memberId', async (req, res, next) => {
  try {
    const { memberId } = req.params;
    const member = await memberDirectoryService.getMember(memberId);

    if (!member) {
      return res.status(404).json({
        error: ErrorCode.NOT_FOUND,
        message: `Member ${memberId} not found`,
      });
    }

    res.json(member);
  } catch (error) {
    next(error);
  }
});

/**
 * GET /api/v1/as4/directory/members
 * Search members
 */
router.get('/members', async (req, res, next) => {
  try {
    const filters = {
      status: req.query.status as string | undefined,
      capacityTier: req.query.capacityTier ? parseInt(req.query.capacityTier as string) : undefined,
      routingGroup: req.query.routingGroup as string | undefined,
    };

    const members = await memberDirectoryService.searchMembers(filters);
    res.json(members);
  } catch (error) {
    next(error);
  }
});

/**
 * POST /api/v1/as4/directory/members
 * Register new member
 */
router.post('/members', async (req, res, next) => {
  try {
    const member = await memberDirectoryService.registerMember(req.body);
    res.status(201).json(member);
  } catch (error) {
    next(error);
  }
});

/**
 * PATCH /api/v1/as4/directory/members/:memberId
 * Update member
 */
router.patch('/members/:memberId', async (req, res, next) => {
  try {
    const { memberId } = req.params;
    const member = await memberDirectoryService.updateMember(memberId, req.body);
    res.json(member);
  } catch (error) {
    next(error);
  }
});

/**
 * GET /api/v1/as4/directory/members/:memberId/certificates
 * Get member certificates
 */
router.get('/members/:memberId/certificates', async (req, res, next) => {
  try {
    const { memberId } = req.params;
    const certificates = await memberDirectoryService.getMemberCertificates(memberId);
    res.json(certificates);
  } catch (error) {
    next(error);
  }
});

/**
 * POST /api/v1/as4/directory/members/:memberId/certificates
 * Add certificate to member
 */
router.post('/members/:memberId/certificates', async (req, res, next) => {
  try {
    const { memberId } = req.params;
    const certificate = await memberDirectoryService.addCertificate(memberId, req.body);
    res.status(201).json(certificate);
  } catch (error) {
    next(error);
  }
});

/**
 * GET /api/v1/as4/directory/members/:memberId/endpoint
 * Get member endpoint configuration
 */
router.get('/members/:memberId/endpoint', async (req, res, next) => {
  try {
    const { memberId } = req.params;
    const member = await memberDirectoryService.getMember(memberId);

    if (!member) {
      return res.status(404).json({
        error: ErrorCode.NOT_FOUND,
        message: `Member ${memberId} not found`,
      });
    }

    res.json({
      memberId: member.memberId,
      as4EndpointUrl: member.as4EndpointUrl,
      tlsCertFingerprint: member.tlsCertFingerprint,
      allowedMessageTypes: member.allowedMessageTypes,
      routingGroups: member.routingGroups,
    });
  } catch (error) {
    next(error);
  }
});

/**
 * GET /api/v1/as4/directory/certificates/expiration-warnings
 * Get certificate expiration warnings
 */
router.get('/certificates/expiration-warnings', async (req, res, next) => {
  try {
    const daysThreshold = req.query.days ? parseInt(req.query.days as string) : 30;
    const warnings = await certificateManagerService.getExpirationWarnings(daysThreshold);
    res.json(warnings);
  } catch (error) {
    next(error);
  }
});

export default router;
