import { Router } from 'express';
import { requireTraceContext } from '../../core/guards/trace-context.guard';
import { requirePermission } from '../../core/guards/rbac.guard';
import {
  WinNodeController,
  TaskCenterController,
  IndependentSiteController,
  CostTemplateController,
  PlatformAccountController,
  ReturnController,
} from '../controllers/SettingsController';

const router = Router();

// WinNode routes
router.get(
  '/winnode',
  requireTraceContext,
  requirePermission('settings:read'),
  WinNodeController.list
);

router.get(
  '/winnode/stats',
  requireTraceContext,
  requirePermission('settings:read'),
  WinNodeController.getStats
);

router.get(
  '/winnode/:id',
  requireTraceContext,
  requirePermission('settings:read'),
  WinNodeController.getById
);

router.post(
  '/winnode',
  requireTraceContext,
  requirePermission('settings:write'),
  WinNodeController.create
);

router.put(
  '/winnode/:id',
  requireTraceContext,
  requirePermission('settings:write'),
  WinNodeController.update
);

router.delete(
  '/winnode/:id',
  requireTraceContext,
  requirePermission('settings:write'),
  WinNodeController.delete
);

router.post(
  '/winnode/:id/test',
  requireTraceContext,
  requirePermission('settings:write'),
  WinNodeController.testConnection
);

router.post(
  '/winnode/:id/restart',
  requireTraceContext,
  requirePermission('settings:write'),
  WinNodeController.restart
);

router.post(
  '/winnode/:id/heartbeat',
  WinNodeController.heartbeat
);

// TaskCenter routes
router.get(
  '/taskcenter',
  requireTraceContext,
  requirePermission('task:read'),
  TaskCenterController.list
);

router.get(
  '/taskcenter/stats',
  requireTraceContext,
  requirePermission('task:read'),
  TaskCenterController.getStats
);

router.get(
  '/taskcenter/:id',
  requireTraceContext,
  requirePermission('task:read'),
  TaskCenterController.getById
);

router.post(
  '/taskcenter',
  requireTraceContext,
  requirePermission('task:write'),
  TaskCenterController.create
);

router.put(
  '/taskcenter/:id/status',
  requireTraceContext,
  requirePermission('task:write'),
  TaskCenterController.updateStatus
);

router.post(
  '/taskcenter/:id/cancel',
  requireTraceContext,
  requirePermission('task:write'),
  TaskCenterController.cancel
);

router.post(
  '/taskcenter/:id/retry',
  requireTraceContext,
  requirePermission('task:write'),
  TaskCenterController.retry
);

// IndependentSite routes
router.get(
  '/independent-site',
  requireTraceContext,
  requirePermission('site:read'),
  IndependentSiteController.listSites
);

router.get(
  '/independent-site/:id',
  requireTraceContext,
  requirePermission('site:read'),
  IndependentSiteController.getSiteById
);

router.post(
  '/independent-site',
  requireTraceContext,
  requirePermission('site:write'),
  IndependentSiteController.createSite
);

router.put(
  '/independent-site/:id',
  requireTraceContext,
  requirePermission('site:write'),
  IndependentSiteController.updateSite
);

router.delete(
  '/independent-site/:id',
  requireTraceContext,
  requirePermission('site:write'),
  IndependentSiteController.deleteSite
);

router.post(
  '/independent-site/:id/sync-products',
  requireTraceContext,
  requirePermission('site:write'),
  IndependentSiteController.syncProducts
);

router.get(
  '/independent-site/:id/products',
  requireTraceContext,
  requirePermission('site:read'),
  IndependentSiteController.getSiteProducts
);

router.get(
  '/independent-site/:id/orders',
  requireTraceContext,
  requirePermission('site:read'),
  IndependentSiteController.getSiteOrders
);

router.get(
  '/independent-site/:id/analytics',
  requireTraceContext,
  requirePermission('site:read'),
  IndependentSiteController.getSiteAnalytics
);

// CostTemplate routes
router.get(
  '/cost-template',
  requireTraceContext,
  requirePermission('settings:read'),
  CostTemplateController.list
);

router.get(
  '/cost-template/:id',
  requireTraceContext,
  requirePermission('settings:read'),
  CostTemplateController.getById
);

router.post(
  '/cost-template',
  requireTraceContext,
  requirePermission('settings:write'),
  CostTemplateController.create
);

router.put(
  '/cost-template/:id',
  requireTraceContext,
  requirePermission('settings:write'),
  CostTemplateController.update
);

router.delete(
  '/cost-template/:id',
  requireTraceContext,
  requirePermission('settings:write'),
  CostTemplateController.delete
);

router.post(
  '/cost-template/:id/duplicate',
  requireTraceContext,
  requirePermission('settings:write'),
  CostTemplateController.duplicate
);

router.post(
  '/cost-template/:id/calculate',
  requireTraceContext,
  requirePermission('settings:read'),
  CostTemplateController.calculateCost
);

// PlatformAccount routes
router.get(
  '/platform-account',
  requireTraceContext,
  requirePermission('settings:read'),
  PlatformAccountController.list
);

router.get(
  '/platform-account/stats',
  requireTraceContext,
  requirePermission('settings:read'),
  PlatformAccountController.getStats
);

router.get(
  '/platform-account/:id',
  requireTraceContext,
  requirePermission('settings:read'),
  PlatformAccountController.getById
);

router.post(
  '/platform-account',
  requireTraceContext,
  requirePermission('settings:write'),
  PlatformAccountController.create
);

router.put(
  '/platform-account/:id',
  requireTraceContext,
  requirePermission('settings:write'),
  PlatformAccountController.update
);

router.delete(
  '/platform-account/:id',
  requireTraceContext,
  requirePermission('settings:write'),
  PlatformAccountController.delete
);

router.post(
  '/platform-account/:id/refresh-token',
  requireTraceContext,
  requirePermission('settings:write'),
  PlatformAccountController.refreshToken
);

router.post(
  '/platform-account/:id/test',
  requireTraceContext,
  requirePermission('settings:write'),
  PlatformAccountController.testConnection
);

router.post(
  '/platform-account/:id/sync',
  requireTraceContext,
  requirePermission('settings:write'),
  PlatformAccountController.sync
);

// Return routes
router.get(
  '/return/sku',
  requireTraceContext,
  requirePermission('return:read'),
  ReturnController.fetchSKUData
);

router.put(
  '/return/sku/:id/status',
  requireTraceContext,
  requirePermission('return:write'),
  ReturnController.updateSKUStatus
);

router.get(
  '/return',
  requireTraceContext,
  requirePermission('return:read'),
  ReturnController.fetchReturns
);

router.post(
  '/return',
  requireTraceContext,
  requirePermission('return:write'),
  ReturnController.createReturn
);

router.put(
  '/return/:id/status',
  requireTraceContext,
  requirePermission('return:write'),
  ReturnController.updateReturnStatus
);

router.get(
  '/return/trend',
  requireTraceContext,
  requirePermission('return:read'),
  ReturnController.getTrend
);

router.get(
  '/return/stats',
  requireTraceContext,
  requirePermission('return:read'),
  ReturnController.getStats
);

export default router;
