// IRU Marketplace API — public routes + admin (inquiry/offering services).

import { Router } from 'express';
import iruMarketplacePublicRoutes from '@/integration/api-gateway/routes/iru-marketplace-public.routes';
import { offeringService } from '@/core/iru/offering.service';
import { inquiryService } from '@/core/iru/inquiry.service';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';
import { adminPermissionMiddleware } from '@/integration/api-gateway/middleware/admin-permission.middleware';

const router = Router();

router.use(iruMarketplacePublicRoutes);

// ============================================================================
// Admin Routes (Auth Required)
// ============================================================================

router.post(
  '/admin/offerings',
  zeroTrustAuthMiddleware,
  adminPermissionMiddleware,
  async (req, res, next) => {
    try {
      const offering = await offeringService.createOffering(req.body);
      res.status(201).json({
        success: true,
        data: offering,
        timestamp: new Date(),
      });
    } catch (error) {
      return next(error);
    }
  }
);

router.put(
  '/admin/offerings/:offeringId',
  zeroTrustAuthMiddleware,
  adminPermissionMiddleware,
  async (req, res, next) => {
    try {
      const { offeringId } = req.params;
      const offering = await offeringService.updateOffering(offeringId, req.body);
      res.json({
        success: true,
        data: offering,
        timestamp: new Date(),
      });
    } catch (error) {
      return next(error);
    }
  }
);

router.delete(
  '/admin/offerings/:offeringId',
  zeroTrustAuthMiddleware,
  adminPermissionMiddleware,
  async (req, res, next) => {
    try {
      const { offeringId } = req.params;
      await offeringService.deleteOffering(offeringId);
      res.json({
        success: true,
        message: `Offering ${offeringId} deleted successfully`,
        timestamp: new Date(),
      });
    } catch (error) {
      return next(error);
    }
  }
);

router.get(
  '/admin/offerings/:offeringId/stats',
  zeroTrustAuthMiddleware,
  adminPermissionMiddleware,
  async (req, res, next) => {
    try {
      const { offeringId } = req.params;
      const stats = await offeringService.getOfferingStats(offeringId);
      res.json({
        success: true,
        data: stats,
        timestamp: new Date(),
      });
    } catch (error) {
      return next(error);
    }
  }
);

router.get(
  '/admin/inquiries',
  zeroTrustAuthMiddleware,
  adminPermissionMiddleware,
  async (req, res, next) => {
    try {
      const filters: Record<string, unknown> = {};
      if (req.query.status) {
        filters.status = req.query.status as string;
      }
      if (req.query.offeringId) {
        filters.offeringId = req.query.offeringId as string;
      }
      if (req.query.capacityTier) {
        filters.capacityTier = parseInt(req.query.capacityTier as string, 10);
      }
      if (req.query.limit) {
        filters.limit = parseInt(req.query.limit as string, 10);
      }
      if (req.query.offset) {
        filters.offset = parseInt(req.query.offset as string, 10);
      }

      const inquiries = await inquiryService.getInquiries(filters);
      res.json({
        success: true,
        data: inquiries,
        timestamp: new Date(),
      });
    } catch (error) {
      return next(error);
    }
  }
);

router.get(
  '/admin/inquiries/:inquiryId',
  zeroTrustAuthMiddleware,
  adminPermissionMiddleware,
  async (req, res, next) => {
    try {
      const { inquiryId } = req.params;
      const inquiry = await inquiryService.getInquiryById(inquiryId);
      res.json({
        success: true,
        data: inquiry,
        timestamp: new Date(),
      });
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/admin/inquiries/:inquiryId/acknowledge',
  zeroTrustAuthMiddleware,
  adminPermissionMiddleware,
  async (req, res, next) => {
    try {
      const { inquiryId } = req.params;
      const result = await inquiryService.acknowledgeInquiry(inquiryId);
      res.json({
        success: true,
        data: result,
        timestamp: new Date(),
      });
    } catch (error) {
      return next(error);
    }
  }
);

router.put(
  '/admin/inquiries/:inquiryId',
  zeroTrustAuthMiddleware,
  adminPermissionMiddleware,
  async (req, res, next) => {
    try {
      const { inquiryId } = req.params;
      const result = await inquiryService.updateInquiry(inquiryId, req.body);
      res.json({
        success: true,
        data: result,
        timestamp: new Date(),
      });
    } catch (error) {
      return next(error);
    }
  }
);

export default router;
